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