X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=bundles%2Forg.simantics.databoard%2Fsrc%2Forg%2Fsimantics%2Fdataboard%2Fserialization%2FSerializer.java;h=31578eaa626c4170d890945cefe71c7af46795c3;hb=refs%2Fchanges%2F38%2F238%2F2;hp=70829699bc2dce5b42d773c142a03537d940fc34;hpb=24e2b34260f219f0d1644ca7a138894980e25b14;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.databoard/src/org/simantics/databoard/serialization/Serializer.java b/bundles/org.simantics.databoard/src/org/simantics/databoard/serialization/Serializer.java index 70829699b..31578eaa6 100644 --- a/bundles/org.simantics.databoard/src/org/simantics/databoard/serialization/Serializer.java +++ b/bundles/org.simantics.databoard/src/org/simantics/databoard/serialization/Serializer.java @@ -1,41 +1,41 @@ -/******************************************************************************* - * 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 - *******************************************************************************/ +/******************************************************************************* + * 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; - -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; +import gnu.trove.map.hash.TObjectIntHashMap; -/** - * Databoard binary serializer. - * - * @author Toni Kalajainen +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 */ 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 + * Typically an empty map is provided. If the type has no recursion, i.e. Referable * Records, a null value can be provided. * * @param out @@ -54,27 +54,27 @@ public abstract class Serializer { */ public abstract void serialize(DataOutput out, TObjectIntHashMap identities, Object obj) throws IOException; public abstract void serialize(DataOutput out, Object obj) throws IOException; - public void serialize(OutputStream out, Object obj) throws IOException - { - OutputStreamWriteable writ = new OutputStreamWriteable(out); - serialize(writ, obj); - } + 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 - * Records, a null value can be provided.

- * - * 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 + * Typically an empty list is provided. If the type has no recursion, i.e. Referable + * Records, a null value can be provided.

+ * + * 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 identities) throws IOException; public abstract Object deserialize(DataInput in) throws IOException; - - - /** - * Deserialize into an existing instance. This method writes over previous values. - * - * @param in - * @param identities - * @param dst valid object - * @throws IOException + + + /** + * 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 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 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 Object deserializeToTry(DataInput in, List identities, Object dst) throws IOException - { - deserializeTo(in, identities, dst); - return dst; - } + public Object deserializeToTry(DataInput in, List 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, + * 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 + // InputStreamReadable adapts InputStream to BinaryReadable&DataInput InputStreamReadable read = new InputStreamReadable(in, Long.MAX_VALUE); return deserialize(read); - } - - /** - * 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(); - } + } + + /** + * 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(); + } } - /** + /** * 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); - } - - /** - * 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); + } + + /** + * 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(); - /** + /** * 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 identities) throws IOException; public abstract int getSize(Object obj) throws IOException; - - public abstract int getMinSize(); + + 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 - { + { // OutputStreamWriteable adapts OutputStream to DataOutput&BinaryWritable OutputStreamWriteable writ = new OutputStreamWriteable(out); TObjectIntHashMap identities = new TObjectIntHashMap(); serialize(writ, identities, obj); - } - - /** - * 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 identities = new TObjectIntHashMap(); - BinaryFile writable = new BinaryFile( file ); - try { - serialize(writable, identities, obj); - } finally { - writable.close(); - } + } + + /** + * 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 identities = new TObjectIntHashMap(); + 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 + // 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 { - - /** - * Finalize the construction of the serializer. This is called once all component - * serializers are constructed. - */ - public abstract void finalizeConstruction(); + 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 identities = new TObjectIntHashMap(0); @@ -348,10 +348,10 @@ public abstract class Serializer { List identities = new ArrayList(0); return deserialize(in, identities); } - public void deserializeTo(DataInput in, Object obj) throws IOException { - List identities = new ArrayList(0); - deserializeTo(in, identities, obj); - } + public void deserializeTo(DataInput in, Object obj) throws IOException { + List identities = new ArrayList(0); + deserializeTo(in, identities, obj); + } @Override public void skip(DataInput in) throws IOException { List identities = new ArrayList(0); @@ -374,9 +374,9 @@ public abstract class Serializer { } public Object deserialize(DataInput in, List identities) throws IOException { return deserialize(in); - } - public void deserializeTo(DataInput in, List identities, Object obj) throws IOException { - deserializeTo(in, obj); + } + public void deserializeTo(DataInput in, List identities, Object obj) throws IOException { + deserializeTo(in, obj); } @Override public void skip(DataInput in, List identities) throws IOException { @@ -395,26 +395,26 @@ public abstract class Serializer { boolean recursive; protected CompositeSerializer(boolean recursive) { this.recursive = recursive; - } - - /** - * Finalize the construction of the serializer. This is called once all component - * serializers are constructed. - */ - public abstract void finalizeConstruction( ); + } + + /** + * 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 identities = recursive ? new TObjectIntHashMap(0) : null; serialize(out, identities, obj); } - public Object deserialize(DataInput in) throws IOException { - List identities = recursive ? new ArrayList(0) : null; - return deserialize(in, identities); - } - public void deserializeTo(DataInput in, Object obj) throws IOException { - List identities = recursive ? new ArrayList(0) : null; - deserializeTo(in, identities, obj); - } + public Object deserialize(DataInput in) throws IOException { + List identities = recursive ? new ArrayList(0) : null; + return deserialize(in, identities); + } + public void deserializeTo(DataInput in, Object obj) throws IOException { + List identities = recursive ? new ArrayList(0) : null; + deserializeTo(in, identities, obj); + } @Override public void skip(DataInput in) throws IOException { List identities = recursive ? new ArrayList(0) : null; @@ -427,22 +427,22 @@ public abstract class Serializer { return getSize(obj, identities); } } - - /** - * 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?)"); - } + + /** + * 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?)"); + } }