]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/serialization/impl/DoubleArraySerializer.java
isImmutable can NPE
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / serialization / impl / DoubleArraySerializer.java
1 package org.simantics.databoard.serialization.impl;
2
3 import java.io.DataInput;
4 import java.io.DataOutput;
5 import java.io.IOException;
6 import java.util.List;
7
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;
13
14 public class DoubleArraySerializer extends NonRecursiveSerializer {
15
16         Range length;
17         Integer fixedLength, fixedSize;
18         
19         public DoubleArraySerializer(ArrayBinding binding)
20         {
21                 ArrayType arrayType = (ArrayType) binding.type();                       
22                 this.length = arrayType.getLength();
23                 if (length!=null && length.getLower().equals(length.getUpper()) && length.getLower().getValue()!=null)
24                 {
25                         fixedLength = length.getLower().getValue().intValue();
26                         fixedSize = fixedLength * 8;
27                 }
28         }
29         
30         @Override
31         public Object deserialize(DataInput in)
32         throws IOException 
33         {
34                 int length = fixedSize != null ? fixedLength : in.readInt();
35                 if (length<0) throw new SerializationException("Cannot use negative array length");
36                 assertRemainingBytes(in, length*8L);                                    
37                 
38                 double[] array = new double[length];
39                 for(int i=0;i<array.length;++i)
40                         array[i] = in.readDouble();
41                 return array;
42         }
43         
44         public Object deserializeToTry(DataInput in, List<Object> identities, Object obj) throws IOException
45         {
46                 int length = fixedLength != null ? fixedLength : in.readInt();
47                 double[] array = (double[]) obj;
48                 if (length!=array.length) array = new double[ length ];
49                 assertRemainingBytes(in, length*8L);                                    
50                 
51                 for (int i=0; i<array.length;i++)
52                         array[i] = in.readDouble();
53                 
54                 return array;
55         }
56
57         @Override
58         public void deserializeTo(DataInput in, Object obj) throws IOException {
59                 int length = fixedLength != null ? fixedLength : in.readInt();          
60                 double[] array = (double[]) obj;
61                 if (length!=array.length) throw new SerializationException("primitive array is size immutable");
62                 assertRemainingBytes(in, length*8L);
63                 
64                 for (int i=0; i<array.length;i++)
65                         array[i] = in.readDouble();
66         }
67         
68         @Override
69         public void skip(DataInput in)
70         throws IOException {
71                 int length = fixedSize != null ? fixedLength : in.readInt();                    
72                 in.skipBytes(length * 8);
73         }
74         
75         @Override
76         public void serialize(DataOutput out, Object obj)
77         throws IOException 
78         {
79                 double[] array = (double[])obj;
80                 if (fixedSize==null) 
81                         out.writeInt(array.length);
82                 for(double f : array)
83                         out.writeDouble(f);
84         }
85
86         @Override
87         public Integer getConstantSize() {
88                 return fixedSize;
89         }
90
91         @Override
92         public int getSize(Object obj) {
93                 if (fixedSize!=null) return fixedSize;
94                 double[] array = (double[])obj;                 
95                 return 4 + 8 * array.length;
96         }
97         
98         @Override
99         public int getMinSize() {
100                 return fixedSize != null ? fixedSize : 4;
101         }
102         
103 }
104