]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/serialization/impl/ByteArraySerializer.java
isImmutable can NPE
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / serialization / impl / ByteArraySerializer.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 ByteArraySerializer extends NonRecursiveSerializer {
16
17         Range length;
18         Integer fixedLength, fixedSize;
19         
20         public ByteArraySerializer(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 * 1;
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);                       
38                 
39                 byte[] array = new byte[length];
40                 in.readFully(array);
41                 return array;
42         }
43         
44         @Override
45         public void deserializeTo(DataInput in, Object obj) throws IOException {
46                 try {
47                         int length = fixedLength != null ? fixedLength : in.readInt();                  
48                         byte[] array = (byte[]) obj;
49                         if (length!=array.length) throw new BindingException("primitive array is size immutable");
50                         assertRemainingBytes(in, length);                       
51                         in.readFully(array);
52                 } catch (BindingException e) {
53                         throw new IOException( e ); 
54                 }
55         }
56         
57         public Object deserializeToTry(DataInput in, List<Object> identities, Object obj) throws IOException
58         {
59                 int length = fixedLength != null ? fixedLength : in.readInt();
60                 byte[] array = (byte[]) obj;
61                 if (length!=array.length) obj = new byte[ length ];
62                 assertRemainingBytes(in, length);                       
63                 in.readFully(array);            
64                 return array;
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 );
72         }
73         
74         @Override
75         public void serialize(DataOutput out, Object obj)
76         throws IOException 
77         {
78                 byte[] array = (byte[])obj;
79                 if (fixedSize==null) 
80                         out.writeInt(array.length);
81                 out.write(array);
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                 byte[] array = (byte[])obj;                     
93                 return 4 + array.length;
94         }
95         
96         @Override
97         public int getMinSize() {
98                 return fixedSize != null ? fixedSize : 4;
99         }
100         
101 }