]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/serialization/impl/ModifiedUTF8StringSerializer.java
Add qualifier for SLF4J version to force update of JAR in TP
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / serialization / impl / ModifiedUTF8StringSerializer.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
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;
13
14 /**
15  * Serializes strings to Modified UTF-8, 
16  * see http://download.oracle.com/javase/6/docs/api/java/io/DataInput.html
17  * 
18  * @author Toni Kalajainen
19  */
20 public class ModifiedUTF8StringSerializer extends NonRecursiveSerializer {
21         
22         StringBinding binding;
23         
24         public ModifiedUTF8StringSerializer(StringBinding binding) {
25                 this.binding = binding;
26         }
27
28         @Override
29         public Object deserialize(DataInput in) throws IOException {
30                 try {
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 ); 
37                 }
38                         
39         }
40         
41         @Override
42         public void deserializeTo(DataInput in, Object obj) throws IOException {
43                 try {
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 ); 
50                 }
51         }
52         
53         @Override
54         public void skip(DataInput in) throws IOException, SerializationException {
55                 int length = Endian.readDynamicUInt32(in);
56                 in.skipBytes(length);
57         }
58         
59         @Override
60         public void serialize(DataOutput out, Object obj) throws IOException {
61                 try {
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 ); 
68                 }
69         }
70         
71         @Override
72         public Integer getConstantSize() {                      
73                 return null;
74         }
75
76         @Override
77         public int getSize(Object obj) throws IOException {
78                 try {
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 ); 
85                 }
86         }
87         
88         @Override
89         public int getMinSize() {
90                 return 1;
91         }
92         
93 }
94