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