org.simantics.databoard.util.binary
Class BinaryFile

java.lang.Object
  extended by org.simantics.databoard.util.binary.BinaryFile
All Implemented Interfaces:
BinaryReadable, BinaryWriteable, RandomAccessBinary

public class BinaryFile
extends java.lang.Object
implements RandomAccessBinary, BinaryReadable, BinaryWriteable

This class is a Random Access File implementation to RandomAccessBinary. The implementation is buffered. The implementation is not multi-thread safe.

There is a buffer used for reading and writing. It has a buffer read and write position. When read, the buffer is filled. When written, the buffer is written. When the file pointer is moved, the file size changed or flushed the buffers are cleared. If there were unwritten bytes, they are flushed to disc.

There is internal pointer variable. The actual file pointer is moved on disc read and write operations.

Author:
Toni Kalajainen

Constructor Summary
BinaryFile(java.io.File file)
           
BinaryFile(java.io.File file, int bufSize)
           
BinaryFile(java.io.RandomAccessFile file)
           
BinaryFile(java.io.RandomAccessFile file, int bufSize)
           
 
Method Summary
 void clearBuffer()
          Clears read&write buffer.
 void close()
          Closes the object.
 java.io.File file()
           
 void flush()
          Flushes internal buffer
 byte get()
           
 void get(byte[] dst)
           
 void get(byte[] dst, int offset, int length)
           
 void get(java.nio.ByteBuffer buf)
          Get buf fully
 void get(java.nio.ByteBuffer buf, int length)
          Get fully length bytes
 double getDouble()
           
 float getFloat()
           
 int getInt()
           
 long getLong()
           
 java.io.RandomAccessFile getRandomAccessFile()
           
 short getShort()
           
 void insertBytes(long bytes)
          Insert bytes at current pointer.
static void insertBytes(java.io.RandomAccessFile file, long position, long bytes)
          Inserts bytes into a middle of a file.
 boolean isOpen()
           
 long length()
          Get the length of the binary object.
 java.nio.ByteOrder order()
          Get byte order
 void order(java.nio.ByteOrder order)
          Set a new byte order
 long position()
          Get the position of the cursor
 void position(long newPosition)
          Set new cursor position.
 void put(byte b)
          Write a byte
 void put(byte[] src)
           
 void put(byte[] src, int offset, int length)
           
 void put(java.nio.ByteBuffer src)
          Put n bytes from the remaining of the byte array.
 void put(java.nio.ByteBuffer src, int length)
          Put n bytes from the remaining of the byte buffer.
 void putDouble(double value)
           
 void putFloat(float value)
           
 void putInt(int value)
           
 void putLong(long value)
           
 void putShort(short value)
           
 void removeBytes(long bytes)
          Remove bytes from the position of the current pointer.
static void removeBytes(java.io.RandomAccessFile file, long position, long bytes)
          Remove bytes from a file
 void setLength(long newLength)
          Set length of the binary object.
 void skip(long bytes)
          Move pointer forward
static BinaryFile tempFile(long size)
           
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

BinaryFile

public BinaryFile(java.io.RandomAccessFile file)
           throws java.io.IOException
Throws:
java.io.IOException

BinaryFile

public BinaryFile(java.io.File file)
           throws java.io.IOException
Throws:
java.io.IOException

BinaryFile

public BinaryFile(java.io.RandomAccessFile file,
                  int bufSize)
           throws java.io.IOException
Throws:
java.io.IOException

BinaryFile

public BinaryFile(java.io.File file,
                  int bufSize)
           throws java.io.IOException
Throws:
java.io.IOException
Method Detail

tempFile

public static BinaryFile tempFile(long size)
                           throws java.io.IOException
Throws:
java.io.IOException

isOpen

public boolean isOpen()

close

public void close()
           throws java.io.IOException
Closes the object. Note, this will close the input random access file. This method may be called several times.

Specified by:
close in interface RandomAccessBinary
Throws:
java.io.IOException

file

