1 /*******************************************************************************
\r
2 * Copyright (c) 2010 Association for Decentralized Information Management in
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.databoard.file;
14 import java.io.File;
\r
15 import java.io.IOException;
\r
16 import java.io.RandomAccessFile;
\r
17 import java.util.RandomAccess;
\r
19 import org.simantics.databoard.Bindings;
\r
20 import org.simantics.databoard.binding.Binding;
\r
21 import org.simantics.databoard.binding.error.BindingConstructionException;
\r
22 import org.simantics.databoard.serialization.SerializationException;
\r
23 import org.simantics.databoard.serialization.SerializerConstructionException;
\r
24 import org.simantics.databoard.serialization.SerializerScheme;
\r
25 import org.simantics.databoard.util.binary.BinaryFile;
\r
28 * FileList is a file based implementation of a List collection.
29 * add() serializes the object to a file and get() deserializes.<p>
31 * Set, remove, insert and add operations flushes modifications to disk before
34 * Each operation may throw {@link RuntimeIOException}<p>
36 * Entry position index is on open if the file has variable width
37 * data type (eg. String). The entire file is scanned through.<p>
39 * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
41 public class FileList<T> extends RandomAccessBinaryList<T> implements IFileList<T>, RandomAccess {
46 /** Random Access File */
50 * Create new random access list of Variants (Object) backed by a file
54 * @throws BindingConstructionException
55 * @throws SerializationException
56 * @throws SerializerConstructionException
58 public FileList(File file) throws IOException, BindingConstructionException, SerializerConstructionException, SerializationException
60 this(file, Bindings.getBinding(Object.class), 0L, Bindings.serializationFactory);
64 * Create new random access list backed by a file
69 * @throws BindingConstructionException
70 * @throws SerializationException
71 * @throws SerializerConstructionException
73 public FileList(File file, Class<T> clazz) throws IOException, BindingConstructionException, SerializerConstructionException, SerializationException
75 this(file, Bindings.getBinding(clazz), 0L, Bindings.serializationFactory);
79 * Create new random access list backed by a file
84 * @throws BindingConstructionException
85 * @throws SerializationException
86 * @throws SerializerConstructionException
88 public FileList(String file, Class<T> clazz) throws IOException, BindingConstructionException, SerializerConstructionException, SerializationException
90 this(new File(file), Bindings.getBinding(clazz), 0L, Bindings.serializationFactory);
94 * Create new random access list backed by a file
99 * @throws SerializationException
100 * @throws SerializerConstructionException
102 public FileList(String file, Binding binding) throws IOException, SerializerConstructionException, SerializationException
104 this(new File(file), binding, 0L, Bindings.serializationFactory);
108 * Create new random access list backed by a file
112 * @throws IOException
113 * @throws SerializationException
114 * @throws SerializerConstructionException
116 public FileList(File file, Binding binding) throws IOException, SerializerConstructionException, SerializationException
118 this(file, binding, 0L, Bindings.serializationFactory);
123 * Create new random access list backed by a file
128 * @throws IOException
129 * @throws BindingConstructionException
130 * @throws SerializationException
131 * @throws SerializerConstructionException
133 public FileList(File file, Class<T> clazz, long startPos)
134 throws IOException, BindingConstructionException, SerializerConstructionException, SerializationException
136 this(file, Bindings.getBinding(clazz), startPos, Bindings.serializationFactory);
140 * Create new random access list backed by a file using the default binary serialization.
144 * @param startPos The position of the first sample in file
145 * @throws IOException
146 * @throws SerializationException Error with the file, could not build entry index table
147 * @throws SerializerConstructionException
149 public FileList(File file, Binding binding, long startPos)
150 throws IOException, SerializationException, SerializerConstructionException
152 super(new BinaryFile(new RandomAccessFile(file, "rw")), binding, startPos, Bindings.serializationFactory);
\r
154 raf = ((BinaryFile)blob).getRandomAccessFile();
\r
158 * Create new random access list backed by a file
162 * @param startPos The position of the first sample in file
163 * @param format serialization format
164 * @throws IOException
165 * @throws SerializerConstructionException could not create serializer, never thrown with BinarySerializationFormat
166 * @throws SerializationException Error with the file, could not build entry index
168 public FileList(File file, Binding binding, long startPos, SerializerScheme format)
169 throws IOException, SerializerConstructionException, SerializationException
171 super(new BinaryFile(new RandomAccessFile(file, "rw")), binding, startPos, format);
173 raf = ((BinaryFile)blob).getRandomAccessFile();
177 * Flushes the caches and closes the file handle.
185 } catch (IOException ignored) {
190 public File getFile() {