]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/serialization/impl/LongArraySerializer.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / serialization / impl / LongArraySerializer.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.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;
14
15 public class LongArraySerializer extends NonRecursiveSerializer {
16
17         Range length;
18         Integer fixedLength, fixedSize;
19         
20         public LongArraySerializer(ArrayBinding binding)
21         {
22                 ArrayType arrayType = (ArrayType) binding.type();
23                 this.length = arrayType.getLength();
24                 if (length!=null && length.getLower().equals(length.getUpper()) && length.getLower().getValue()!=null)
25                 {
26                         fixedLength = length.getLower().getValue().intValue();
27                         fixedSize = fixedLength * 8;
28                 }
29         }
30         
31         @Override
32         public Object deserialize(DataInput in)
33         throws IOException 
34         {
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);                                    
38
39                 long[] array = new long[length];
40                 for(int i=0;i<array.length;++i)
41                         array[i] = in.readLong();
42                 return array;
43         }
44         
45         public Object deserializeToTry(DataInput in, List<Object> identities, Object obj) throws IOException
46         {
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);                                    
51                 
52                 for (int i=0; i<array.length;i++)
53                         array[i] = in.readLong();
54                 
55                 return array;
56         }
57         
58
59         @Override
60         public void deserializeTo(DataInput in, Object obj) throws IOException {
61                 try {
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 ); 
70                 }
71         }
72
73         @Override
74         public void skip(DataInput in)
75         throws IOException {
76                 int length = fixedSize != null ? fixedLength : in.readInt();                    
77                 in.skipBytes(8* length);                        
78         }
79         
80         @Override
81         public void serialize(DataOutput out, Object obj)
82         throws IOException 
83         {
84                 long[] array = (long[])obj;
85                 if (fixedSize==null) 
86                         out.writeInt(array.length);
87                 for(long f : array)
88                         out.writeLong(f);
89         }
90
91         @Override
92         public Integer getConstantSize() {
93                 return fixedSize;
94         }
95
96         @Override
97         public int getSize(Object obj) {
98                 if (fixedSize!=null) return fixedSize;
99                 long[] array = (long[])obj;                     
100                 return 4 + 8 * array.length;
101         }
102         
103         @Override
104         public int getMinSize() {
105                 return fixedSize != null ? fixedSize : 4;
106         }
107         
108 }