--- /dev/null
+package org.simantics.databoard.serialization.impl;\r
+\r
+import java.io.DataInput;\r
+import java.io.DataOutput;\r
+import java.io.IOException;\r
+import java.util.List;\r
+\r
+import org.simantics.databoard.binding.ArrayBinding;\r
+import org.simantics.databoard.serialization.SerializationException;\r
+import org.simantics.databoard.serialization.Serializer.NonRecursiveSerializer;\r
+import org.simantics.databoard.type.ArrayType;\r
+import org.simantics.databoard.util.Range;\r
+\r
+public class BooleanArraySerializer extends NonRecursiveSerializer {\r
+\r
+ Range length;\r
+ Integer fixedLength, fixedSize;\r
+ \r
+ public BooleanArraySerializer(ArrayBinding binding)\r
+ {\r
+ ArrayType arrayType = (ArrayType) binding.type();\r
+ this.length = arrayType.getLength();\r
+ if (length!=null && length.getLower().equals(length.getUpper()) && length.getLower().getValue()!=null)\r
+ {\r
+ fixedLength = length.getLower().getValue().intValue();\r
+ fixedSize = fixedLength * 1;\r
+ }\r
+ }\r
+ \r
+ @Override\r
+ public Object deserialize(DataInput in)\r
+ throws IOException, SerializationException \r
+ {\r
+ int length = fixedLength != null ? fixedLength : in.readInt();\r
+ if (length<0) throw new SerializationException("Cannot use negative array length");\r
+ assertRemainingBytes(in, length); \r
+ \r
+ boolean[] array = new boolean[length];\r
+ for(int i=0;i<array.length;++i) {\r
+ byte value = in.readByte();\r
+ if (value==0) array[i] = false;\r
+ else if (value==1) array[i] = true;\r
+ else throw new SerializationException("Unexpected value \""+value+"\" for boolean");\r
+ }\r
+ return array;\r
+ }\r
+ \r
+ public Object deserializeToTry(DataInput in, List<Object> identities, Object obj) throws IOException\r
+ {\r
+ int length = fixedLength != null ? fixedLength : in.readInt();\r
+ boolean[] array = (boolean[]) obj;\r
+ if (length!=array.length) array = new boolean[ length ];\r
+ assertRemainingBytes(in, length); \r
+ \r
+ for (int i=0; i<array.length;i++) {\r
+ byte value = in.readByte();\r
+ if (value==0) array[i] = false;\r
+ else if (value==1) array[i] = true;\r
+ else throw new SerializationException("Unexpected value \""+value+"\" for boolean");\r
+ }\r
+ \r
+ return array;\r
+ }\r
+\r
+ @Override\r
+ public void deserializeTo(DataInput in, Object obj)\r
+ throws IOException { \r
+ int length = fixedLength != null ? fixedLength : in.readInt();\r
+ boolean[] array = (boolean[]) obj;\r
+ if (length!=array.length) throw new SerializationException("primitive array is size immutable");\r
+ assertRemainingBytes(in, length); \r
+ \r
+ for (int i=0; i<array.length;i++) {\r
+ byte value = in.readByte();\r
+ if (value==0) array[i] = false;\r
+ else if (value==1) array[i] = true;\r
+ else throw new SerializationException("Unexpected value \""+value+"\" for boolean");\r
+ }\r
+ }\r
+\r
+ @Override\r
+ public void skip(DataInput in)\r
+ throws IOException {\r
+ int length = fixedSize != null ? fixedLength : in.readInt(); \r
+ in.skipBytes(length);\r
+ }\r
+ \r
+ @Override\r
+ public void serialize(DataOutput out, Object obj)\r
+ throws IOException \r
+ {\r
+ boolean[] array = (boolean[])obj;\r
+ if (fixedLength==null) \r
+ out.writeInt(array.length);\r
+ for(boolean f : array)\r
+ out.write( (byte) (f ? 1 : 0) );\r
+ }\r
+\r
+ @Override\r
+ public Integer getConstantSize() {\r
+ return fixedSize;\r
+ }\r
+\r
+ @Override\r
+ public int getSize(Object obj) {\r
+ if (fixedSize!=null) return fixedSize;\r
+ boolean[] array = (boolean[])obj; \r
+ return 4 + array.length;\r
+ }\r
+\r
+ @Override\r
+ public int getMinSize() {\r
+ return fixedSize != null ? fixedSize : 4;\r
+ }\r
+ \r
+}
\ No newline at end of file