org.simantics.databoard.util.binary
Class BinaryMemory

java.lang.Object
  extended by org.simantics.databoard.util.binary.BinaryMemory
All Implemented Interfaces:
java.io.DataInput, java.io.DataOutput, BinaryReadable, BinaryWriteable, RandomAccessBinary

public class BinaryMemory
extends java.lang.Object
implements RandomAccessBinary

Rancom access memory blob

Author:
Toni Kalajainen

Nested Class Summary
 
Nested classes/interfaces inherited from interface org.simantics.databoard.util.binary.RandomAccessBinary
RandomAccessBinary.ByteSide
 
Constructor Summary
BinaryMemory()
          New memory blob
BinaryMemory(byte[] data)
          New memory blob
BinaryMemory(java.nio.ByteBuffer buf)
          Assume an existing byte buffer
BinaryMemory(int initialSize)
          New memory blob
BinaryMemory(int initialSize, int increment)
          New memory blob
 
Method Summary
 void close()
          Flush and close the blob.
 void flush()
          Flush write buffer
 void insertBytes(long bytes, RandomAccessBinary.ByteSide side)
          Insert bytes at current pointer.
 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.
 void put(java.io.InputStream is)
           
 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.
 void reset()
          Reset internal cache.
 void setLength(long newLength)
          Set length of the binary object.
 int skipBytes(int bytes)
           
 long skipBytes(long bytes)
           
 java.nio.ByteBuffer toByteBuffer()
          Get the backend byte buffer.
 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

BinaryMemory

public BinaryMemory()
New memory blob


BinaryMemory

public BinaryMemory(byte[] data)
New memory blob


BinaryMemory

public BinaryMemory(int initialSize)
New memory blob


BinaryMemory

public BinaryMemory(int initialSize,
                    int increment)
New memory blob


BinaryMemory

public BinaryMemory(java.nio.ByteBuffer buf)
Assume an existing byte buffer

Parameters:
buf - buffer
Method Detail

toByteBuffer

public java.nio.ByteBuffer toByteBuffer()
Get the backend byte buffer. The result may change if BinaryMemory is written.

Returns:
byte buffer

close

public void close()
           throws java.io.IOException
Description copied from interface: RandomAccessBinary
Flush and close the blob.

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

flush

public void flush()
           throws java.io.IOException
Description copied from interface: RandomAccessBinary
Flush write 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
Description copied from interface: RandomAccessBinary
Reset internal cache. Before reseting unwritten changes are flushed to disc.

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

readByte

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

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

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)
Specified by:
readFully in interface java.io.DataInput

readFully

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

readFully

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

Specified by:
readFully in interface BinaryReadable

readFully

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

Specified by:
readFully in interface BinaryReadable

readDouble

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

readFloat

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

readInt

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

readLong

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

readShort

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

readChar

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

readUnsignedShort

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

length

public long length()
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

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)
              throws java.io.IOException
Description copied from interface: RandomAccessBinary
Set new cursor position. The position can be set outside the binary object.

Specified by:
position in interface RandomAccessBinary
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

put

public void put(java.io.InputStream is)
         throws java.io.IOException
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

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

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

toString

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