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