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
9 * VTT Technical Research Centre of Finland - initial API and implementation
10 *******************************************************************************/
11 package org.simantics.databoard.example;
14 import java.util.Arrays;
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;
26 public class FileAccessorExample {
28 public static void main(String[] args) throws Exception {
31 // File Accessor is an access to data that resides in a file in
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);
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 );
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.
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 );
67 // Large datasets can be accessed partially
68 // It conserves memory.
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() );
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++) {
81 aa.addNoflush( Bindings.FLOAT, v );
83 // Flushing ensures bytes are moved from memory cache to disc
84 // Note, reading works without flushing
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( ", ");
93 System.out.println("]");
96 binding = Bindings.getBinding( float[].class );
97 float data[] = (float[]) aa.getValue( binding );
98 System.out.println( Arrays.toString(data) );
100 // File handle can be closed from any of its accessors (fa and aa)
106 // To read the data all-at-once an accessor is not needed, you can use Serializer aswell
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) );