org.simantics.databoard.util.binary
Class Blob

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

public class Blob
extends java.lang.Object
implements RandomAccessBinary

Blob represents a part of a larger random access binary. Blob is isolated random access binary, modifications, insertions and removals of bytes outside the represented bytes do not affect the blob. Insertions and removals of bytes to the parent do affect the blob, its start index, length and pointer are changed.

A backend must not be wrapped in a blob more than once.

Grow, Shrink, Insertion, and Removal affects child blobs if affected region intersects with a child. Grow, Shrink, Insertion, and Removal affects parent. It updates parent length, and start positions of the following (not preceding) siblings.

Author:
Toni Kalajainen

Constructor Summary
Blob(RandomAccessBinary parent)
          Create a sub-blob to a random access binary.
Blob(RandomAccessBinary parent, long start, long length)
          Create a sub-blob to a random access binary.
 
Method Summary
 void close()
          Flush and close the blob.
 Blob createSubBlob(long start, long length)
           
 void flush()
          Flush write 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()
           
 RandomAccessBinary getParent()
           
 short getShort()
           
 RandomAccessBinary getSource()
           
 long getStartPositionInSourceBinary()
           
 void insertBytes(long bytes)
          Insert bytes at current pointer.
 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 at pointer.
 void setLength(long newLength)
          Modify the size of the blob.
 void setPositionInSource(long start, long length)
           
 void skip(long bytes)
          Move pointer forward
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Blob

public Blob(RandomAccessBinary parent)
     throws java.io.IOException
Create a sub-blob to a random access binary.

Parameters:
parent -
Throws:
java.io.IOException

Blob

public Blob(RandomAccessBinary parent,
            long start,
            long length)
Create a sub-blob to a random access binary.

Parameters:
parent -
start -
length -
Method Detail

createSubBlob

public Blob createSubBlob(long start,
                          long length)

getParent

public RandomAccessBinary getParent()

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

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
Remove bytes at pointer.

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

setLength

public void setLength(long newLength)
               throws java.io.IOException
Modify the size of the blob. The operation changes the size of the parent blob aswell.

Specified by:
setLength in interface RandomAccessBinary
Parameters:
newLength - new number of bytes
Throws:
java.io.IOException

getSource

public RandomAccessBinary getSource()

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

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

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

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

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

position

public long position()
              throws java.io.IOException
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
Throws:
java.io.IOException

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

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

getStartPositionInSourceBinary

public long getStartPositionInSourceBinary()

toString

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

setPositionInSource

public void setPositionInSource(long start,
                                long length)