]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.databoard/src/org/simantics/databoard/serialization/impl/UTF8StringSerializer.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / serialization / impl / UTF8StringSerializer.java
diff --git a/bundles/org.simantics.databoard/src/org/simantics/databoard/serialization/impl/UTF8StringSerializer.java b/bundles/org.simantics.databoard/src/org/simantics/databoard/serialization/impl/UTF8StringSerializer.java
new file mode 100644 (file)
index 0000000..11b9385
--- /dev/null
@@ -0,0 +1,91 @@
+package org.simantics.databoard.serialization.impl;\r
+\r
+import java.io.DataInput;\r
+import java.io.DataOutput;\r
+import java.io.IOException;\r
+\r
+import org.simantics.databoard.binding.StringBinding;\r
+import org.simantics.databoard.binding.error.BindingException;\r
+import org.simantics.databoard.serialization.Serializer.NonRecursiveSerializer;\r
+import org.simantics.databoard.util.binary.Endian;\r
+import org.simantics.databoard.util.binary.UTF8;\r
+\r
+/**\r
+ * Serializes Strings to UTF8 strings\r
+ * \r
+ * @author Toni Kalajainen\r
+ */\r
+public class UTF8StringSerializer extends NonRecursiveSerializer {\r
+\r
+       \r
+       StringBinding binding;\r
+       \r
+       public UTF8StringSerializer(StringBinding binding) {\r
+               this.binding = binding;\r
+       }\r
+\r
+       @Override\r
+       public Object deserialize(DataInput in) throws IOException {\r
+               try {\r
+                       int length = Endian.readDynamicUInt32(in);\r
+                       assertRemainingBytes(in, length); \r
+                       String str = UTF8.readUTF(in, length);\r
+                       return binding.create(str);\r
+               } catch (BindingException e) {\r
+                       throw new IOException( e ); \r
+               }\r
+       }\r
+       \r
+       @Override\r
+       public void deserializeTo(DataInput in, Object obj) throws IOException {\r
+               try {\r
+                       int length = Endian.readDynamicUInt32(in);\r
+                       assertRemainingBytes(in, length); \r
+                       String str = UTF8.readUTF(in, length);\r
+                       binding.setValue(obj, str);\r
+               } catch (BindingException e) {\r
+                       throw new IOException( e ); \r
+               }\r
+       }\r
+\r
+       @Override\r
+       public void skip(DataInput in) throws IOException {             \r
+               int length = Endian.readDynamicUInt32(in);\r
+               in.skipBytes(length);\r
+       }\r
+       \r
+       @Override\r
+       public void serialize(DataOutput out, Object obj) throws IOException {\r
+               try {\r
+                       byte[] bytes = binding.getValue(obj).getBytes(UTF8.CHARSET);\r
+                       Endian.writeDynamicUInt32(out, bytes.length);   \r
+                       out.write(bytes);\r
+               } catch (BindingException e) {\r
+                       throw new IOException( e ); \r
+               }\r
+       }\r
+\r
+       @Override\r
+       public Integer getConstantSize() {                      \r
+               return null;\r
+       }\r
+\r
+       @Override\r
+       public int getSize(Object obj) throws IOException {\r
+               try {\r
+                       String string = binding.getValue(obj);\r
+                       int stringByteLength = UTF8.getUTF8EncodingByteLength( string ); \r
+                       int lengthLength = Endian.getDynamicUInt32Length( stringByteLength );                           \r
+                       return lengthLength + stringByteLength;\r
+               } catch (BindingException e) {\r
+                       throw new IOException( e ); \r
+               }\r
+       }\r
+\r
+       @Override\r
+       public int getMinSize() {\r
+               return 1;\r
+       }\r
+       \r
+}\r
+\r