]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/file/FileList.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / file / FileList.java
1 /*******************************************************************************
2  *  Copyright (c) 2010 Association for Decentralized Information Management in
3  *  Industry THTH ry.
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
8  *
9  *  Contributors:
10  *      VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.databoard.file;
13
14 import java.io.File;
15 import java.io.IOException;
16 import java.io.RandomAccessFile;
17 import java.util.RandomAccess;
18
19 import org.simantics.databoard.Bindings;
20 import org.simantics.databoard.binding.Binding;
21 import org.simantics.databoard.binding.error.BindingConstructionException;
22 import org.simantics.databoard.serialization.SerializationException;
23 import org.simantics.databoard.serialization.SerializerConstructionException;
24 import org.simantics.databoard.serialization.SerializerScheme;
25 import org.simantics.databoard.util.binary.BinaryFile;
26
27 /**
28  * FileList is a file based implementation of a List collection.
29  * add() serializes the object to a file and get() deserializes.<p>
30  * 
31  * Set, remove, insert and add operations flushes modifications to disk before
32  * returning.<p>
33  * 
34  * Each operation may throw {@link RuntimeIOException}<p> 
35  * 
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>
38  * 
39  * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
40  */
41 public class FileList<T> extends RandomAccessBinaryList<T> implements IFileList<T>, RandomAccess {
42         
43         /** File Reference */
44         File f;
45         
46         /** Random Access File */
47         RandomAccessFile raf;
48
49         /**
50          * Create new random access list of Variants (Object) backed by a file
51          * 
52          * @param file
53          * @throws IOException
54          * @throws BindingConstructionException 
55          * @throws SerializationException 
56          * @throws SerializerConstructionException 
57          */
58         public FileList(File file) throws IOException, BindingConstructionException, SerializerConstructionException, SerializationException
59         {
60                 this(file, Bindings.getBinding(Object.class), 0L, Bindings.serializationFactory);
61         }
62         
63         /**
64          * Create new random access list backed by a file
65          * 
66          * @param file
67          * @param clazz
68          * @throws IOException
69          * @throws BindingConstructionException 
70          * @throws SerializationException 
71          * @throws SerializerConstructionException 
72          */
73         public FileList(File file, Class<T> clazz) throws IOException, BindingConstructionException, SerializerConstructionException, SerializationException
74         {
75                 this(file, Bindings.getBinding(clazz), 0L, Bindings.serializationFactory);
76         }
77         
78         /**
79          * Create new random access list backed by a file
80          * 
81          * @param file
82          * @param clazz
83          * @throws IOException
84          * @throws BindingConstructionException 
85          * @throws SerializationException 
86          * @throws SerializerConstructionException 
87          */
88         public FileList(String file, Class<T> clazz) throws IOException, BindingConstructionException, SerializerConstructionException, SerializationException
89         {
90                 this(new File(file), Bindings.getBinding(clazz), 0L, Bindings.serializationFactory);
91         }       
92
93         /**
94          * Create new random access list backed by a file
95          * 
96          * @param file
97          * @param binding
98          * @throws IOException
99          * @throws SerializationException 
100          * @throws SerializerConstructionException 
101          */
102         public FileList(String file, Binding binding) throws IOException, SerializerConstructionException, SerializationException
103         {
104                 this(new File(file), binding, 0L, Bindings.serializationFactory);
105         }
106         
107         /**
108          * Create new random access list backed by a file
109          * 
110          * @param file
111          * @param binding
112          * @throws IOException
113          * @throws SerializationException 
114          * @throws SerializerConstructionException 
115          */
116         public FileList(File file, Binding binding) throws IOException, SerializerConstructionException, SerializationException
117         {
118                 this(file, binding, 0L, Bindings.serializationFactory);
119         }
120         
121         
122         /**
123          * Create new random access list backed by a file
124          * 
125          * @param file
126          * @param clazz
127          * @param startPos
128          * @throws IOException
129          * @throws BindingConstructionException 
130          * @throws SerializationException 
131          * @throws SerializerConstructionException 
132          */
133         public FileList(File file, Class<T> clazz, long startPos) 
134         throws IOException, BindingConstructionException, SerializerConstructionException, SerializationException
135         {
136                 this(file, Bindings.getBinding(clazz), startPos, Bindings.serializationFactory);
137         }
138         
139         /**
140          * Create new random access list backed by a file using the default binary serialization.
141          * 
142          * @param file file
143          * @param binding
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 
148          */
149         public FileList(File file, Binding binding, long startPos) 
150         throws IOException, SerializationException, SerializerConstructionException
151         {
152                 super(new BinaryFile(new RandomAccessFile(file, "rw")), binding, startPos, Bindings.serializationFactory);
153                 this.f = file;
154                 raf = ((BinaryFile)blob).getRandomAccessFile();
155         }
156         
157         /**
158          * Create new random access list backed by a file
159          * 
160          * @param file file
161          * @param binding
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
167          */
168         public FileList(File file, Binding binding, long startPos, SerializerScheme format) 
169         throws IOException, SerializerConstructionException, SerializationException 
170         {               
171                 super(new BinaryFile(new RandomAccessFile(file, "rw")), binding, startPos, format);
172                 f = file;
173                 raf = ((BinaryFile)blob).getRandomAccessFile();         
174         }       
175         
176         /**
177          * Flushes the caches and closes the file handle. 
178          */
179         public void close()
180         {
181                 super.close();          
182                 if (raf!=null)
183                 try {
184                         raf.close();
185                 } catch (IOException ignored) {
186                 }
187         }
188
189         @Override
190         public File getFile() {
191                 return f;
192         }
193
194 }
195