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 DoubleArraySerializer extends NonRecursiveSerializer {
\r
17 Integer fixedLength, fixedSize;
\r
19 public DoubleArraySerializer(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 * 8;
\r
31 public Object deserialize(DataInput in)
\r
34 int length = fixedSize != null ? fixedLength : in.readInt();
\r
35 if (length<0) throw new SerializationException("Cannot use negative array length");
\r
36 assertRemainingBytes(in, length*8L);
\r
38 double[] array = new double[length];
\r
39 for(int i=0;i<array.length;++i)
\r
40 array[i] = in.readDouble();
\r
44 public Object deserializeToTry(DataInput in, List<Object> identities, Object obj) throws IOException
\r
46 int length = fixedLength != null ? fixedLength : in.readInt();
\r
47 double[] array = (double[]) obj;
\r
48 if (length!=array.length) array = new double[ length ];
\r
49 assertRemainingBytes(in, length*8L);
\r
51 for (int i=0; i<array.length;i++)
\r
52 array[i] = in.readDouble();
\r
58 public void deserializeTo(DataInput in, Object obj) throws IOException {
\r
59 int length = fixedLength != null ? fixedLength : in.readInt();
\r
60 double[] array = (double[]) obj;
\r
61 if (length!=array.length) throw new SerializationException("primitive array is size immutable");
\r
62 assertRemainingBytes(in, length*8L);
\r
64 for (int i=0; i<array.length;i++)
\r
65 array[i] = in.readDouble();
\r
69 public void skip(DataInput in)
\r
70 throws IOException {
\r
71 int length = fixedSize != null ? fixedLength : in.readInt();
\r
72 in.skipBytes(length * 8);
\r
76 public void serialize(DataOutput out, Object obj)
\r
79 double[] array = (double[])obj;
\r
80 if (fixedSize==null)
\r
81 out.writeInt(array.length);
\r
82 for(double f : array)
\r
87 public Integer getConstantSize() {
\r
92 public int getSize(Object obj) {
\r
93 if (fixedSize!=null) return fixedSize;
\r
94 double[] array = (double[])obj;
\r
95 return 4 + 8 * array.length;
\r
99 public int getMinSize() {
\r
100 return fixedSize != null ? fixedSize : 4;
\r