org.simantics.databoard.util.binary
Class Blob

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

Nested Class Summary
 
Nested classes/interfaces inherited from interface org.simantics.databoard.util.binary.RandomAccessBinary
RandomAccessBinary.ByteSide
 
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
 RandomAccessBinary getParent()
           
 RandomAccessBinary getSource()
           
 long getStartPositionInSourceBinary()
           
 void insertBytes(long bytes, RandomAccessBinary.ByteSide side)
          Insert bytes at current pointer.
 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 at pointer.
 void setLength(long newLength)
          Modify the size of the blob.
 void setPositionInSource(long start, long length)
           
 int skipBytes(int bytes)
           
 long skipBytes(long bytes)
           
 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

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,
                        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
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

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

readByte

public byte readByte()
              throws java.io.IOException
Specified by:
readByte 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

readLine

public 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

readChar

public char readChar()
              throws java.io.IOException
Specified by:
readChar 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

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

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

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)