]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.databoard/examples/org/simantics/databoard/example/FileAccessorExample.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.databoard / examples / org / simantics / databoard / example / FileAccessorExample.java
diff --git a/bundles/org.simantics.databoard/examples/org/simantics/databoard/example/FileAccessorExample.java b/bundles/org.simantics.databoard/examples/org/simantics/databoard/example/FileAccessorExample.java
new file mode 100644 (file)
index 0000000..eb03357
--- /dev/null
@@ -0,0 +1,120 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007 VTT Technical Research Centre of Finland and others.\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.example;\r
+\r
+import java.io.File;\r
+import java.util.Arrays;\r
+\r
+import org.simantics.databoard.Accessors;\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.Datatypes;\r
+import org.simantics.databoard.accessor.file.FileArrayAccessor;\r
+import org.simantics.databoard.accessor.file.FileVariantAccessor;\r
+import org.simantics.databoard.binding.Binding;\r
+import org.simantics.databoard.binding.mutable.MutableVariant;\r
+import org.simantics.databoard.serialization.Serializer;\r
+import org.simantics.databoard.type.ArrayType;\r
+\r
+public class FileAccessorExample {\r
+\r
+       public static void main(String[] args) throws Exception {\r
+               \r
+               //\r
+               // File Accessor is an access to data that resides in a file in\r
+               // byte format.\r
+               //\r
+               \r
+               // Create new binary file\r
+               File file = File.createTempFile("variable", ".dbb");\r
+               // Create file and open an accessor\r
+               // Files are always variants, so the root accessor is VariantAccessor.\r
+               FileVariantAccessor fa = Accessors.createFile(file); \r
+               \r
+               // We can put any value into the file\r
+               fa.setContentValue( Bindings.STRING, "Hello World!" );\r
+               // Or change it to completely another type \r
+               fa.setContentValue( Bindings.INTEGER, 500 );\r
+               \r
+               // Values are normally flushed automatically\r
+               // If special *Noflush methods are used...              \r
+               fa.setContentValueNoflush( Bindings.DOUBLE, 99.0 );\r
+               // then a separate cache flush is required.\r
+               fa.flush();\r
+               \r
+               // Close the file\r
+               fa.close();\r
+               \r
+               \r
+               \r
+               // Re-open the file, get an accessor\r
+               fa = Accessors.openAccessor(file); \r
+               // Lets read what the type is\r
+               System.out.println( fa.getContentType() );\r
+               // Read the content, we know its a Double so we use an instance of DoubleBinding\r
+               Double value = (Double) fa.getContentValue( Bindings.DOUBLE );\r
+               System.out.println( value );\r
+               \r
+               \r
+               \r
+               // Large datasets can be accessed partially\r
+               // It conserves memory.\r
+               \r
+               // Initialize the file into a Float[]\r
+               ArrayType floatArrayType = new ArrayType( Datatypes.FLOAT );\r
+               Binding binding = Bindings.getMutableBinding( floatArrayType );\r
+               fa.setContentValue( binding, binding.createDefault() );\r
+               \r
+               // Write partially              \r
+               // Get a sub-accessor to content of the file.   \r
+               FileArrayAccessor aa = (FileArrayAccessor) fa.getContentAccessor();\r
+               // Add 1024 entries one by one\r
+               for (int i=0; i<1024; i++) {\r
+                       float v = i * 1.5f;\r
+                       aa.addNoflush( Bindings.FLOAT, v );\r
+               }\r
+               // Flushing ensures bytes are moved from memory cache to disc\r
+               // Note, reading works without flushing\r
+               aa.flush();\r
+\r
+               // Read partially\r
+               System.out.print("[");\r
+               for (int i=0; i<aa.size(); i++) {\r
+                       System.out.print( aa.get(i, Bindings.FLOAT) );\r
+                       System.out.print( ", ");\r
+               }\r
+               System.out.println("]");\r
+                               \r
+               // Read all-at-once\r
+               binding = Bindings.getBinding( float[].class );\r
+               float data[] = (float[]) aa.getValue( binding );\r
+               System.out.println( Arrays.toString(data) );\r
+               \r
+               // File handle can be closed from any of its accessors (fa and aa)\r
+               aa.close();\r
+               \r
+               \r
+               \r
+                       \r
+               // To read the data all-at-once an accessor is not needed, you can use Serializer aswell\r
+               \r
+               // The root of the file is always Variant so use an instance of VariantBinding\r
+               Serializer s = Bindings.getSerializerUnchecked( Bindings.MUTABLE_VARIANT );             \r
+               MutableVariant variant = (MutableVariant) s.deserialize( file );\r
+               data = (float[]) variant.getValue( binding );\r
+               System.out.println( Arrays.toString(data) );            \r
+               \r
+               \r
+               // Delete tmp file\r
+               file.delete();          \r
+       }\r
+       \r
+}\r
+\r