1 package org.simantics.databoard.serialization.impl;
3 import java.io.DataInput;
4 import java.io.DataOutput;
5 import java.io.IOException;
8 import org.simantics.databoard.binding.ArrayBinding;
9 import org.simantics.databoard.serialization.SerializationException;
10 import org.simantics.databoard.serialization.Serializer.NonRecursiveSerializer;
11 import org.simantics.databoard.type.ArrayType;
12 import org.simantics.databoard.util.Range;
14 public class BooleanArraySerializer extends NonRecursiveSerializer {
17 Integer fixedLength, fixedSize;
19 public BooleanArraySerializer(ArrayBinding binding)
21 ArrayType arrayType = (ArrayType) binding.type();
22 this.length = arrayType.getLength();
23 if (length!=null && length.getLower().equals(length.getUpper()) && length.getLower().getValue()!=null)
25 fixedLength = length.getLower().getValue().intValue();
26 fixedSize = fixedLength * 1;
31 public Object deserialize(DataInput in)
32 throws IOException, SerializationException
34 int length = fixedLength != null ? fixedLength : in.readInt();
35 if (length<0) throw new SerializationException("Cannot use negative array length");
36 assertRemainingBytes(in, length);
38 boolean[] array = new boolean[length];
39 for(int i=0;i<array.length;++i) {
40 byte value = in.readByte();
41 if (value==0) array[i] = false;
42 else if (value==1) array[i] = true;
43 else throw new SerializationException("Unexpected value \""+value+"\" for boolean");
48 public Object deserializeToTry(DataInput in, List<Object> identities, Object obj) throws IOException
50 int length = fixedLength != null ? fixedLength : in.readInt();
51 boolean[] array = (boolean[]) obj;
52 if (length!=array.length) array = new boolean[ length ];
53 assertRemainingBytes(in, length);
55 for (int i=0; i<array.length;i++) {
56 byte value = in.readByte();
57 if (value==0) array[i] = false;
58 else if (value==1) array[i] = true;
59 else throw new SerializationException("Unexpected value \""+value+"\" for boolean");
66 public void deserializeTo(DataInput in, Object obj)
68 int length = fixedLength != null ? fixedLength : in.readInt();
69 boolean[] array = (boolean[]) obj;
70 if (length!=array.length) throw new SerializationException("primitive array is size immutable");
71 assertRemainingBytes(in, length);
73 for (int i=0; i<array.length;i++) {
74 byte value = in.readByte();
75 if (value==0) array[i] = false;
76 else if (value==1) array[i] = true;
77 else throw new SerializationException("Unexpected value \""+value+"\" for boolean");
82 public void skip(DataInput in)
84 int length = fixedSize != null ? fixedLength : in.readInt();
89 public void serialize(DataOutput out, Object obj)
92 boolean[] array = (boolean[])obj;
93 if (fixedLength==null)
94 out.writeInt(array.length);
95 for(boolean f : array)
96 out.write( (byte) (f ? 1 : 0) );
100 public Integer getConstantSize() {
105 public int getSize(Object obj) {
106 if (fixedSize!=null) return fixedSize;
107 boolean[] array = (boolean[])obj;
108 return 4 + array.length;
112 public int getMinSize() {
113 return fixedSize != null ? fixedSize : 4;