public java.io.File file()

getRandomAccessFile

public java.io.RandomAccessFile getRandomAccessFile()

order

public java.nio.ByteOrder order()
Description copied from interface: RandomAccessBinary
Get byte order

Specified by:
order in interface BinaryReadable
Specified by:
order in interface BinaryWriteable
Specified by:
order in interface RandomAccessBinary
Returns:
byte order

order

public void order(java.nio.ByteOrder order)
Description copied from interface: RandomAccessBinary
Set a new byte order

Specified by:
order in interface BinaryReadable
Specified by:
order in interface BinaryWriteable
Specified by:
order in interface RandomAccessBinary
Parameters:
order - new byte order

get

public byte get()
         throws java.io.IOException
Specified by:
get in interface BinaryReadable
Specified by:
get in interface RandomAccessBinary
Throws:
java.io.IOException

get

public void get(byte[] dst,
                int offset,
                int length)
         throws java.io.IOException
Specified by:
get in interface BinaryReadable
Specified by:
get in interface RandomAccessBinary
Throws:
java.io.IOException

get

public void get(byte[] dst)
         throws java.io.IOException
Specified by:
get in interface BinaryReadable
Specified by:
get in interface RandomAccessBinary
Throws:
java.io.IOException

get

public void get(java.nio.ByteBuffer buf)
         throws java.io.IOException
Description copied from interface: RandomAccessBinary
Get buf fully

Specified by:
get in interface BinaryReadable
Specified by:
get in interface RandomAccessBinary
Throws:
java.io.IOException

get

public void get(java.nio.ByteBuffer buf,
                int length)
         throws java.io.IOException
Description copied from interface: RandomAccessBinary
Get fully length bytes

Specified by:
get in interface BinaryReadable
Specified by:
get in interface RandomAccessBinary
Throws:
java.io.IOException

getDouble

public double getDouble()
                 throws java.io.IOException
Specified by:
getDouble in interface BinaryReadable
Specified by:
getDouble in interface RandomAccessBinary
Throws:
java.io.IOException

getFloat

public float getFloat()
               throws java.io.IOException
Specified by:
getFloat in interface BinaryReadable
Specified by:
getFloat in interface RandomAccessBinary
Throws:
java.io.IOException

getInt

public int getInt()
           throws java.io.IOException
Specified by:
getInt in interface BinaryReadable
Specified by:
getInt in interface RandomAccessBinary
Throws:
java.io.IOException

getLong

public long getLong()
             throws java.io.IOException
Specified by:
getLong in interface BinaryReadable
Specified by:
getLong in interface RandomAccessBinary
Throws:
java.io.IOException

getShort

public short getShort()
               throws java.io.IOException
Specified by:
getShort in interface BinaryReadable
Specified by:
getShort in interface RandomAccessBinary
Throws:
java.io.IOException

position

public long position()
Description copied from interface: RandomAccessBinary
Get the position of the cursor

Specified by:
position in interface BinaryReadable
Specified by:
position in interface RandomAccessBinary
Returns:
cursor

position

public void position(long newPosition)
Description copied from interface: RandomAccessBinary
Set new cursor position. The position can be set outside the binary object.

Specified by:
position in interface RandomAccessBinary

flush

public void flush()
           throws java.io.IOException
Flushes internal buffer

Specified by:
flush in interface BinaryWriteable
Specified by:
flush in interface RandomAccessBinary
Throws:
java.io.IOException

clearBuffer

public void clearBuffer()
                 throws java.io.IOException
Clears read&write buffer. The file can be modified elsewere after this.

Throws:
java.io.IOException

skip

public void skip(long bytes)
          throws java.io.IOException
Description copied from interface: RandomAccessBinary
Move pointer forward

Specified by:
skip in interface BinaryReadable
Specified by:
skip in interface RandomAccessBinary
Throws:
java.io.IOException

put

public void put(byte b)
         throws java.io.IOException
Description copied from interface: RandomAccessBinary
Write a byte

