]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/serialization/impl/FloatArraySerializer.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / serialization / impl / FloatArraySerializer.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 FloatArraySerializer extends NonRecursiveSerializer {
15
16         Range length;
17         Integer fixedLength, fixedSize;
18         
19         public FloatArraySerializer(ArrayBinding binding)
20         {                       
21                 ArrayType arrayType = (ArrayType) binding.type();
22                 this.length = arrayType.getLength();
23                 
24                 if (length!=null && length.getLower().equals(length.getUpper()) && length.getLower().getValue()!=null)
25                 {
26                         fixedLength = length.getLower().getValue().intValue();
27                         fixedSize = fixedLength * 4;
28                 }
29         }
30         
31         @Override
32         public Object deserialize(DataInput in)
33         throws IOException {
34                 int length = fixedLength != null ? fixedLength : in.readInt();
35                 if (length<0) throw new SerializationException("Cannot use negative array length");
36                 assertRemainingBytes(in, length*4L);                                    
37                 
38                 float[] array = new float[length];
39                 for(int i=0;i<array.length;++i)
40                         array[i] = in.readFloat();
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                 float[] array = (float[]) obj;
48                 if (length!=array.length) array = new float[ length ];
49                 assertRemainingBytes(in, length*4L);                                    
50                 
51                 for (int i=0; i<array.length;i++)
52                         array[i] = in.readFloat();
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                 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();
65         }
66
67         @Override
68         public void skip(DataInput in)
69         throws IOException {
70                 int length = fixedSize != null ? fixedLength : in.readInt();                    
71                 in.skipBytes( length * 4 );
72         }
73         
74         @Override
75         public void serialize(DataOutput out, Object obj)
76                 throws IOException {
77                 float[] array = (float[])obj;
78                 if (fixedSize==null) 
79                         out.writeInt(array.length);
80                 for(float f : array)
81                         out.writeFloat(f);
82         }
83
84         @Override
85         public Integer getConstantSize() {
86                 return fixedSize;
87         }
88
89         @Override
90         public int getSize(Object obj) {
91                 if (fixedSize!=null) return fixedSize;
92                 float[] array = (float[])obj;                   
93                 return 4 + 4 * array.length;
94         }
95         
96         /**
97          * Get the number of bytes it takes to output length field of value <code>length</code>.
98          *  
99          * @param length length value
100          * @return bytes required
101          */
102         public static int getSizeOfPutLength(int length)
103         {
104                 if(length < 0x80) return 1;             
105                 if(length < 0x4080) return 2;
106                 if(length < 0x204000) return 3;
107                 if(length < 0x10200000) return 4;
108                 return 5;
109         }
110         
111         @Override
112         public int getMinSize() {
113                 return fixedSize != null ? fixedSize : 4;
114         }
115                 
116 }