1 /*******************************************************************************
\r
2 * Copyright (c) 2010- Association for Decentralized Information Management in
\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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.databoard.util.binary;
\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
21 * BinaryFile of multiple cache windows.
\r
23 * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
\r
25 public class BinaryFile2 {
\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
32 static class Block {
\r
33 // size of the block
\r
35 // Position in file, valid if File-flag is enabled
\r
37 // Memory buffer, valid if Memory-flag is enabled
\r
39 // Last read time of virtual time code
\r
42 EnumSet<Flag> flags;
\r
43 // Next block in a linked list
\r
47 RandomAccessFile raf;
\r
49 ByteOrder order = ByteOrder.BIG_ENDIAN;
\r
54 public BinaryFile2(RandomAccessFile file, int buf_size) throws IOException
\r
57 this.buf_size = buf_size;
\r
60 public BinaryFile2(File file, int buf_size) throws IOException
\r
62 this.raf = new RandomAccessFile(file, "rw");
\r
64 this.buf_size = buf_size;
\r
68 * Closes the object. Note, this will close the input random access file.
\r
69 * This method may be called several times.
\r
71 * @throws IOException
\r
73 public synchronized void close() throws IOException {
\r
74 if (raf==null) return;
\r
82 public File file() {
\r
86 public RandomAccessFile getRandomAccessFile() {
\r
90 public ByteOrder order() {
\r
94 public void order(ByteOrder order) {
\r
95 if (order==null) throw new IllegalArgumentException("null argument");
\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
103 * @throws IOException
\r
105 public void flush() throws IOException {
\r
108 // Prune until memory blocks use less memory than buf_size
\r