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.binding.error.BindingException;
10 import org.simantics.databoard.serialization.SerializationException;
11 import org.simantics.databoard.serialization.Serializer.NonRecursiveSerializer;
12 import org.simantics.databoard.type.ArrayType;
13 import org.simantics.databoard.util.Range;
15 public class LongArraySerializer extends NonRecursiveSerializer {
18 Integer fixedLength, fixedSize;
20 public LongArraySerializer(ArrayBinding binding)
22 ArrayType arrayType = (ArrayType) binding.type();
23 this.length = arrayType.getLength();
24 if (length!=null && length.getLower().equals(length.getUpper()) && length.getLower().getValue()!=null)
26 fixedLength = length.getLower().getValue().intValue();
27 fixedSize = fixedLength * 8;
32 public Object deserialize(DataInput in)
35 int length = fixedSize != null ? fixedLength : in.readInt();
36 if (length<0) throw new SerializationException("Cannot use negative array length");
37 assertRemainingBytes(in, length*8L);
39 long[] array = new long[length];
40 for(int i=0;i<array.length;++i)
41 array[i] = in.readLong();
45 public Object deserializeToTry(DataInput in, List<Object> identities, Object obj) throws IOException
47 int length = fixedLength != null ? fixedLength : in.readInt();
48 long[] array = (long[]) obj;
49 if (length!=array.length) array = new long[ length ];
50 assertRemainingBytes(in, length*8L);
52 for (int i=0; i<array.length;i++)
53 array[i] = in.readLong();
60 public void deserializeTo(DataInput in, Object obj) throws IOException {
62 int length = fixedLength != null ? fixedLength : in.readInt();
63 long[] array = (long[]) obj;
64 if (length!=array.length) throw new BindingException("primitive array is size immutable");
65 assertRemainingBytes(in, length*8L);
66 for (int i=0; i<array.length;i++)
67 array[i] = in.readLong();
68 } catch (BindingException e) {
69 throw new IOException( e );
74 public void skip(DataInput in)
76 int length = fixedSize != null ? fixedLength : in.readInt();
77 in.skipBytes(8* length);
81 public void serialize(DataOutput out, Object obj)
84 long[] array = (long[])obj;
86 out.writeInt(array.length);
92 public Integer getConstantSize() {
97 public int getSize(Object obj) {
98 if (fixedSize!=null) return fixedSize;
99 long[] array = (long[])obj;
100 return 4 + 8 * array.length;
104 public int getMinSize() {
105 return fixedSize != null ? fixedSize : 4;