org.simantics.databoard.util.binary
Class BinaryFile

java.lang.Object
  extended by org.simantics.databoard.util.binary.BinaryFile
All Implemented Interfaces:
java.io.DataInput, java.io.DataOutput, 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.

Primitive number writes (int, short, char, double, float and long) and written in big endian (network) byte order. Use Endian to make little endian operations.

Author:
Toni Kalajainen

Nested Class Summary
 
Nested classes/interfaces inherited from interface org.simantics.databoard.util.binary.RandomAccessBinary
RandomAccessBinary.ByteSide
 
Constructor Summary
BinaryFile(java.io.File file)
           
BinaryFile(java.io.File file, int bufSize)
           
BinaryFile(java.io.File file, java.lang.String mode)
           
BinaryFile(java.io.RandomAccessFile file)
           
BinaryFile(java.io.RandomAccessFile file, int bufSize)
           
 
Method Summary
 void close()
          Closes the object.
 java.io.File file()
           
 void flush()
          Flushes internal buffer
 java.io.RandomAccessFile getRandomAccessFile()
           
 void insertBytes(long bytes, RandomAccessBinary.ByteSide side)
          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()
          Check if binary is open.
 long length()
          Get the length of the binary object.
 long position()
          Get the position of the cursor
 void position(long newPosition)
          Set new cursor position.
 boolean readBoolean()
           
 byte readByte()
           
 char readChar()
           
 double readDouble()
           
 float readFloat()
           
 void readFully(byte[] dst)
           
 void readFully(byte[] dst, int offset, int length)
           
 void readFully(java.nio.ByteBuffer buf)
          Read buf fully
 void readFully(java.nio.ByteBuffer buf, int length)
          Read fully length bytes
 int readInt()
           
 java.lang.String readLine()
           
 long readLong()
           
 short readShort()
           
 int readUnsignedByte()
           
 int readUnsignedShort()
           
 java.lang.String readUTF()
           
 void removeBytes(long bytes, RandomAccessBinary.ByteSide side)
          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 reset()
          Clears read&write buffer.
 void setLength(long newLength)
          Set length of the binary object.
 int skipBytes(int bytes)
           
 long skipBytes(long bytes)
           
static BinaryFile tempFile(long size)
           
 java.lang.String toString()
           
 void write(byte[] src)
           
 void write(byte[] src, int offset, int length)
           
 void write(int b)
           
 void writeBoolean(boolean v)
           
 void writeByte(int b)
           
 void writeBytes(java.lang.String s)
           
 void writeChar(int value)
           
 void writeChars(java.lang.String s)
           
 void writeDouble(double value)
           
 void writeFloat(float value)
           
 void writeFully(java.nio.ByteBuffer src)
          Put n bytes from the remaining of the byte array.
 void writeFully(java.nio.ByteBuffer src, int length)
          Put n bytes from the remaining of the byte buffer.
 void writeInt(int value)
           
 void writeLong(long value)
           
 void writeShort(int value)
           
 void writeUTF(java.lang.String s)
           
 
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.File file,
                  java.lang.String mode)
           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

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

isOpen

public boolean isOpen()
Description copied from interface: RandomAccessBinary
Check if binary is open.

Specified by:
isOpen in interface RandomAccessBinary
Returns:
true if file is open

file

public java.io.File file()

getRandomAccessFile

public java.io.RandomAccessFile getRandomAccessFile()

readByte

public byte readByte()
              throws java.io.IOException
Specified by:
readByte in interface java.io.DataInput
Throws:
java.io.IOException

readChar

public char readChar()
              throws java.io.IOException
Specified by:
readChar in interface java.io.DataInput
Throws:
java.io.IOException

readUnsignedByte

public int readUnsignedByte()
                     throws java.io.IOException
Specified by:
readUnsignedByte in interface java.io.DataInput
Throws:
java.io.IOException

readBoolean

public boolean readBoolean()
                    throws java.io.IOException
Specified by:
readBoolean in interface java.io.DataInput
Throws:
java.io.IOException

readFully

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

readFully

public void readFully(byte[] dst)
               throws java.io.IOException
Specified by:
readFully in interface java.io.DataInput
Throws:
java.io.IOException

readFully

public void readFully(java.nio.ByteBuffer buf)
               throws java.io.IOException
