1 /*******************************************************************************
2 * Copyright (c) 2010- Association for Decentralized Information Management in
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.databoard;
14 import java.io.DataInput;
15 import java.io.DataOutput;
17 import java.io.FileInputStream;
18 import java.io.FileOutputStream;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.io.OutputStream;
22 import java.io.OutputStreamWriter;
23 import java.nio.ByteBuffer;
25 import org.simantics.databoard.accessor.error.AccessorException;
26 import org.simantics.databoard.adapter.AdaptException;
27 import org.simantics.databoard.adapter.Adapter;
28 import org.simantics.databoard.adapter.AdapterConstructionException;
29 import org.simantics.databoard.binding.Binding;
30 import org.simantics.databoard.binding.RecordBinding;
31 import org.simantics.databoard.binding.error.BindingException;
32 import org.simantics.databoard.binding.mutable.MutableVariant;
33 import org.simantics.databoard.parser.repository.DataTypeSyntaxError;
34 import org.simantics.databoard.parser.repository.DataValueRepository;
35 import org.simantics.databoard.serialization.RuntimeSerializerConstructionException;
36 import org.simantics.databoard.serialization.Serializer;
37 import org.simantics.databoard.serialization.SerializerConstructionException;
38 import org.simantics.databoard.type.Datatype;
39 import org.simantics.databoard.util.StreamUtil;
40 import org.simantics.databoard.util.binary.BinaryFile;
41 import org.simantics.databoard.util.binary.BinaryReadable;
42 import org.simantics.databoard.util.binary.ByteBufferReadable;
43 import org.simantics.databoard.util.binary.ByteBufferWriteable;
44 import org.simantics.databoard.util.binary.InputStreamReadable;
45 import org.simantics.databoard.util.binary.OutputStreamWriteable;
46 import org.simantics.databoard.util.binary.UTF8;
51 * Create a text file (.dbv) with a value. The file is UTF-8 encoded.
52 * If old file exists, it is deleted.
59 public static void createTextFile(File file, Binding binding, Object value)
63 if (file.exists()) file.delete();
66 String txt = binding.printValueDefinition(value, false);
67 FileOutputStream fos = new FileOutputStream(file, false);
69 OutputStreamWriter os = new OutputStreamWriter(fos, UTF8.CHARSET);
76 } catch(BindingException e) {
77 throw new IOException(e);
82 * Create a binary file (.dbb) with a initial value. Binary file is a variant,
83 * there is a filetype in the header of the file.
84 * If old file exists, it is deleted.
91 public static void createFile(File file, Binding binding, Object value)
94 if (file.exists()) file.delete();
96 MutableVariant v = new MutableVariant(binding, value);
97 Serializer s = Bindings.getSerializerUnchecked( Bindings.MUTABLE_VARIANT );
102 * Create a new binary file (.dbb) with an empty value. Binary file is a variant,
103 * there is a filetype in the header of the file.
104 * If old file exists, it is deleted.
108 * @throws IOException
109 * @throws RuntimeSerializerConstructionException
111 public static void createFile(File file) throws IOException, RuntimeSerializerConstructionException
113 if (file.exists()) file.delete();
114 file.createNewFile();
115 BinaryFile bf = new BinaryFile(file);
117 Binding binding = Bindings.MUTABLE_VARIANT;
118 Object value = new MutableVariant();
119 Serializer s = Bindings.getSerializerUnchecked( binding );
120 s.serialize(bf, value);
127 * Create a binary file (.dbb) with empty value of given type. Binary file is a variant,
128 * there is a filetype in the header of the file.
129 * If old file exists, it is deleted.
133 * @throws IOException
134 * @throws RuntimeSerializerConstructionException
136 public static void createFile(File file, Datatype type) throws IOException, RuntimeSerializerConstructionException
138 if (file.exists()) file.delete();
139 file.createNewFile();
140 BinaryFile bf = new BinaryFile(file);
142 Binding binding = Bindings.MUTABLE_VARIANT;
143 Object value = binding.createDefault();
144 Serializer s = Bindings.getSerializer( binding );
145 s.serialize(bf, value);
146 } catch (BindingException e) {
147 throw new IOException(e);
148 } catch (SerializerConstructionException e) {
149 throw new IOException(e);
157 * Read a text file (.dbv).
162 * @throws IOException
163 * @throws BindingException
164 * @throws DataTypeSyntaxError
166 public static Object readTextFile(File file, Binding binding)
167 throws IOException, DataTypeSyntaxError, BindingException
169 FileInputStream fis = new FileInputStream(file);
171 byte[] data = StreamUtil.readFully(fis);
172 String txt = new String(data, UTF8.CHARSET);
173 DataValueRepository repo = new DataValueRepository();
174 repo.setTypeRepository( Datatypes.datatypeRepository );
175 return binding.parseValue(txt, repo);
182 * Read file type of a binary file.
186 * @throws IOException
188 public static Datatype readFileType(File file) throws IOException {
189 BinaryFile rf = new BinaryFile( file );
191 Binding datatype_binding = Bindings.getBindingUnchecked( Datatype.class );
192 return (Datatype) Bindings.getSerializerUnchecked( datatype_binding ).deserialize( rf );
199 * Read a binary file into a java instance. Binary file is a variant,
200 * there is a filetype in the header of the file.
201 * If requested binding is not the exact binding of the file, an adapter is tried.
204 * @param binding content binding
206 * @throws IOException
208 public static Object readFile(File file, Binding binding) throws IOException {
209 BinaryFile rf = new BinaryFile( file );
211 Binding datatype_binding = Bindings.getBindingUnchecked( Datatype.class );
212 Datatype type = (Datatype) Bindings.getSerializerUnchecked( datatype_binding ).deserialize( rf );
214 if (type.equals(binding.type())) {
215 return Bindings.getSerializerUnchecked( binding ).deserialize(rf);
218 Binding fileContentBinding = Bindings.getMutableBinding(type);
219 Adapter adapter = Bindings.getAdapter(fileContentBinding, binding);
220 Object value = Bindings.getSerializerUnchecked( fileContentBinding ).deserialize(rf);
221 return adapter.adapt( value );
222 } catch (AdapterConstructionException e) {
223 throw new IOException(e);
224 } catch (AdaptException e) {
225 throw new IOException(e);
234 * Read a file to an object.
239 * @throws IOException
241 public static void readFile(File file, RecordBinding binding, Object dst) throws IOException {
242 BinaryFile rf = new BinaryFile( file );
244 Binding datatype_binding = Bindings.getBindingUnchecked( Datatype.class );
245 Datatype type = (Datatype) Bindings.getSerializerUnchecked( datatype_binding ).deserialize( rf );
247 if (type.equals(binding.type())) {
248 Serializer s = Bindings.getSerializerUnchecked( binding );
249 s.deserializeTo(rf, dst);
252 Binding fileContentBinding = Bindings.getMutableBinding(type);
253 Serializer s = Bindings.getSerializerUnchecked( fileContentBinding );
254 Object tmpObj = s.deserialize( rf );
255 binding.readFrom(fileContentBinding, tmpObj, dst);
256 } catch (BindingException e) {
257 throw new IOException(e);
266 * Read input stream into a java instance. Binary file is a variant,
267 * there is a filetype in the header of the file. If requested binding is not the
268 * exact binding of the file, an adapter is tried.<p>
270 * The implementation reads the inputstream fully into memory.<p>
272 * @param is input stream
273 * @param binding content binding
275 * @throws IOException
277 public static Object readFile(InputStream is, Binding binding) throws IOException {
278 BinaryReadable readable = InputStreamReadable.readFully( is );
279 Binding datatype_binding = Bindings.getBindingUnchecked( Datatype.class );
280 Datatype type = (Datatype) Bindings.getSerializerUnchecked( datatype_binding ).deserialize( readable );
282 if (!type.equals(binding.type())) {
284 Binding fileContentBinding = Bindings.getMutableBinding(type);
285 Adapter adapter = Bindings.getAdapter(fileContentBinding, binding);
286 Object value = Bindings.getSerializerUnchecked( fileContentBinding ).deserialize( readable );
287 return adapter.adapt( value );
288 } catch (AdapterConstructionException e) {
289 throw new IOException(e);
290 } catch (AdaptException e) {
291 throw new IOException(e);
295 return Bindings.getSerializerUnchecked( binding ).deserialize( readable );
299 * Read input stream into a java instance. If requested binding is not the
300 * exact binding of the file, an adapter is tried.
302 * @param is input stream
303 * @param streamLength
304 * @param binding content binding
306 * @throws IOException
307 * @throws AccessorException
309 public static Object readFile(InputStream is, long streamLength, Binding binding) throws IOException {
310 BinaryReadable readable = new InputStreamReadable( is, streamLength );
311 Binding datatype_binding = Bindings.getBindingUnchecked( Datatype.class );
312 Datatype type = (Datatype) Bindings.getSerializerUnchecked( datatype_binding ).deserialize( readable );
314 if (!type.equals(binding.type())) {
316 Binding fileContentBinding = Bindings.getMutableBinding(type);
317 Adapter adapter = Bindings.getAdapter(fileContentBinding, binding);
318 Object value = Bindings.getSerializerUnchecked( fileContentBinding ).deserialize( readable );
319 return adapter.adapt( value );
320 } catch (AdapterConstructionException e) {
321 throw new IOException(e);
322 } catch (AdaptException e) {
323 throw new IOException(e);
327 return Bindings.getSerializerUnchecked( binding ).deserialize( readable );
331 * Write value as binary file (.dbb).
334 * @param binding content binding
336 * @throws IOException
338 public static void writeFile(File file, Binding binding, Object value) throws IOException {
339 BinaryFile rf = new BinaryFile( file );
341 MutableVariant v = new MutableVariant(binding, value);
342 Serializer s = Bindings.getSerializerUnchecked( Bindings.MUTABLE_VARIANT );
349 public static DataInput openInput( InputStream is )
351 return new InputStreamReadable(is, Long.MAX_VALUE);
354 public static DataInput openInput( File file ) throws IOException
356 return new BinaryFile(file);
359 public static DataInput openInput( byte[] data )
361 ByteBuffer buffer = ByteBuffer.wrap( data );
362 return new ByteBufferReadable( buffer );
365 public static DataOutput openOutput( OutputStream os )
367 return new OutputStreamWriteable( os );
370 public static DataOutput openOutput( File file ) throws IOException
372 return new BinaryFile( file );
375 public static DataOutput openOutput( byte[] data ) throws IOException
377 ByteBuffer buffer = ByteBuffer.wrap( data );
378 return new ByteBufferWriteable( buffer );