1 package org.simantics.databoard.streaming;
3 import java.io.IOException;
4 import java.io.PrintStream;
6 import org.simantics.databoard.Bindings;
7 import org.simantics.databoard.serialization.Serializer;
8 import org.simantics.databoard.type.ArrayType;
9 import org.simantics.databoard.type.BooleanType;
10 import org.simantics.databoard.type.ByteType;
11 import org.simantics.databoard.type.Component;
12 import org.simantics.databoard.type.Datatype;
13 import org.simantics.databoard.type.Datatype.Visitor;
14 import org.simantics.databoard.type.DoubleType;
15 import org.simantics.databoard.type.FloatType;
16 import org.simantics.databoard.type.IntegerType;
17 import org.simantics.databoard.type.LongType;
18 import org.simantics.databoard.type.MapType;
19 import org.simantics.databoard.type.OptionalType;
20 import org.simantics.databoard.type.RecordType;
21 import org.simantics.databoard.type.StringType;
22 import org.simantics.databoard.type.UnionType;
23 import org.simantics.databoard.type.VariantType;
24 import org.simantics.databoard.util.Range;
25 import org.simantics.databoard.util.binary.Endian;
28 * Utility for debugging corrupted values.
30 public class DataSerializationDebugger {
35 private void indentation() {
36 for(int i=0;i<indentation;++i)
41 * Constructs the debugger that reads the given stream and prints the value
42 * content to given print stream.
44 public DataSerializationDebugger(DataReader in, PrintStream out) {
50 * Expects value of given datatype in the input stream.
52 public void expect(Datatype datatype) throws IOException {
53 if(datatype instanceof BooleanType)
55 else if(datatype instanceof ByteType)
57 else if(datatype instanceof IntegerType)
59 else if(datatype instanceof LongType)
61 else if(datatype instanceof FloatType)
63 else if(datatype instanceof DoubleType)
65 else if(datatype instanceof StringType)
67 else if(datatype instanceof RecordType)
68 expectRecord((RecordType)datatype);
69 else if(datatype instanceof ArrayType)
70 expectArray((ArrayType)datatype);
71 else if(datatype instanceof MapType)
72 expectMap((MapType)datatype);
73 else if(datatype instanceof OptionalType)
74 expectOptional((OptionalType)datatype);
75 else if(datatype instanceof UnionType)
76 expectUnion((UnionType)datatype);
77 else if(datatype instanceof VariantType)
81 private void expectVariant() throws IOException {
82 out.print("Variant:");
83 Datatype datatype = in.readDatatype();
84 out.println(" " + datatype);
91 private void expectUnion(UnionType datatype) throws IOException {
93 int tag = in.readUnionTag(datatype.getComponentCount());
94 Component component = datatype.getComponent(tag);
95 out.print("(" + component.name + "): ");
96 expect(component.type);
99 private void expectOptional(OptionalType datatype) throws IOException {
100 out.print("Optional: ");
101 if(in.beginOptional()) {
102 expect(datatype.componentType);
108 private void expectMap(MapType datatype) throws IOException {
110 int length = in.beginMap();
111 out.println("(" + length + "):");
113 for(int i=0;i<length;++i) {
115 out.print(i+"-key: ");
116 expect(datatype.keyType);
118 out.print(i+"-value: ");
119 expect(datatype.valueType);
124 private static final Integer I1 = 1;
125 private static final Integer I4 = 4;
126 private static final Integer I8 = 8;
128 private static int getFixedLength(ArrayType b) {
129 Range length = b.getLength();
130 return length.getLower().getValue().intValue();
133 private static Visitor<Integer> FIXED_SIZE = new Visitor<Integer>() {
136 public Integer visit(ArrayType b) {
137 Range length = b.getLength();
139 || length.getLower().getValue()==null
140 || !length.getLower().equals(length.getUpper()))
143 int fixedLength = length.getLower().getValue().intValue();
145 Integer componentLength = b.componentType.accept(this);
146 if(componentLength == null)
149 return fixedLength * componentLength;
153 public Integer visit(BooleanType b) {
158 public Integer visit(DoubleType b) {
163 public Integer visit(FloatType b) {
168 public Integer visit(IntegerType b) {
173 public Integer visit(ByteType b) {
178 public Integer visit(LongType b) {
183 public Integer visit(OptionalType b) {
188 public Integer visit(RecordType b) {
192 for(Component component : b.getComponents()) {
193 Integer componentSize = component.type.accept(this);
194 if(componentSize == null)
196 sum += componentSize;
202 public Integer visit(StringType b) {
207 public Integer visit(UnionType b) {
208 Integer commonComponentSize = null;
209 for(Component component : b.components) {
210 Integer componentSize = component.type.accept(this);
211 if(componentSize == null)
213 if(commonComponentSize == null)
214 commonComponentSize = componentSize;
215 else if(!commonComponentSize.equals(componentSize))
218 if(commonComponentSize == null)
220 return commonComponentSize + Endian.getUIntLength(b.components.length-1);
224 public Integer visit(VariantType b) {
229 public Integer visit(MapType b) {
235 private void expectArray(ArrayType datatype) throws IOException {
238 if(datatype.accept(FIXED_SIZE) == null)
239 length = in.beginVariableLengthArray();
241 length = getFixedLength(datatype);
242 out.println("(" + length + "):");
244 for(int i=0;i<length;++i) {
248 expect(datatype.componentType);
253 private void expectRecord(RecordType datatype) throws IOException {
254 out.print("Record:");
255 if(datatype.isReferable()) {
256 int ref = in.readReferableRecordReference();
258 out.println(" old " + ref);
268 for(Component component : datatype.getComponents()) {
270 out.print(component.name);
272 expect(component.type);
277 private void expectString() throws IOException {
279 int length = in.readStringLength();
280 out.print("(" + length + "): ");
281 out.println(in.readStringContent(length));
284 private void expectDouble() throws IOException {
285 out.print("Double: ");
286 out.println(in.readDouble());
289 private void expectFloat() throws IOException {
290 out.print("Float: ");
291 out.println(in.readFloat());
294 private void expectLong() throws IOException {
296 out.println(in.readLong());
299 private void expectInteger() throws IOException {
300 out.print("Integer: ");
301 out.println(in.readInteger());
304 private void expectByte() throws IOException {
306 out.println(in.readByte());
309 private void expectBoolean() throws IOException {
310 out.print("Boolean: ");
311 out.println(in.readBoolean());