1 package org.simantics.databoard.serialization.impl;
3 import java.io.DataInput;
4 import java.io.DataOutput;
5 import java.io.IOException;
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;
15 public class ByteArraySerializer extends NonRecursiveSerializer {
18 Integer fixedLength, fixedSize;
20 public ByteArraySerializer(ArrayBinding binding)
22 ArrayType arrayType = (ArrayType) binding.type();
23 this.length = arrayType.getLength();
24 if (length!=null && length.getLower().equals(length.getUpper()) && length.getLower().getValue()!=null)
26 fixedLength = length.getLower().getValue().intValue();
27 fixedSize = fixedLength * 1;
32 public Object deserialize(DataInput in)
35 int length = fixedSize != null ? fixedLength : in.readInt();
36 if (length<0) throw new SerializationException("Cannot use negative array length");
37 assertRemainingBytes(in, length);
39 byte[] array = new byte[length];
45 public void deserializeTo(DataInput in, Object obj) throws IOException {
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);
52 } catch (BindingException e) {
53 throw new IOException( e );
57 public Object deserializeToTry(DataInput in, List<Object> identities, Object obj) throws IOException
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);
68 public void skip(DataInput in)
70 int length = fixedSize != null ? fixedLength : in.readInt();
71 in.skipBytes( length );
75 public void serialize(DataOutput out, Object obj)
78 byte[] array = (byte[])obj;
80 out.writeInt(array.length);
85 public Integer getConstantSize() {
90 public int getSize(Object obj) {
91 if (fixedSize!=null) return fixedSize;
92 byte[] array = (byte[])obj;
93 return 4 + array.length;
97 public int getMinSize() {
98 return fixedSize != null ? fixedSize : 4;