Description copied from interface: BinaryReadable
Read buf fully

Specified by:
readFully in interface BinaryReadable
Throws:
java.io.IOException

readFully

public void readFully(java.nio.ByteBuffer buf,
                      int length)
               throws java.io.IOException
Description copied from interface: BinaryReadable
Read fully length bytes

Specified by:
readFully in interface BinaryReadable
Throws:
java.io.IOException

readDouble

public double readDouble()
                  throws java.io.IOException
Specified by:
readDouble in interface java.io.DataInput
Throws:
java.io.IOException

readFloat

public float readFloat()
                throws java.io.IOException
Specified by:
readFloat in interface java.io.DataInput
Throws:
java.io.IOException

readInt

public int readInt()
            throws java.io.IOException
Specified by:
readInt in interface java.io.DataInput
Throws:
java.io.IOException

readLong

public long readLong()
              throws java.io.IOException
Specified by:
readLong in interface java.io.DataInput
Throws:
java.io.IOException

readShort

public short readShort()
                throws java.io.IOException
Specified by:
readShort in interface java.io.DataInput
Throws:
java.io.IOException

readUnsignedShort

public int readUnsignedShort()
                      throws java.io.IOException
Specified by:
readUnsignedShort in interface java.io.DataInput
Throws:
java.io.IOException

readLine

public final java.lang.String readLine()
                                throws java.io.IOException
Specified by:
readLine in interface java.io.DataInput
Throws:
java.io.IOException

readUTF

public final java.lang.String readUTF()
                               throws java.io.IOException
Specified by:
readUTF in interface java.io.DataInput
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

reset

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

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

skipBytes

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

skipBytes

public int skipBytes(int bytes)
              throws java.io.IOException
Specified by:
skipBytes in interface java.io.DataInput
Throws:
java.io.IOException

write

public void write(int b)
           throws java.io.IOException
Specified by:
write in interface java.io.DataOutput
Throws:
java.io.IOException

writeByte

public void writeByte(int b)
               throws java.io.IOException
Specified by:
writeByte in interface java.io.DataOutput
Throws:
java.io.IOException

writeBoolean

public void writeBoolean(boolean v)
                  throws java.io.IOException
Specified by:
writeBoolean in interface java.io.DataOutput
Throws:
java.io.IOException

writeFully

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

Specified by:
writeFully in interface BinaryWriteable
Throws:
java.io.IOException

writeFully

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

Specified by:
writeFully in interface BinaryWriteable
Throws:
java.io.IOException

write

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

write

public void write(byte[] src)
           throws java.io.IOException
Specified by:
write in interface java.io.DataOutput
Throws:
java.io.IOException

writeDouble

public void writeDouble(double value)
                 throws java.io.IOException
Specified by:
writeDouble in interface java.io.DataOutput
Throws:
java.io.IOException

writeFloat

public void writeFloat(float value)
                throws java.io.IOException
Specified by:
writeFloat in interface java.io.DataOutput
Throws:
java.io.IOException

writeInt

public void writeInt(int value)
              throws java.io.IOException
Specified by:
writeInt in interface java.io.DataOutput
Throws:
java.io.IOException

writeLong

public void writeLong(long value)
               throws java.io.IOException
Specified by:
writeLong in interface java.io.DataOutput
Throws:
java.io.IOException

writeShort

public void writeShort(int value)
                throws java.io.IOException
Specified by:
writeShort in interface java.io.DataOutput
Throws:
java.io.IOException

writeChar

public void writeChar(int value)
               throws java.io.IOException
Specified by:
writeChar in interface java.io.DataOutput
Throws:
java.io.IOException

writeBytes

public void writeBytes(java.lang.String s)
                throws java.io.IOException
Specified by:
writeBytes in interface java.io.DataOutput
Throws:
java.io.IOException

writeChars

public void writeChars(java.lang.String s)
                throws java.io.IOException
Specified by:
writeChars in interface java.io.DataOutput
Throws:
java.io.IOException

writeUTF

public void writeUTF(java.lang.String s)
              throws java.io.IOException
Specified by:
writeUTF in interface java.io.DataOutput
Throws:
java.io.IOException

insertBytes

public void insertBytes(long bytes,
                        RandomAccessBinary.ByteSide side)
                 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,
                        RandomAccessBinary.ByteSide side)
                 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