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