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