]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 /*******************************************************************************\r
2  * Copyright (c) 2007 VTT Technical Research Centre of Finland and others.\r
3  * All rights reserved. This program and the accompanying materials\r
4  * are made available under the terms of the Eclipse Public License v1.0\r
5  * which accompanies this distribution, and is available at\r
6  * http://www.eclipse.org/legal/epl-v10.html\r
7  *\r
8  * Contributors:\r
9  *     VTT Technical Research Centre of Finland - initial API and implementation\r
10  *******************************************************************************/\r
11 package org.simantics.databoard.example;\r
12 \r
13 import java.io.File;\r
14 import java.util.Arrays;\r
15 \r
16 import org.simantics.databoard.Accessors;\r
17 import org.simantics.databoard.Bindings;\r
18 import org.simantics.databoard.Datatypes;\r
19 import org.simantics.databoard.accessor.file.FileArrayAccessor;\r
20 import org.simantics.databoard.accessor.file.FileVariantAccessor;\r
21 import org.simantics.databoard.binding.Binding;\r
22 import org.simantics.databoard.binding.mutable.MutableVariant;\r
23 import org.simantics.databoard.serialization.Serializer;\r
24 import org.simantics.databoard.type.ArrayType;\r
25 \r
26 public class FileAccessorExample {\r
27 \r
28         public static void main(String[] args) throws Exception {\r
29                 \r
30                 //\r
31                 // File Accessor is an access to data that resides in a file in\r
32                 // byte format.\r
33                 //\r
34                 \r
35                 // Create new binary file\r
36                 File file = File.createTempFile("variable", ".dbb");\r
37                 // Create file and open an accessor\r
38                 // Files are always variants, so the root accessor is VariantAccessor.\r
39                 FileVariantAccessor fa = Accessors.createFile(file); \r
40                 \r
41                 // We can put any value into the file\r
42                 fa.setContentValue( Bindings.STRING, "Hello World!" );\r
43                 // Or change it to completely another type \r
44                 fa.setContentValue( Bindings.INTEGER, 500 );\r
45                 \r
46                 // Values are normally flushed automatically\r
47                 // If special *Noflush methods are used...              \r
48                 fa.setContentValueNoflush( Bindings.DOUBLE, 99.0 );\r
49                 // then a separate cache flush is required.\r
50                 fa.flush();\r
51                 \r
52                 // Close the file\r
53                 fa.close();\r
54                 \r
55                 \r
56                 \r
57                 // Re-open the file, get an accessor\r
58                 fa = Accessors.openAccessor(file); \r
59                 // Lets read what the type is\r
60                 System.out.println( fa.getContentType() );\r
61                 // Read the content, we know its a Double so we use an instance of DoubleBinding\r
62                 Double value = (Double) fa.getContentValue( Bindings.DOUBLE );\r
63                 System.out.println( value );\r
64                 \r
65                 \r
66                 \r
67                 // Large datasets can be accessed partially\r
68                 // It conserves memory.\r
69                 \r
70                 // Initialize the file into a Float[]\r
71                 ArrayType floatArrayType = new ArrayType( Datatypes.FLOAT );\r
72                 Binding binding = Bindings.getMutableBinding( floatArrayType );\r
73                 fa.setContentValue( binding, binding.createDefault() );\r
74                 \r
75                 // Write partially              \r
76                 // Get a sub-accessor to content of the file.   \r
77                 FileArrayAccessor aa = (FileArrayAccessor) fa.getContentAccessor();\r
78                 // Add 1024 entries one by one\r
79                 for (int i=0; i<1024; i++) {\r
80                         float v = i * 1.5f;\r
81                         aa.addNoflush( Bindings.FLOAT, v );\r
82                 }\r
83                 // Flushing ensures bytes are moved from memory cache to disc\r
84                 // Note, reading works without flushing\r
85                 aa.flush();\r
86 \r
87                 // Read partially\r
88                 System.out.print("[");\r
89                 for (int i=0; i<aa.size(); i++) {\r
90                         System.out.print( aa.get(i, Bindings.FLOAT) );\r
91                         System.out.print( ", ");\r
92                 }\r
93                 System.out.println("]");\r
94                                 \r
95                 // Read all-at-once\r
96                 binding = Bindings.getBinding( float[].class );\r
97                 float data[] = (float[]) aa.getValue( binding );\r
98                 System.out.println( Arrays.toString(data) );\r
99                 \r
100                 // File handle can be closed from any of its accessors (fa and aa)\r
101                 aa.close();\r
102                 \r
103                 \r
104                 \r
105                         \r
106                 // To read the data all-at-once an accessor is not needed, you can use Serializer aswell\r
107                 \r
108                 // The root of the file is always Variant so use an instance of VariantBinding\r
109                 Serializer s = Bindings.getSerializerUnchecked( Bindings.MUTABLE_VARIANT );             \r
110                 MutableVariant variant = (MutableVariant) s.deserialize( file );\r
111                 data = (float[]) variant.getValue( binding );\r
112                 System.out.println( Arrays.toString(data) );            \r
113                 \r
114                 \r
115                 // Delete tmp file\r
116                 file.delete();          \r
117         }\r
118         \r
119 }\r
120 \r