]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Fixed databoard Files.writeFile to not open file twice for writing 36/236/1
authorTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Thu, 29 Dec 2016 09:05:07 +0000 (11:05 +0200)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Thu, 29 Dec 2016 09:05:07 +0000 (11:05 +0200)
refs #6902

Change-Id: I543afb745922391f7a9a9ca8150f24cd3092b359

bundles/org.simantics.databoard/src/org/simantics/databoard/Files.java

index 16dbd1e00f493cf783b4fd0c34043d69f94389a4..ae4f46f31634cf091fc0977db09630abaa9f62d0 100644 (file)
-/*******************************************************************************\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
-package org.simantics.databoard;\r
-\r
-import java.io.DataInput;\r
-import java.io.DataOutput;\r
-import java.io.File;\r
-import java.io.FileInputStream;\r
-import java.io.FileOutputStream;\r
-import java.io.IOException;\r
-import java.io.InputStream;\r
-import java.io.OutputStream;\r
-import java.io.OutputStreamWriter;\r
-import java.nio.ByteBuffer;\r
-\r
-import org.simantics.databoard.accessor.error.AccessorException;\r
-import org.simantics.databoard.adapter.AdaptException;\r
-import org.simantics.databoard.adapter.Adapter;\r
-import org.simantics.databoard.adapter.AdapterConstructionException;\r
-import org.simantics.databoard.binding.Binding;\r
-import org.simantics.databoard.binding.RecordBinding;\r
-import org.simantics.databoard.binding.error.BindingException;\r
-import org.simantics.databoard.binding.mutable.MutableVariant;\r
-import org.simantics.databoard.parser.repository.DataTypeSyntaxError;\r
-import org.simantics.databoard.parser.repository.DataValueRepository;\r
-import org.simantics.databoard.serialization.RuntimeSerializerConstructionException;\r
-import org.simantics.databoard.serialization.Serializer;\r
-import org.simantics.databoard.serialization.SerializerConstructionException;\r
-import org.simantics.databoard.type.Datatype;\r
-import org.simantics.databoard.util.StreamUtil;\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 org.simantics.databoard.util.binary.UTF8;\r
-\r
-public class Files {\r
-       \r
-       /**\r
-        * Create a text file (.dbv) with a value. The file is UTF-8 encoded.  \r
-        * If old file exists, it is deleted.\r
-        * \r
-        * @param file\r
-        * @param binding\r
-        * @param value\r
-        * @throws IOException \r
-        */\r
-       public static void createTextFile(File file, Binding binding, Object value) \r
-       throws IOException\r
-       {\r
-               try {\r
-                       if (file.exists()) file.delete();\r
-                       file.createNewFile();\r
-                       \r
-                       String txt = binding.printValueDefinition(value, false);\r
-                       FileOutputStream fos = new FileOutputStream(file, false);\r
-                       try {\r
-                               OutputStreamWriter os = new OutputStreamWriter(fos, UTF8.CHARSET);\r
-                               os.append(txt);\r
-                               os.flush();\r
-                               os.close();\r
-                       } finally {\r
-                               fos.close();\r
-                       }\r
-               } catch(BindingException e) {\r
-                       throw new IOException(e);\r
-               }\r
-       }\r
-       \r
-       /**\r
-        * Create a binary file (.dbb) with a initial value. Binary file is a variant, \r
-        * there is a filetype in the header of the file.\r
-        * If old file exists, it is deleted.\r
-        * \r
-        * @param file\r
-        * @param binding\r
-        * @param value\r
-        * @throws IOException \r
-        */\r
-       public static void createFile(File file, Binding binding, Object value) \r
-       throws IOException \r
-       {\r
-               if (file.exists()) file.delete();\r
-               file.createNewFile();\r
-               MutableVariant v = new MutableVariant(binding, value);\r
-               Serializer s = Bindings.getSerializerUnchecked( Bindings.MUTABLE_VARIANT );\r
-               s.serialize(v, file);\r
-       }\r
-\r
-       /**\r
-        * Create a new binary file (.dbb) with an empty value. Binary file is a variant, \r
-        * there is a filetype in the header of the file.\r
-        * If old file exists, it is deleted.\r
-        * \r
-        * @since 0.5\r
-        * @param file\r
-        * @throws IOException \r
-        * @throws RuntimeSerializerConstructionException\r
-        */\r
-       public static void createFile(File file) throws IOException, RuntimeSerializerConstructionException  \r
-       {\r
-               if (file.exists()) file.delete();\r
-               file.createNewFile();\r
-               BinaryFile bf = new BinaryFile(file);\r
-               try {\r
-                       Binding binding = Bindings.MUTABLE_VARIANT;\r
-                       Object value = new MutableVariant(); \r
-                       Serializer s = Bindings.getSerializerUnchecked( binding );\r
-                       s.serialize(bf, value);\r
-               } finally {\r
-                       bf.close();\r
-               }\r
-       }       \r
-\r
-       /**\r
-        * Create a binary file (.dbb) with empty value of given type. Binary file is a variant, \r
-        * there is a filetype in the header of the file.\r
-        * If old file exists, it is deleted.\r
-        * \r
-        * @param file\r
-        * @param type\r
-        * @throws IOException \r
-        * @throws RuntimeSerializerConstructionException \r
-        */\r
-       public static void createFile(File file, Datatype type) throws IOException, RuntimeSerializerConstructionException \r
-       {\r
-               if (file.exists()) file.delete();\r
-               file.createNewFile();\r
-               BinaryFile bf = new BinaryFile(file);\r
-               try {\r
-                       Binding binding = Bindings.MUTABLE_VARIANT;\r
-                       Object value = binding.createDefault();\r
-                       Serializer s = Bindings.getSerializer( binding );\r
-                       s.serialize(bf, value);\r
-               } catch (BindingException e) {\r
-                       throw new IOException(e);\r
-               } catch (SerializerConstructionException e) {\r
-                       throw new IOException(e);\r
-               } finally {\r
-                       bf.close();\r
-               }\r
-       }\r
-       \r
-       \r
-       /**\r
-        * Read a text file (.dbv).   \r
-        * \r
-        * @param file\r
-        * @param binding\r
-        * @return value\r
-        * @throws IOException \r
-        * @throws BindingException \r
-        * @throws DataTypeSyntaxError \r
-        */\r
-       public static Object readTextFile(File file, Binding binding) \r
-       throws IOException, DataTypeSyntaxError, BindingException \r
-       {\r
-               FileInputStream fis = new FileInputStream(file);\r
-               try {\r
-                       byte[] data = StreamUtil.readFully(fis);\r
-                       String txt = new String(data, UTF8.CHARSET);\r
-                       DataValueRepository repo = new DataValueRepository();\r
-                       repo.setTypeRepository( Datatypes.datatypeRepository );\r
-                       return binding.parseValue(txt, repo);\r
-               } finally {\r
-                       fis.close();\r
-               }\r
-       }\r
-       \r
-       /**\r
-        * Read file type of a binary file. \r
-        * \r
-        * @param file\r
-        * @return datatype\r
-        * @throws IOException \r
-        */\r
-       public static Datatype readFileType(File file) throws IOException {\r
-               BinaryFile rf = new BinaryFile( file );\r
-               try {\r
-                       Binding datatype_binding = Bindings.getBindingUnchecked( Datatype.class );\r
-                       return (Datatype) Bindings.getSerializerUnchecked( datatype_binding ).deserialize( rf );\r
-               } finally {\r
-                       rf.close();\r
-               }               \r
-       }\r
-       \r
-       /**\r
-        * Read a binary file into a java instance. Binary file is a variant, \r
-        * there is a filetype in the header of the file. \r
-        * If requested binding is not the exact binding of the file, an adapter is tried.\r
-        * \r
-        * @param file file\r
-        * @param binding content binding\r
-        * @return instance\r
-        * @throws IOException \r
-        */\r
-       public static Object readFile(File file, Binding binding) throws IOException {\r
-               BinaryFile rf = new BinaryFile( file );\r
-               try {\r
-                       Binding datatype_binding = Bindings.getBindingUnchecked( Datatype.class );\r
-                       Datatype type = (Datatype) Bindings.getSerializerUnchecked( datatype_binding ).deserialize( rf );\r
-                       \r
-                       if (type.equals(binding.type())) {\r
-                               return Bindings.getSerializerUnchecked( binding ).deserialize(rf);\r
-                       } else {\r
-                               try {\r
-                                       Binding fileContentBinding = Bindings.getMutableBinding(type);\r
-                                       Adapter adapter = Bindings.getAdapter(fileContentBinding, binding);\r
-                                       Object value = Bindings.getSerializerUnchecked( fileContentBinding ).deserialize(rf);\r
-                                       return adapter.adapt( value );\r
-                               } catch (AdapterConstructionException e) {\r
-                                       throw new IOException(e);\r
-                               } catch (AdaptException e) {\r
-                                       throw new IOException(e);\r
-                               }\r
-                       }\r
-               } finally {\r
-                       rf.close();\r
-               }\r
-       }\r
-\r
-       /**\r
-        * Read a file to an object.\r
-        * \r
-        * @param file\r
-        * @param binding\r
-        * @param dst\r
-        * @throws IOException \r
-        */\r
-       public static void readFile(File file, RecordBinding binding, Object dst) throws IOException {\r
-               BinaryFile rf = new BinaryFile( file );\r
-               try {\r
-                       Binding datatype_binding = Bindings.getBindingUnchecked( Datatype.class );\r
-                       Datatype type = (Datatype) Bindings.getSerializerUnchecked( datatype_binding ).deserialize( rf );\r
-                       \r
-                       if (type.equals(binding.type())) {\r
-                               Serializer s = Bindings.getSerializerUnchecked( binding ); \r
-                               s.deserializeTo(rf, dst);\r
-                       } else {\r
-                               try {\r
-                                       Binding fileContentBinding = Bindings.getMutableBinding(type);\r
-                                       Serializer s = Bindings.getSerializerUnchecked( fileContentBinding );\r
-                                       Object tmpObj = s.deserialize( rf );                                    \r
-                                       binding.readFrom(fileContentBinding, tmpObj, dst);\r
-                               } catch (BindingException e) {\r
-                                       throw new IOException(e);\r
-                               }\r
-                       }\r
-               } finally {\r
-                       rf.close();\r
-               }\r
-       }\r
-       \r
-       /**\r
-        * Read input stream into a java instance. Binary file is a variant, \r
-        * there is a filetype in the header of the file. If requested binding is not the \r
-        * exact binding of the file, an adapter is tried.<p>\r
-        * \r
-        * The implementation reads the inputstream fully into memory.<p>\r
-        * \r
-        * @param is input stream\r
-        * @param binding content binding\r
-        * @return instance\r
-        * @throws IOException \r
-        */\r
-       public static Object readFile(InputStream is, Binding binding) throws IOException {\r
-               BinaryReadable readable = InputStreamReadable.readFully( is );\r
-               Binding datatype_binding = Bindings.getBindingUnchecked( Datatype.class );\r
-               Datatype type = (Datatype) Bindings.getSerializerUnchecked( datatype_binding ).deserialize( readable );\r
-                       \r
-               if (!type.equals(binding.type())) {\r
-                       try {\r
-                               Binding fileContentBinding = Bindings.getMutableBinding(type);\r
-                               Adapter adapter = Bindings.getAdapter(fileContentBinding, binding);\r
-                               Object value = Bindings.getSerializerUnchecked( fileContentBinding ).deserialize( readable );\r
-                               return adapter.adapt( value );\r
-                       } catch (AdapterConstructionException e) {\r
-                               throw new IOException(e);\r
-                       } catch (AdaptException e) {\r
-                               throw new IOException(e);\r
-                       }\r
-               }\r
-                       \r
-               return Bindings.getSerializerUnchecked( binding ).deserialize( readable );\r
-       }\r
-       \r
-       /**\r
-        * Read input stream into a java instance. If requested binding is not the \r
-        * exact binding of the file, an adapter is tried.\r
-        * \r
-        * @param is input stream\r
-        * @param streamLength\r
-        * @param binding content binding\r
-        * @return instance\r
-        * @throws IOException \r
-        * @throws AccessorException \r
-        */\r
-       public static Object readFile(InputStream is, long streamLength, Binding binding) throws IOException {\r
-               BinaryReadable readable = new InputStreamReadable( is, streamLength );\r
-               Binding datatype_binding = Bindings.getBindingUnchecked( Datatype.class );\r
-               Datatype type = (Datatype) Bindings.getSerializerUnchecked( datatype_binding ).deserialize( readable );\r
-                       \r
-               if (!type.equals(binding.type())) {\r
-                       try {\r
-                               Binding fileContentBinding = Bindings.getMutableBinding(type);\r
-                               Adapter adapter = Bindings.getAdapter(fileContentBinding, binding);\r
-                               Object value = Bindings.getSerializerUnchecked( fileContentBinding ).deserialize( readable );\r
-                               return adapter.adapt( value );\r
-                       } catch (AdapterConstructionException e) {\r
-                               throw new IOException(e);\r
-                       } catch (AdaptException e) {\r
-                               throw new IOException(e);\r
-                       }\r
-               }\r
-                       \r
-               return Bindings.getSerializerUnchecked( binding ).deserialize( readable );\r
-       }\r
-\r
-       /**\r
-        * Write value as binary file (.dbb).\r
-        * \r
-        * @param file file\r
-        * @param binding content binding\r
-        * @param value value\r
-        * @throws IOException \r
-        */\r
-       public static void writeFile(File file, Binding binding, Object value) throws IOException {\r
-               BinaryFile rf = new BinaryFile( file );\r
-               try {\r
-                       MutableVariant v = new MutableVariant(binding, value);\r
-                       Serializer s = Bindings.getSerializerUnchecked( Bindings.MUTABLE_VARIANT );\r
-                       s.serialize(v, file);\r
-               } finally {\r
-                       rf.close();\r
-               }\r
-       }       \r
-\r
-       public static DataInput openInput( InputStream is )\r
-       {\r
-               return new InputStreamReadable(is, Long.MAX_VALUE);\r
-       }\r
-       \r
-       public static DataInput openInput( File file ) throws IOException\r
-       {\r
-               return new BinaryFile(file);\r
-       }\r
-       \r
-       public static DataInput openInput( byte[] data ) \r
-       {\r
-               ByteBuffer buffer = ByteBuffer.wrap( data );\r
-               return new ByteBufferReadable( buffer );\r
-       }\r
-       \r
-       public static DataOutput openOutput( OutputStream os )\r
-       {\r
-               return new OutputStreamWriteable( os );\r
-       }\r
-       \r
-       public static DataOutput openOutput( File file ) throws IOException\r
-       {\r
-               return new BinaryFile( file );\r
-       }\r
-       \r
-       public static DataOutput openOutput( byte[] data ) throws IOException\r
-       {\r
-               ByteBuffer buffer = ByteBuffer.wrap( data );\r
-               return new ByteBufferWriteable( buffer );\r
-       }       \r
-\r
-}\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;
+
+import java.io.DataInput;
+import java.io.DataOutput;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.nio.ByteBuffer;
+
+import org.simantics.databoard.accessor.error.AccessorException;
+import org.simantics.databoard.adapter.AdaptException;
+import org.simantics.databoard.adapter.Adapter;
+import org.simantics.databoard.adapter.AdapterConstructionException;
+import org.simantics.databoard.binding.Binding;
+import org.simantics.databoard.binding.RecordBinding;
+import org.simantics.databoard.binding.error.BindingException;
+import org.simantics.databoard.binding.mutable.MutableVariant;
+import org.simantics.databoard.parser.repository.DataTypeSyntaxError;
+import org.simantics.databoard.parser.repository.DataValueRepository;
+import org.simantics.databoard.serialization.RuntimeSerializerConstructionException;
+import org.simantics.databoard.serialization.Serializer;
+import org.simantics.databoard.serialization.SerializerConstructionException;
+import org.simantics.databoard.type.Datatype;
+import org.simantics.databoard.util.StreamUtil;
+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 org.simantics.databoard.util.binary.UTF8;
+
+public class Files {
+       
+       /**
+        * Create a text file (.dbv) with a value. The file is UTF-8 encoded.  
+        * If old file exists, it is deleted.
+        * 
+        * @param file
+        * @param binding
+        * @param value
+        * @throws IOException 
+        */
+       public static void createTextFile(File file, Binding binding, Object value) 
+       throws IOException
+       {
+               try {
+                       if (file.exists()) file.delete();
+                       file.createNewFile();
+                       
+                       String txt = binding.printValueDefinition(value, false);
+                       FileOutputStream fos = new FileOutputStream(file, false);
+                       try {
+                               OutputStreamWriter os = new OutputStreamWriter(fos, UTF8.CHARSET);
+                               os.append(txt);
+                               os.flush();
+                               os.close();
+                       } finally {
+                               fos.close();
+                       }
+               } catch(BindingException e) {
+                       throw new IOException(e);
+               }
+       }
+       
+       /**
+        * Create a binary file (.dbb) with a initial value. Binary file is a variant, 
+        * there is a filetype in the header of the file.
+        * If old file exists, it is deleted.
+        * 
+        * @param file
+        * @param binding
+        * @param value
+        * @throws IOException 
+        */
+       public static void createFile(File file, Binding binding, Object value) 
+       throws IOException 
+       {
+               if (file.exists()) file.delete();
+               file.createNewFile();
+               MutableVariant v = new MutableVariant(binding, value);
+               Serializer s = Bindings.getSerializerUnchecked( Bindings.MUTABLE_VARIANT );
+               s.serialize(v, file);
+       }
+
+       /**
+        * Create a new binary file (.dbb) with an empty value. Binary file is a variant, 
+        * there is a filetype in the header of the file.
+        * If old file exists, it is deleted.
+        * 
+        * @since 0.5
+        * @param file
+        * @throws IOException 
+        * @throws RuntimeSerializerConstructionException
+        */
+       public static void createFile(File file) throws IOException, RuntimeSerializerConstructionException  
+       {
+               if (file.exists()) file.delete();
+               file.createNewFile();
+               BinaryFile bf = new BinaryFile(file);
+               try {
+                       Binding binding = Bindings.MUTABLE_VARIANT;
+                       Object value = new MutableVariant(); 
+                       Serializer s = Bindings.getSerializerUnchecked( binding );
+                       s.serialize(bf, value);
+               } finally {
+                       bf.close();
+               }
+       }       
+
+       /**
+        * Create a binary file (.dbb) with empty value of given type. Binary file is a variant, 
+        * there is a filetype in the header of the file.
+        * If old file exists, it is deleted.
+        * 
+        * @param file
+        * @param type
+        * @throws IOException 
+        * @throws RuntimeSerializerConstructionException 
+        */
+       public static void createFile(File file, Datatype type) throws IOException, RuntimeSerializerConstructionException 
+       {
+               if (file.exists()) file.delete();
+               file.createNewFile();
+               BinaryFile bf = new BinaryFile(file);
+               try {
+                       Binding binding = Bindings.MUTABLE_VARIANT;
+                       Object value = binding.createDefault();
+                       Serializer s = Bindings.getSerializer( binding );
+                       s.serialize(bf, value);
+               } catch (BindingException e) {
+                       throw new IOException(e);
+               } catch (SerializerConstructionException e) {
+                       throw new IOException(e);
+               } finally {
+                       bf.close();
+               }
+       }
+       
+       
+       /**
+        * Read a text file (.dbv).   
+        * 
+        * @param file
+        * @param binding
+        * @return value
+        * @throws IOException 
+        * @throws BindingException 
+        * @throws DataTypeSyntaxError 
+        */
+       public static Object readTextFile(File file, Binding binding) 
+       throws IOException, DataTypeSyntaxError, BindingException 
+       {
+               FileInputStream fis = new FileInputStream(file);
+               try {
+                       byte[] data = StreamUtil.readFully(fis);
+                       String txt = new String(data, UTF8.CHARSET);
+                       DataValueRepository repo = new DataValueRepository();
+                       repo.setTypeRepository( Datatypes.datatypeRepository );
+                       return binding.parseValue(txt, repo);
+               } finally {
+                       fis.close();
+               }
+       }
+       
+       /**
+        * Read file type of a binary file. 
+        * 
+        * @param file
+        * @return datatype
+        * @throws IOException 
+        */
+       public static Datatype readFileType(File file) throws IOException {
+               BinaryFile rf = new BinaryFile( file );
+               try {
+                       Binding datatype_binding = Bindings.getBindingUnchecked( Datatype.class );
+                       return (Datatype) Bindings.getSerializerUnchecked( datatype_binding ).deserialize( rf );
+               } finally {
+                       rf.close();
+               }               
+       }
+       
+       /**
+        * Read a binary file into a java instance. Binary file is a variant, 
+        * there is a filetype in the header of the file. 
+        * If requested binding is not the exact binding of the file, an adapter is tried.
+        * 
+        * @param file file
+        * @param binding content binding
+        * @return instance
+        * @throws IOException 
+        */
+       public static Object readFile(File file, Binding binding) throws IOException {
+               BinaryFile rf = new BinaryFile( file );
+               try {
+                       Binding datatype_binding = Bindings.getBindingUnchecked( Datatype.class );
+                       Datatype type = (Datatype) Bindings.getSerializerUnchecked( datatype_binding ).deserialize( rf );
+                       
+                       if (type.equals(binding.type())) {
+                               return Bindings.getSerializerUnchecked( binding ).deserialize(rf);
+                       } else {
+                               try {
+                                       Binding fileContentBinding = Bindings.getMutableBinding(type);
+                                       Adapter adapter = Bindings.getAdapter(fileContentBinding, binding);
+                                       Object value = Bindings.getSerializerUnchecked( fileContentBinding ).deserialize(rf);
+                                       return adapter.adapt( value );
+                               } catch (AdapterConstructionException e) {
+                                       throw new IOException(e);
+                               } catch (AdaptException e) {
+                                       throw new IOException(e);
+                               }
+                       }
+               } finally {
+                       rf.close();
+               }
+       }
+
+       /**
+        * Read a file to an object.
+        * 
+        * @param file
+        * @param binding
+        * @param dst
+        * @throws IOException 
+        */
+       public static void readFile(File file, RecordBinding binding, Object dst) throws IOException {
+               BinaryFile rf = new BinaryFile( file );
+               try {
+                       Binding datatype_binding = Bindings.getBindingUnchecked( Datatype.class );
+                       Datatype type = (Datatype) Bindings.getSerializerUnchecked( datatype_binding ).deserialize( rf );
+                       
+                       if (type.equals(binding.type())) {
+                               Serializer s = Bindings.getSerializerUnchecked( binding ); 
+                               s.deserializeTo(rf, dst);
+                       } else {
+                               try {
+                                       Binding fileContentBinding = Bindings.getMutableBinding(type);
+                                       Serializer s = Bindings.getSerializerUnchecked( fileContentBinding );
+                                       Object tmpObj = s.deserialize( rf );                                    
+                                       binding.readFrom(fileContentBinding, tmpObj, dst);
+                               } catch (BindingException e) {
+                                       throw new IOException(e);
+                               }
+                       }
+               } finally {
+                       rf.close();
+               }
+       }
+       
+       /**
+        * Read input stream into a java instance. Binary file is a variant, 
+        * there is a filetype in the header of the file. If requested binding is not the 
+        * exact binding of the file, an adapter is tried.<p>
+        * 
+        * The implementation reads the inputstream fully into memory.<p>
+        * 
+        * @param is input stream
+        * @param binding content binding
+        * @return instance
+        * @throws IOException 
+        */
+       public static Object readFile(InputStream is, Binding binding) throws IOException {
+               BinaryReadable readable = InputStreamReadable.readFully( is );
+               Binding datatype_binding = Bindings.getBindingUnchecked( Datatype.class );
+               Datatype type = (Datatype) Bindings.getSerializerUnchecked( datatype_binding ).deserialize( readable );
+                       
+               if (!type.equals(binding.type())) {
+                       try {
+                               Binding fileContentBinding = Bindings.getMutableBinding(type);
+                               Adapter adapter = Bindings.getAdapter(fileContentBinding, binding);
+                               Object value = Bindings.getSerializerUnchecked( fileContentBinding ).deserialize( readable );
+                               return adapter.adapt( value );
+                       } catch (AdapterConstructionException e) {
+                               throw new IOException(e);
+                       } catch (AdaptException e) {
+                               throw new IOException(e);
+                       }
+               }
+                       
+               return Bindings.getSerializerUnchecked( binding ).deserialize( readable );
+       }
+       
+       /**
+        * Read input stream into a java instance. If requested binding is not the 
+        * exact binding of the file, an adapter is tried.
+        * 
+        * @param is input stream
+        * @param streamLength
+        * @param binding content binding
+        * @return instance
+        * @throws IOException 
+        * @throws AccessorException 
+        */
+       public static Object readFile(InputStream is, long streamLength, Binding binding) throws IOException {
+               BinaryReadable readable = new InputStreamReadable( is, streamLength );
+               Binding datatype_binding = Bindings.getBindingUnchecked( Datatype.class );
+               Datatype type = (Datatype) Bindings.getSerializerUnchecked( datatype_binding ).deserialize( readable );
+                       
+               if (!type.equals(binding.type())) {
+                       try {
+                               Binding fileContentBinding = Bindings.getMutableBinding(type);
+                               Adapter adapter = Bindings.getAdapter(fileContentBinding, binding);
+                               Object value = Bindings.getSerializerUnchecked( fileContentBinding ).deserialize( readable );
+                               return adapter.adapt( value );
+                       } catch (AdapterConstructionException e) {
+                               throw new IOException(e);
+                       } catch (AdaptException e) {
+                               throw new IOException(e);
+                       }
+               }
+                       
+               return Bindings.getSerializerUnchecked( binding ).deserialize( readable );
+       }
+
+       /**
+        * Write value as binary file (.dbb).
+        * 
+        * @param file file
+        * @param binding content binding
+        * @param value value
+        * @throws IOException 
+        */
+       public static void writeFile(File file, Binding binding, Object value) throws IOException {
+               BinaryFile rf = new BinaryFile( file );
+               try {
+                       MutableVariant v = new MutableVariant(binding, value);
+                       Serializer s = Bindings.getSerializerUnchecked( Bindings.MUTABLE_VARIANT );
+                       s.serialize(rf, v);
+               } finally {
+                       rf.close();
+               }
+       }       
+
+       public static DataInput openInput( InputStream is )
+       {
+               return new InputStreamReadable(is, Long.MAX_VALUE);
+       }
+       
+       public static DataInput openInput( File file ) throws IOException
+       {
+               return new BinaryFile(file);
+       }
+       
+       public static DataInput openInput( byte[] data ) 
+       {
+               ByteBuffer buffer = ByteBuffer.wrap( data );
+               return new ByteBufferReadable( buffer );
+       }
+       
+       public static DataOutput openOutput( OutputStream os )
+       {
+               return new OutputStreamWriteable( os );
+       }
+       
+       public static DataOutput openOutput( File file ) throws IOException
+       {
+               return new BinaryFile( file );
+       }
+       
+       public static DataOutput openOutput( byte[] data ) throws IOException
+       {
+               ByteBuffer buffer = ByteBuffer.wrap( data );
+               return new ByteBufferWriteable( buffer );
+       }       
+
+}
+