Minor, move byte arrays logic to ClassData/PackageData

This commit is contained in:
Alexander Udalov
2013-07-08 18:41:07 +04:00
parent e63cef0848
commit 93d200be82
6 changed files with 73 additions and 48 deletions
@@ -21,6 +21,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.utils.ExceptionUtils;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
public final class ClassData {
@@ -55,4 +56,17 @@ public final class ClassData {
public ProtoBuf.Class getClassProto() {
return classProto;
}
@NotNull
public byte[] toBytes() {
try {
ByteArrayOutputStream result = new ByteArrayOutputStream();
NameSerializationUtil.serializeNameResolver(result, nameResolver);
classProto.writeTo(result);
return result.toByteArray();
}
catch (IOException e) {
throw ExceptionUtils.rethrow(e);
}
}
}
@@ -36,6 +36,16 @@ public class NameResolver {
this.qualifiedNames = qualifiedNames;
}
@NotNull
public ProtoBuf.SimpleNameTable getSimpleNameTable() {
return simpleNames;
}
@NotNull
public ProtoBuf.QualifiedNameTable getQualifiedNameTable() {
return qualifiedNames;
}
@NotNull
public Name getName(int index) {
String name = simpleNames.getName(index);
@@ -39,12 +39,21 @@ public class NameSerializationUtil {
}
}
public static void serializeNameTable(@NotNull OutputStream out, @NotNull NameTable nameTable) {
try {
ProtoBuf.SimpleNameTable simpleNamesProto = toSimpleNameTable(nameTable);
simpleNamesProto.writeDelimitedTo(out);
public static void serializeNameResolver(@NotNull OutputStream out, @NotNull NameResolver nameResolver) {
serializeNameTable(out, nameResolver.getSimpleNameTable(), nameResolver.getQualifiedNameTable());
}
ProtoBuf.QualifiedNameTable qualifiedNameTable = toQualifiedNameTable(nameTable);
public static void serializeNameTable(@NotNull OutputStream out, @NotNull NameTable nameTable) {
serializeNameTable(out, toSimpleNameTable(nameTable), toQualifiedNameTable(nameTable));
}
private static void serializeNameTable(
@NotNull OutputStream out,
@NotNull ProtoBuf.SimpleNameTable simpleNameTable,
@NotNull ProtoBuf.QualifiedNameTable qualifiedNameTable
) {
try {
simpleNameTable.writeDelimitedTo(out);
qualifiedNameTable.writeDelimitedTo(out);
}
catch (IOException e) {
@@ -72,8 +81,6 @@ public class NameSerializationUtil {
@NotNull
public static NameResolver createNameResolver(@NotNull NameTable table) {
return new NameResolver(
toSimpleNameTable(table),
toQualifiedNameTable(table));
return new NameResolver(toSimpleNameTable(table), toQualifiedNameTable(table));
}
}
@@ -21,6 +21,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.utils.ExceptionUtils;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
public final class PackageData {
@@ -41,16 +42,31 @@ public final class PackageData {
private final ProtoBuf.Package packageProto;
public PackageData(@NotNull NameResolver resolver, @NotNull ProtoBuf.Package proto) {
nameResolver = resolver;
packageProto = proto;
public PackageData(@NotNull NameResolver nameResolver, @NotNull ProtoBuf.Package packageProto) {
this.nameResolver = nameResolver;
this.packageProto = packageProto;
}
@NotNull
public NameResolver getNameResolver() {
return nameResolver;
}
@NotNull
public ProtoBuf.Package getPackageProto() {
return packageProto;
}
public NameResolver getNameResolver() {
return nameResolver;
@NotNull
public byte[] toBytes() {
try {
ByteArrayOutputStream result = new ByteArrayOutputStream();
NameSerializationUtil.serializeNameResolver(result, nameResolver);
packageProto.writeTo(result);
return result.toByteArray();
}
catch (IOException e) {
throw ExceptionUtils.rethrow(e);
}
}
}