1 package org.simantics.databoard.serialization.impl;
3 import java.io.DataInput;
4 import java.io.DataOutput;
5 import java.io.IOException;
7 import org.simantics.databoard.binding.StringBinding;
8 import org.simantics.databoard.binding.error.BindingException;
9 import org.simantics.databoard.serialization.SerializationException;
10 import org.simantics.databoard.serialization.Serializer.NonRecursiveSerializer;
11 import org.simantics.databoard.util.binary.Endian;
12 import org.simantics.databoard.util.binary.UTF8;
15 * Serializes strings to Modified UTF-8,
16 * see http://download.oracle.com/javase/6/docs/api/java/io/DataInput.html
18 * @author Toni Kalajainen
20 public class ModifiedUTF8StringSerializer extends NonRecursiveSerializer {
22 StringBinding binding;
24 public ModifiedUTF8StringSerializer(StringBinding binding) {
25 this.binding = binding;
29 public Object deserialize(DataInput in) throws IOException {
31 int len = Endian.readDynamicUInt32(in);
32 assertRemainingBytes(in, len);
33 String str = UTF8.readModifiedUTF(in, len);
34 return binding.create(str);
35 } catch (BindingException e) {
36 throw new IOException( e );
42 public void deserializeTo(DataInput in, Object obj) throws IOException {
44 int len = Endian.readDynamicUInt32(in);
45 assertRemainingBytes(in, len);
46 String str = UTF8.readModifiedUTF(in, len);
47 binding.setValue(obj, str);
48 } catch (BindingException e) {
49 throw new IOException( e );
54 public void skip(DataInput in) throws IOException, SerializationException {
55 int length = Endian.readDynamicUInt32(in);
60 public void serialize(DataOutput out, Object obj) throws IOException {
62 String str = binding.getValue(obj);
63 int utflen = UTF8.getModifiedUTF8EncodingByteLength(str);
64 Endian.writeDynamicUInt32(out, utflen);
65 UTF8.writeModifiedUTF(out, str);
66 } catch (BindingException e) {
67 throw new IOException( e );
72 public Integer getConstantSize() {
77 public int getSize(Object obj) throws IOException {
79 String string = binding.getValue(obj);
80 int stringByteLength = UTF8.getModifiedUTF8EncodingByteLength( string );
81 int lengthLength = Endian.getDynamicUInt32Length( stringByteLength );
82 return lengthLength + stringByteLength;
83 } catch (BindingException e) {
84 throw new IOException( e );
89 public int getMinSize() {