--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2010- Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ * \r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.databoard.util.binary;\r
+\r
+import java.io.File;\r
+import java.io.IOException;\r
+import java.io.RandomAccessFile;\r
+import java.nio.ByteOrder;\r
+import java.util.EnumSet;\r
+\r
+/**\r
+ * BinaryFile of multiple cache windows.\r
+ *\r
+ * @author Toni Kalajainen <toni.kalajainen@vtt.fi>\r
+ */\r
+public class BinaryFile2 {\r
+\r
+ static enum Flag { \r
+ File, // The block represents the contents in file at file_pos\r
+ Memory, // Has allocated memory, and the data is in memory\r
+ Cache // The memory buffer reflects the contents in the file - it is cache\r
+ }\r
+ static class Block {\r
+ // size of the block\r
+ int size;\r
+ // Position in file, valid if File-flag is enabled \r
+ long file_pos;\r
+ // Memory buffer, valid if Memory-flag is enabled\r
+ byte buf[];\r
+ // Last read time of virtual time code\r
+ int access_time; \r
+ // Flags\r
+ EnumSet<Flag> flags;\r
+ // Next block in a linked list\r
+ Block next;\r
+ }\r
+ \r
+ RandomAccessFile raf;\r
+ File file;\r
+ ByteOrder order = ByteOrder.BIG_ENDIAN;\r
+ Block first = null;\r
+ int buf_size;\r
+ long pointer = 0;\r
+ \r
+ public BinaryFile2(RandomAccessFile file, int buf_size) throws IOException\r
+ {\r
+ this.raf = file;\r
+ this.buf_size = buf_size;\r
+ }\r
+\r
+ public BinaryFile2(File file, int buf_size) throws IOException\r
+ {\r
+ this.raf = new RandomAccessFile(file, "rw");\r
+ this.file = file;\r
+ this.buf_size = buf_size;\r
+ }\r
+\r
+ /**\r
+ * Closes the object. Note, this will close the input random access file.\r
+ * This method may be called several times.\r
+ * \r
+ * @throws IOException\r
+ */\r
+ public synchronized void close() throws IOException {\r
+ if (raf==null) return;\r
+ flush();\r
+ pointer = -1;\r
+ raf.close();\r
+ raf = null;\r
+ first = null;\r
+ }\r
+ \r
+ public File file() {\r
+ return file;\r
+ }\r
+ \r
+ public RandomAccessFile getRandomAccessFile() {\r
+ return raf;\r
+ }\r
+ \r
+ public ByteOrder order() {\r
+ return order;\r
+ }\r
+\r
+ public void order(ByteOrder order) {\r
+ if (order==null) throw new IllegalArgumentException("null argument");\r
+ this.order = order;\r
+ }\r
+ \r
+ /**\r
+ * Complete flush. After this call all dirty blocks are flushed into disk.\r
+ * The sum of memory blocks will be less than buf_size.\r
+ * \r
+ * @throws IOException\r
+ */\r
+ public void flush() throws IOException {\r
+ // Flush\r
+ \r
+ // Prune until memory blocks use less memory than buf_size\r
+ }\r
+ \r
+}\r
+\r