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 IntArraySerializer extends NonRecursiveSerializer {
17 Integer fixedLength, fixedSize;
19 public IntArraySerializer(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 * 4;
31 public Object deserialize(DataInput in)
34 int length = fixedSize != null ? fixedLength : in.readInt();
35 if (length<0) throw new SerializationException("Cannot use negative array length");
36 assertRemainingBytes(in, length*4L);
38 int[] array = new int[length];
39 for(int i=0;i<array.length;++i)
40 array[i] = in.readInt();
44 public Object deserializeToTry(DataInput in, List<Object> identities, Object obj) throws IOException
46 int length = fixedLength != null ? fixedLength : in.readInt();
47 int[] array = (int[]) obj;
48 if (length!=array.length) array = new int[ length ];
49 assertRemainingBytes(in, length*4L);
51 for (int i=0; i<array.length;i++)
52 array[i] = in.readInt();
58 public void deserializeTo(DataInput in, Object obj) throws IOException {
59 int length = fixedLength != null ? fixedLength : in.readInt();
60 float[] array = (float[]) obj;
61 if (length!=array.length) throw new SerializationException("primitive array is size immutable");
62 assertRemainingBytes(in, length*4L);
63 for (int i=0; i<array.length;i++)
64 array[i] = in.readFloat();
68 public void skip(DataInput in)
70 int length = fixedSize != null ? fixedLength : in.readInt();
71 in.skipBytes(length*4);
75 public void serialize(DataOutput out, Object obj)
78 int[] array = (int[])obj;
80 out.writeInt(array.length);
86 public Integer getConstantSize() {
91 public int getSize(Object obj) {
92 if (fixedSize!=null) return fixedSize;
93 int[] array = (int[])obj;
94 return 4 + 4 * array.length;
98 public int getMinSize() {
99 return fixedSize != null ? fixedSize : 4;