Specified by:
put in interface BinaryWriteable
Specified by:
put in interface RandomAccessBinary
Throws:
java.io.IOException

put

public void put(java.nio.ByteBuffer src)
         throws java.io.IOException
Description copied from interface: RandomAccessBinary
Put n bytes from the remaining of the byte array. This operation moves the pointer in byte buffer.

Specified by:
put in interface BinaryWriteable
Specified by:
put in interface RandomAccessBinary
Throws:
java.io.IOException

put

public void put(java.nio.ByteBuffer src,
                int length)
         throws java.io.IOException
Description copied from interface: RandomAccessBinary
Put n bytes from the remaining of the byte buffer. This operation moves the pointer in byte buffer.

Specified by:
put in interface BinaryWriteable
Specified by:
put in interface RandomAccessBinary
Throws:
java.io.IOException

put

public void put(byte[] src,
                int offset,
                int length)
         throws java.io.IOException
Specified by:
put in interface BinaryWriteable
Specified by:
put in interface RandomAccessBinary
Throws:
java.io.IOException

put

public void put(byte[] src)
         throws java.io.IOException
Specified by:
put in interface BinaryWriteable
Specified by:
put in interface RandomAccessBinary
Throws:
java.io.IOException

putDouble

public void putDouble(double value)
               throws java.io.IOException
Specified by:
putDouble in interface BinaryWriteable
Specified by:
putDouble in interface RandomAccessBinary
Throws:
java.io.IOException

putFloat

public void putFloat(float value)
              throws java.io.IOException
Specified by:
putFloat in interface BinaryWriteable
Specified by:
putFloat in interface RandomAccessBinary
Throws:
java.io.IOException

putInt

public void putInt(int value)
            throws java.io.IOException
Specified by:
putInt in interface BinaryWriteable
Specified by:
putInt in interface RandomAccessBinary
Throws:
java.io.IOException

putLong

public void putLong(long value)
             throws java.io.IOException
Specified by:
putLong in interface BinaryWriteable
Specified by:
putLong in interface RandomAccessBinary
Throws:
java.io.IOException

putShort

public void putShort(short value)
              throws java.io.IOException
Specified by:
putShort in interface BinaryWriteable
Specified by:
putShort in interface RandomAccessBinary
Throws:
java.io.IOException

insertBytes

public void insertBytes(long bytes)
                 throws java.io.IOException
Description copied from interface: RandomAccessBinary
Insert bytes at current pointer. If the pointer is outside the size of the blob, the blob is enlarged to fit the insertion. The operation does not move pointer.

Specified by:
insertBytes in interface RandomAccessBinary
Throws:
java.io.IOException

removeBytes

public void removeBytes(long bytes)
                 throws java.io.IOException
Description copied from interface: RandomAccessBinary
Remove bytes from the position of the current pointer. If the pointer is outside the blob, an error is thrown.

Specified by:
removeBytes in interface RandomAccessBinary
Throws:
java.io.IOException

length

public long length()
            throws java.io.IOException
Description copied from interface: RandomAccessBinary
Get the length of the binary object.

Specified by:
length in interface BinaryReadable
Specified by:
length in interface RandomAccessBinary
Returns:
the length
Throws:
java.io.IOException

setLength

public void setLength(long newLength)
               throws java.io.IOException
Description copied from interface: RandomAccessBinary
Set length of the binary object.

Specified by:
setLength in interface RandomAccessBinary
Throws:
java.io.IOException

insertBytes

public static void insertBytes(java.io.RandomAccessFile file,
                               long position,
                               long bytes)
                        throws java.io.IOException
Inserts bytes into a middle of a file.

Parameters:
file - file
position -
bytes -
Throws:
java.io.IOException

removeBytes

public static void removeBytes(java.io.RandomAccessFile file,
                               long position,
                               long bytes)
                        throws java.io.IOException
Remove bytes from a file

Parameters:
file -
position -
bytes -
Throws:
java.io.IOException

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object