]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.databoard/src/org/simantics/databoard/serialization/Serializer.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / serialization / Serializer.java
index 70829699bc2dce5b42d773c142a03537d940fc34..31578eaa626c4170d890945cefe71c7af46795c3 100644 (file)
@@ -1,41 +1,41 @@
-/*******************************************************************************\r
- *  Copyright (c) 2010 Association for Decentralized Information Management in\r
- *  Industry THTH ry.\r
- *  All rights reserved. This program and the accompanying materials\r
- *  are made available under the terms of the Eclipse Public License v1.0\r
- *  which accompanies this distribution, and is available at\r
- *  http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- *  Contributors:\r
- *      VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
+/*******************************************************************************
+ *  Copyright (c) 2010 Association for Decentralized Information Management in
+ *  Industry THTH ry.
+ *  All rights reserved. This program and the accompanying materials
+ *  are made available under the terms of the Eclipse Public License v1.0
+ *  which accompanies this distribution, and is available at
+ *  http://www.eclipse.org/legal/epl-v10.html
+ *
+ *  Contributors:
+ *      VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
 package org.simantics.databoard.serialization;
 
-import gnu.trove.map.hash.TObjectIntHashMap;\r
-\r
-import java.io.ByteArrayInputStream;\r
-import java.io.DataInput;\r
-import java.io.DataOutput;\r
-import java.io.File;\r
-import java.io.IOException;\r
-import java.io.InputStream;\r
-import java.io.OutputStream;\r
-import java.nio.ByteBuffer;\r
-import java.util.ArrayList;\r
-import java.util.List;\r
-\r
-import org.simantics.databoard.Files;\r
-import org.simantics.databoard.util.binary.BinaryFile;\r
-import org.simantics.databoard.util.binary.BinaryReadable;\r
-import org.simantics.databoard.util.binary.ByteBufferReadable;\r
-import org.simantics.databoard.util.binary.ByteBufferWriteable;\r
-import org.simantics.databoard.util.binary.InputStreamReadable;\r
-import org.simantics.databoard.util.binary.OutputStreamWriteable;\r
+import gnu.trove.map.hash.TObjectIntHashMap;
 
-/**\r
- * Databoard binary serializer. \r
- *\r
- * @author Toni Kalajainen <toni.kalajainen@vtt.fi>\r
+import java.io.ByteArrayInputStream;
+import java.io.DataInput;
+import java.io.DataOutput;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.simantics.databoard.Files;
+import org.simantics.databoard.util.binary.BinaryFile;
+import org.simantics.databoard.util.binary.BinaryReadable;
+import org.simantics.databoard.util.binary.ByteBufferReadable;
+import org.simantics.databoard.util.binary.ByteBufferWriteable;
+import org.simantics.databoard.util.binary.InputStreamReadable;
+import org.simantics.databoard.util.binary.OutputStreamWriteable;
+
+/**
+ * Databoard binary serializer. 
+ *
+ * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
  */
 public abstract class Serializer {
 
@@ -44,7 +44,7 @@ public abstract class Serializer {
         * 
         * The identities argument is a map of identities (in the binary block) and objects
         * that have already been serialized. Once serialized, an object is added to the map.
-        * Typically an empty map is provided. If the type has no recursion, i.e. Referable \r
+        * Typically an empty map is provided. If the type has no recursion, i.e. Referable 
         * Records, a <code>null</code> value can be provided.  
         * 
         * @param out
@@ -54,27 +54,27 @@ public abstract class Serializer {
         */
        public abstract void serialize(DataOutput out, TObjectIntHashMap<Object> identities, Object obj) throws IOException;
        public abstract void serialize(DataOutput out, Object obj) throws IOException;
-       public void serialize(OutputStream out, Object obj) throws IOException\r
-       {\r
-               OutputStreamWriteable writ = new OutputStreamWriteable(out);\r
-               serialize(writ, obj);           \r
-       }\r
+       public void serialize(OutputStream out, Object obj) throws IOException
+       {
+               OutputStreamWriteable writ = new OutputStreamWriteable(out);
+               serialize(writ, obj);           
+       }
        
        /**
         * Deserialize an object from a readable.
         * 
         * The identities argument is a list of identities (in the binary block) of objects
         * that have already been deserialized. Once deserialized they are added to the list.
-        * Typically an empty list is provided. If the type has no recursion, i.e. Referable\r
-        * Records, a <code>null</code> value can be provided.<p>\r
-        * \r
-        * Note, if in argument is instanceof BinaryReadable or RandomAccessBinary, \r
-        * the serializer performs extra protection against malformed data when \r
-        * deserializing arrays and maps. This prevents the serializer from \r
-        * instanting potentially out-of-memory-invoking huge arrays. For example, \r
-        * if data data says array size is 0xffffffff (-1), 4GB is allocated -> \r
-        * out of memory exception -> unhandled runtime error. BinaryReadable has \r
-        * length limit which allowes serializer to estimate whether future data is \r
+        * Typically an empty list is provided. If the type has no recursion, i.e. Referable
+        * Records, a <code>null</code> value can be provided.<p>
+        * 
+        * Note, if in argument is instanceof BinaryReadable or RandomAccessBinary, 
+        * the serializer performs extra protection against malformed data when 
+        * deserializing arrays and maps. This prevents the serializer from 
+        * instanting potentially out-of-memory-invoking huge arrays. For example, 
+        * if data data says array size is 0xffffffff (-1), 4GB is allocated -> 
+        * out of memory exception -> unhandled runtime error. BinaryReadable has 
+        * length limit which allowes serializer to estimate whether future data is 
         * readable.  
         * 
         * @param in DataInput, BinaryReadable or RandomAccessBinary
@@ -84,38 +84,38 @@ public abstract class Serializer {
         */
        public abstract Object deserialize(DataInput in, List<Object> identities) throws IOException;
        public abstract Object deserialize(DataInput in) throws IOException;
-\r
-       \r
-       /**\r
-        * Deserialize into an existing instance. This method writes over previous values.\r
-        * \r
-        * @param in\r
-        * @param identities\r
-        * @param dst valid object\r
-        * @throws IOException\r
+
+       
+       /**
+        * Deserialize into an existing instance. This method writes over previous values.
+        * 
+        * @param in
+        * @param identities
+        * @param dst valid object
+        * @throws IOException
+        */
+       public abstract void deserializeTo(DataInput in, List<Object> identities, Object dst) throws IOException;
+       public abstract void deserializeTo(DataInput in, Object dst) throws IOException;
+       
+       /**
+        * Attempt deserialize to existing instance. Creates new if not possible.  
+        * 
+        * @param in
+        * @param identities
+        * @param dst
+        * @return dst or new obj
+        * @throws IOException
         */
-       public abstract void deserializeTo(DataInput in, List<Object> identities, Object dst) throws IOException;\r
-       public abstract void deserializeTo(DataInput in, Object dst) throws IOException;\r
-       \r
-       /**\r
-        * Attempt deserialize to existing instance. Creates new if not possible.  \r
-        * \r
-        * @param in\r
-        * @param identities\r
-        * @param dst\r
-        * @return dst or new obj\r
-        * @throws IOException\r
-        */\r
-       public Object deserializeToTry(DataInput in, List<Object> identities, Object dst) throws IOException \r
-       {\r
-               deserializeTo(in, identities, dst);\r
-               return dst;                             \r
-       }\r
+       public Object deserializeToTry(DataInput in, List<Object> identities, Object dst) throws IOException 
+       {
+               deserializeTo(in, identities, dst);
+               return dst;                             
+       }
        
        /**
         * Deserialize the next object in an input stream.
         * Note, if multiple objects are deserialized from the same stream, it is 
-        * more efficient to instantiate InputStreamReadable and identities only once,\r
+        * more efficient to instantiate InputStreamReadable and identities only once,
         * and use {@link #deserialize(DataInput, List)}.
         * 
         * @param in
@@ -124,63 +124,63 @@ public abstract class Serializer {
         */
        public Object deserialize(InputStream in) throws IOException
        {
-               // InputStreamReadable adapts InputStream to BinaryReadable&DataInput\r
+               // InputStreamReadable adapts InputStream to BinaryReadable&DataInput
                InputStreamReadable read = new InputStreamReadable(in, Long.MAX_VALUE);
                return deserialize(read);
-       }\r
-       \r
-       /**\r
-        * Deserialize from an input stream into an object.\r
-        * Note, if multiple objects are deserialized from the same stream, it is \r
-        * more efficient to instantiate InputStreamReadable and identities only once,\r
-        * and use {@link #deserialize(DataInput, List)}.\r
-        * \r
-        * @param in\r
-        * @param obj a valid object\r
-        * @throws IOException\r
-        */\r
-       public void deserialize(InputStream in, Object obj) throws IOException\r
-       {\r
-               // InputStreamReadable adapts InputStream to BinaryReadable&DataInput\r
-               InputStreamReadable read = new InputStreamReadable(in, Long.MAX_VALUE);\r
-               deserializeTo(read, obj);\r
-       }       \r
-       \r
-       /**\r
-        * Deserialize object from a file.  \r
-        * \r
-        * @param file source file\r
-        * @return the object\r
-        * @throws IOException\r
-        */\r
-       public Object deserialize(File file) throws IOException\r
-       {\r
-               BinaryFile f = new BinaryFile(file);\r
-               try {\r
-                       return deserialize(f);\r
-               } finally {\r
-                       f.close();\r
-               }               \r
-       }\r
-       \r
-       /**\r
-        * Deserialize a file into a valid object. This method writes over previous values.\r
-        * \r
-        * @param file source file\r
-        * @param obj a dst valid object\r
-        * @throws IOException\r
-        */\r
-       public void deserialize(File file, Object obj) throws IOException\r
-       {\r
-               BinaryFile f = new BinaryFile(file);\r
-               try {\r
-                       deserializeTo(f, obj);\r
-               } finally {\r
-                       f.close();\r
-               }               \r
+       }
+       
+       /**
+        * Deserialize from an input stream into an object.
+        * Note, if multiple objects are deserialized from the same stream, it is 
+        * more efficient to instantiate InputStreamReadable and identities only once,
+        * and use {@link #deserialize(DataInput, List)}.
+        * 
+        * @param in
+        * @param obj a valid object
+        * @throws IOException
+        */
+       public void deserialize(InputStream in, Object obj) throws IOException
+       {
+               // InputStreamReadable adapts InputStream to BinaryReadable&DataInput
+               InputStreamReadable read = new InputStreamReadable(in, Long.MAX_VALUE);
+               deserializeTo(read, obj);
+       }       
+       
+       /**
+        * Deserialize object from a file.  
+        * 
+        * @param file source file
+        * @return the object
+        * @throws IOException
+        */
+       public Object deserialize(File file) throws IOException
+       {
+               BinaryFile f = new BinaryFile(file);
+               try {
+                       return deserialize(f);
+               } finally {
+                       f.close();
+               }               
+       }
+       
+       /**
+        * Deserialize a file into a valid object. This method writes over previous values.
+        * 
+        * @param file source file
+        * @param obj a dst valid object
+        * @throws IOException
+        */
+       public void deserialize(File file, Object obj) throws IOException
+       {
+               BinaryFile f = new BinaryFile(file);
+               try {
+                       deserializeTo(f, obj);
+               } finally {
+                       f.close();
+               }               
        }       
        
-       /**\r
+       /**
         * Deserialize an object in byte[] format.
         * 
         * @param data
@@ -192,20 +192,20 @@ public abstract class Serializer {
                ByteBuffer buffer = ByteBuffer.wrap( data );
                ByteBufferReadable readable = new ByteBufferReadable( buffer );
                return deserialize(readable);
-       }\r
-       \r
-       /**\r
-        * Deserialize byte[] into a valid object.\r
-        * \r
-        * @param data\r
-        * @param obj dst valid object\r
-        * @throws IOException\r
-        */\r
-       public void deserialize(byte[] data, Object obj) throws IOException\r
-       {\r
-               ByteBuffer buffer = ByteBuffer.wrap( data );\r
-               ByteBufferReadable readable = new ByteBufferReadable( buffer );\r
-               deserializeTo(readable, obj);\r
+       }
+       
+       /**
+        * Deserialize byte[] into a valid object.
+        * 
+        * @param data
+        * @param obj dst valid object
+        * @throws IOException
+        */
+       public void deserialize(byte[] data, Object obj) throws IOException
+       {
+               ByteBuffer buffer = ByteBuffer.wrap( data );
+               ByteBufferReadable readable = new ByteBufferReadable( buffer );
+               deserializeTo(readable, obj);
        }       
 
        /**
@@ -239,7 +239,7 @@ public abstract class Serializer {
         */
        public abstract Integer getConstantSize();
        
-       /**\r
+       /**
         * Get the number of bytes required to serialize an object
         * 
         * @param obj
@@ -249,8 +249,8 @@ public abstract class Serializer {
         */
        public abstract int getSize(Object obj, TObjectIntHashMap<Object> identities) throws IOException;
        public abstract int getSize(Object obj) throws IOException;
-\r
-       public abstract int getMinSize();\r
+
+       public abstract int getMinSize();
        
        /**
         * Serializes an object to a byte[].
@@ -281,37 +281,37 @@ public abstract class Serializer {
         * @throws IOException
         */
        public void serialize(Object obj, OutputStream out) throws IOException
-       {\r
+       {
                // OutputStreamWriteable adapts OutputStream to DataOutput&BinaryWritable
                OutputStreamWriteable writ = new OutputStreamWriteable(out);
                TObjectIntHashMap<Object> identities = new TObjectIntHashMap<Object>();
                serialize(writ, identities, obj);
-       }\r
-       \r
-       /**\r
-        * Serialize an object to a file. Note the type info is not written to the\r
-        * file (unless obj is variant), and therefore is not compatible as .dbb \r
-        * file. \r
-        * \r
-        * Databoard Binary file (.dbb) is a binary file that has datatype in the \r
-        * header. To create .dbb file, serialize Datatype and then the value.\r
-        * Or use methods in {@link Files} for convenience. Variant objects are, by\r
-        * nature, .dbb compatible.   \r
-        * \r
-        * @param obj\r
-        * @param file\r
-        * @throws IOException\r
-        */\r
-       public void serialize(Object obj, File file)\r
-       throws IOException\r
-       {\r
-               TObjectIntHashMap<Object> identities = new TObjectIntHashMap<Object>();\r
-               BinaryFile writable = new BinaryFile( file );\r
-               try {\r
-                       serialize(writable, identities, obj);\r
-               } finally {\r
-                       writable.close();\r
-               }\r
+       }
+       
+       /**
+        * Serialize an object to a file. Note the type info is not written to the
+        * file (unless obj is variant), and therefore is not compatible as .dbb 
+        * file. 
+        * 
+        * Databoard Binary file (.dbb) is a binary file that has datatype in the 
+        * header. To create .dbb file, serialize Datatype and then the value.
+        * Or use methods in {@link Files} for convenience. Variant objects are, by
+        * nature, .dbb compatible.   
+        * 
+        * @param obj
+        * @param file
+        * @throws IOException
+        */
+       public void serialize(Object obj, File file)
+       throws IOException
+       {
+               TObjectIntHashMap<Object> identities = new TObjectIntHashMap<Object>();
+               BinaryFile writable = new BinaryFile( file );
+               try {
+                       serialize(writable, identities, obj);
+               } finally {
+                       writable.close();
+               }
        }       
 
        /**
@@ -323,7 +323,7 @@ public abstract class Serializer {
         */
        public InputStream getInputStream(Object obj) throws IOException
        {
-               // Trivial implementation - better implementation would code bytes on-demend\r
+               // Trivial implementation - better implementation would code bytes on-demend
                // without memory consumption.
                byte[] data = serialize(obj);
                return new ByteArrayInputStream(data);
@@ -332,13 +332,13 @@ public abstract class Serializer {
        /**
         * Serializer for data types that have referable objects
         */
-       public static abstract class RecursiveSerializer extends Serializer {\r
-               \r
-               /**\r
-                * Finalize the construction of the serializer. This is called once all component\r
-                * serializers are constructed.\r
-                */\r
-               public abstract void finalizeConstruction();\r
+       public static abstract class RecursiveSerializer extends Serializer {
+               
+               /**
+                * Finalize the construction of the serializer. This is called once all component
+                * serializers are constructed.
+                */
+               public abstract void finalizeConstruction();
                
                public void serialize(DataOutput out, Object obj) throws IOException {
                        TObjectIntHashMap<Object> identities = new TObjectIntHashMap<Object>(0);
@@ -348,10 +348,10 @@ public abstract class Serializer {
                        List<Object> identities = new ArrayList<Object>(0);
                        return deserialize(in, identities);
                }
-               public void deserializeTo(DataInput in, Object obj) throws IOException {\r
-                       List<Object> identities = new ArrayList<Object>(0);\r
-                       deserializeTo(in, identities, obj);\r
-               }\r
+               public void deserializeTo(DataInput in, Object obj) throws IOException {
+                       List<Object> identities = new ArrayList<Object>(0);
+                       deserializeTo(in, identities, obj);
+               }
                @Override
                public void skip(DataInput in) throws IOException {
                        List<Object> identities = new ArrayList<Object>(0);
@@ -374,9 +374,9 @@ public abstract class Serializer {
                }
                public Object deserialize(DataInput in, List<Object> identities) throws IOException {
                        return deserialize(in);
-               }\r
-               public void deserializeTo(DataInput in, List<Object> identities, Object obj) throws IOException {\r
-                       deserializeTo(in, obj);\r
+               }
+               public void deserializeTo(DataInput in, List<Object> identities, Object obj) throws IOException {
+                       deserializeTo(in, obj);
                }               
                @Override
                public void skip(DataInput in, List<Object> identities) throws IOException {
@@ -395,26 +395,26 @@ public abstract class Serializer {
                boolean recursive;
                protected CompositeSerializer(boolean recursive) {
                        this.recursive = recursive;
-               }\r
-               \r
-               /**\r
-                * Finalize the construction of the serializer. This is called once all component\r
-                * serializers are constructed.\r
-                */\r
-               public abstract void finalizeConstruction( );\r
+               }
+               
+               /**
+                * Finalize the construction of the serializer. This is called once all component
+                * serializers are constructed.
+                */
+               public abstract void finalizeConstruction( );
                
                public void serialize(DataOutput out, Object obj) throws IOException {
                        TObjectIntHashMap<Object> identities = recursive ? new TObjectIntHashMap<Object>(0) : null;
                        serialize(out, identities, obj);
                }
-               public Object deserialize(DataInput in) throws IOException {\r
-                       List<Object> identities = recursive ? new ArrayList<Object>(0) : null;\r
-                       return deserialize(in, identities);\r
-               }\r
-               public void deserializeTo(DataInput in, Object obj) throws IOException {\r
-                       List<Object> identities = recursive ? new ArrayList<Object>(0) : null;\r
-                       deserializeTo(in, identities, obj);\r
-               }\r
+               public Object deserialize(DataInput in) throws IOException {
+                       List<Object> identities = recursive ? new ArrayList<Object>(0) : null;
+                       return deserialize(in, identities);
+               }
+               public void deserializeTo(DataInput in, Object obj) throws IOException {
+                       List<Object> identities = recursive ? new ArrayList<Object>(0) : null;
+                       deserializeTo(in, identities, obj);
+               }
                @Override
                public void skip(DataInput in) throws IOException {
                        List<Object> identities = recursive ? new ArrayList<Object>(0) : null;
@@ -427,22 +427,22 @@ public abstract class Serializer {
                        return getSize(obj, identities);
                }
        }
-\r
-       /**\r
-        * Assert there are enough readable bytes. This method works only if input \r
-        * object is instance of BinaryReadable. DataInput cannot tell the \r
-        * number of remaining bytes.   \r
-        * \r
-        * This method is used by array, map, record and union sub-classes.\r
-        * \r
-        * @param in\r
-        * @throws IOException\r
-        */\r
-       protected void assertRemainingBytes(DataInput in, long bts) throws IOException {\r
-               if (in instanceof BinaryReadable == false) return;\r
-               BinaryReadable r = (BinaryReadable) in;\r
-               if (bts > r.length() - r.position()) throw new SerializationException("Malformed data. Serialization aborted. (Wrong binding?)");\r
-       }\r
+
+       /**
+        * Assert there are enough readable bytes. This method works only if input 
+        * object is instance of BinaryReadable. DataInput cannot tell the 
+        * number of remaining bytes.   
+        * 
+        * This method is used by array, map, record and union sub-classes.
+        * 
+        * @param in
+        * @throws IOException
+        */
+       protected void assertRemainingBytes(DataInput in, long bts) throws IOException {
+               if (in instanceof BinaryReadable == false) return;
+               BinaryReadable r = (BinaryReadable) in;
+               if (bts > r.length() - r.position()) throw new SerializationException("Malformed data. Serialization aborted. (Wrong binding?)");
+       }
        
 }