]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/util/binary/BinaryFile2.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / util / binary / BinaryFile2.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.util.binary;
13
14 import java.io.File;
15 import java.io.IOException;
16 import java.io.RandomAccessFile;
17 import java.nio.ByteOrder;
18 import java.util.EnumSet;
19
20 /**
21  * BinaryFile of multiple cache windows.
22  *
23  * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
24  */
25 public class BinaryFile2 {
26
27         static enum Flag { 
28                 File,     // The block represents the contents in file at file_pos
29                 Memory,   // Has allocated memory, and the data is in memory
30                 Cache     // The memory buffer reflects the contents in the file - it is cache
31         }
32         static class Block {
33                 // size of the block
34                 int size;
35                 // Position in file, valid if File-flag is enabled 
36                 long file_pos;
37                 // Memory buffer, valid if Memory-flag is enabled
38                 byte buf[];
39                 // Last read time of virtual time code
40                 int access_time; 
41                 // Flags
42                 EnumSet<Flag> flags;
43                 // Next block in a linked list
44                 Block next;
45         }
46         
47         RandomAccessFile raf;
48         File file;
49         ByteOrder order = ByteOrder.BIG_ENDIAN;
50         Block first = null;
51         int buf_size;
52         long pointer = 0;
53         
54         public BinaryFile2(RandomAccessFile file, int buf_size) throws IOException
55         {
56                 this.raf = file;
57                 this.buf_size = buf_size;
58         }
59
60         public BinaryFile2(File file, int buf_size) throws IOException
61         {
62                 this.raf = new RandomAccessFile(file, "rw");
63                 this.file = file;
64                 this.buf_size = buf_size;
65         }
66
67         /**
68          * Closes the object. Note, this will close the input random access file.
69          * This method may be called several times.
70          *  
71          * @throws IOException
72          */
73         public synchronized void close() throws IOException {
74                 if (raf==null) return;
75                 flush();
76                 pointer = -1;
77                 raf.close();
78                 raf = null;
79                 first = null;
80         }
81         
82         public File file() {
83                 return file;
84         }
85         
86         public RandomAccessFile getRandomAccessFile() {
87                 return raf;
88         }
89         
90         public ByteOrder order() {
91                 return order;
92         }
93
94         public void order(ByteOrder order) {
95                 if (order==null) throw new IllegalArgumentException("null argument");
96                 this.order = order;
97         }
98         
99         /**
100          * Complete flush. After this call all dirty blocks are flushed into disk.
101          * The sum of memory blocks will be less than buf_size.
102          * 
103          * @throws IOException
104          */
105         public void flush() throws IOException {
106                 // Flush
107                 
108                 // Prune until memory blocks use less memory than buf_size
109         }
110         
111 }
112