]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/serialization/impl/BooleanArraySerializer.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / serialization / impl / BooleanArraySerializer.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 BooleanArraySerializer extends NonRecursiveSerializer {
15
16         Range length;
17         Integer fixedLength, fixedSize;
18         
19         public BooleanArraySerializer(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 * 1;
27                 }
28         }
29         
30         @Override
31         public Object deserialize(DataInput in)
32         throws IOException, SerializationException 
33         {
34                 int length = fixedLength != null ? fixedLength : in.readInt();
35                 if (length<0) throw new SerializationException("Cannot use negative array length");
36                 assertRemainingBytes(in, length);                       
37                 
38                 boolean[] array = new boolean[length];
39                 for(int i=0;i<array.length;++i) {
40                         byte value = in.readByte();
41                         if (value==0) array[i] = false;
42                         else if (value==1) array[i] = true;
43                         else throw new SerializationException("Unexpected value \""+value+"\" for boolean");
44                 }
45                 return array;
46         }
47         
48         public Object deserializeToTry(DataInput in, List<Object> identities, Object obj) throws IOException
49         {
50                 int length = fixedLength != null ? fixedLength : in.readInt();
51                 boolean[] array = (boolean[]) obj;
52                 if (length!=array.length) array = new boolean[ length ];
53                 assertRemainingBytes(in, length);                       
54                 
55                 for (int i=0; i<array.length;i++) {
56                         byte value = in.readByte();
57                         if (value==0) array[i] = false;
58                         else if (value==1) array[i] = true;
59                         else throw new SerializationException("Unexpected value \""+value+"\" for boolean");
60                 }
61                 
62                 return array;
63         }
64
65         @Override
66         public void deserializeTo(DataInput in, Object obj)
67                         throws IOException { 
68                 int length = fixedLength != null ? fixedLength : in.readInt();
69                 boolean[] array = (boolean[]) obj;
70                 if (length!=array.length) throw new SerializationException("primitive array is size immutable");
71                 assertRemainingBytes(in, length);                       
72                 
73                 for (int i=0; i<array.length;i++) {
74                         byte value = in.readByte();
75                         if (value==0) array[i] = false;
76                         else if (value==1) array[i] = true;
77                         else throw new SerializationException("Unexpected value \""+value+"\" for boolean");
78                 }
79         }
80
81         @Override
82         public void skip(DataInput in)
83                         throws IOException {
84                 int length = fixedSize != null ? fixedLength : in.readInt();                    
85                 in.skipBytes(length);
86         }
87         
88         @Override
89         public void serialize(DataOutput out, Object obj)
90         throws IOException 
91         {
92                 boolean[] array = (boolean[])obj;
93                 if (fixedLength==null) 
94                         out.writeInt(array.length);
95                 for(boolean f : array)
96                         out.write( (byte) (f ? 1 : 0) );
97         }
98
99         @Override
100         public Integer getConstantSize() {
101                 return fixedSize;
102         }
103
104         @Override
105         public int getSize(Object obj) {
106                 if (fixedSize!=null) return fixedSize;
107                 boolean[] array = (boolean[])obj;                       
108                 return 4 + array.length;
109         }
110
111         @Override
112         public int getMinSize() {
113                 return fixedSize != null ? fixedSize : 4;
114         }
115         
116 }