1 package org.simantics.databoard.serialization.impl;
\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
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
15 public class ByteArraySerializer extends NonRecursiveSerializer {
\r
18 Integer fixedLength, fixedSize;
\r
20 public ByteArraySerializer(ArrayBinding binding)
\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
26 fixedLength = length.getLower().getValue().intValue();
\r
27 fixedSize = fixedLength * 1;
\r
32 public Object deserialize(DataInput in)
\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
39 byte[] array = new byte[length];
\r
40 in.readFully(array);
\r
45 public void deserializeTo(DataInput in, Object obj) throws IOException {
\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
57 public Object deserializeToTry(DataInput in, List<Object> identities, Object obj) throws IOException
\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
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
75 public void serialize(DataOutput out, Object obj)
\r
78 byte[] array = (byte[])obj;
\r
79 if (fixedSize==null)
\r
80 out.writeInt(array.length);
\r
85 public Integer getConstantSize() {
\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
97 public int getMinSize() {
\r
98 return fixedSize != null ? fixedSize : 4;
\r