org.tentackle.db
Class Binary

java.lang.Object
  extended by org.tentackle.db.Binary
All Implemented Interfaces:
Serializable, Cloneable

public class Binary
extends Object
implements Serializable, Cloneable

Datatype for storing arbitrary (binary) data in a database.

Its primary use is to store Serializables but it can handle arbitrary byte-data as well. It uses the XXXBinaryStream-methods to read and write the data avoiding the transactional limits of blobs, because blobs usually are only valid within a transaction. Binary instead provides a blob-like interface while remaining valid outside transactions.

See Also:
Serialized Form

Field Summary
static int BUF_SIZE
          initial default buffersize when reading from the database.
 
Constructor Summary
Binary()
          Creates an empty binary
Binary(byte[] data)
          Creates a Binary out of an array of arbitrary bytes.
Binary(Object object)
          Creates a Binary from a Serializable.
 
Method Summary
 Binary clone()
          Clones the binary
static Binary createBinary(InputStream stream, int bufSize)
          Creates a Binary out of an InputStream.
 byte[] getData()
          Retrieves the @{code Binary} data as a byte[]-array.
 InputStream getInputStream()
          Gets the stream to store the Binary in the database.
 int getLength()
          Gets the length in bytes of this Binary.
 Object getObject()
          Retrieves the object encapsulated by the Binary.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BUF_SIZE

public static int BUF_SIZE
initial default buffersize when reading from the database.

Constructor Detail

Binary

public Binary(Object object)
       throws IOException
Creates a Binary from a Serializable.

Parameters:
object - the Serializable to be stored in the database
Throws:
IOException - if some serialization error.

Binary

public Binary(byte[] data)
Creates a Binary out of an array of arbitrary bytes.

Parameters:
data - the byte array to be stored in the database.

Binary

public Binary()
Creates an empty binary

Method Detail

clone

public Binary clone()
Clones the binary

Overrides:
clone in class Object
Returns:
a clone of this instance.
See Also:
Cloneable

createBinary

public static Binary createBinary(InputStream stream,
                                  int bufSize)
                           throws IOException
Creates a Binary out of an InputStream. Used to read a Binary from the database. Notice that stream.available() cannot be used according to sun's spec. The stream is closed after creating the Binary.

Parameters:
stream - the InputStream (associated to the database) to read from, may be null
bufSize - the initial buffer size, 0 = default size (BUF_SIZE)
Returns:
the new Binary or null if stream is null or empty.
Throws:
IOException - if reading from the input stream failed

getLength

public int getLength()
Gets the length in bytes of this Binary. The length of a Binary is either the length of a byte-array (if Binary(byte[]) used) or the length of the serialized object (if Binary(Object) used)

Returns:
the length of the Binary in bytes

getInputStream

public InputStream getInputStream()
Gets the stream to store the Binary in the database.

Returns:
the stream associated to the database.

getObject

public Object getObject()
                 throws IOException,
                        ClassNotFoundException
Retrieves the object encapsulated by the Binary.

Returns:
the de-serialized object (retrieved from the db), or null if NULL-value
Throws:
IOException - if reading the object failed
ClassNotFoundException - if class of a serialized object cannot be found.

getData

public byte[] getData()
Retrieves the @{code Binary} data as a byte[]-array.

Returns:
the byte[] representation of the @{code Binary}, null if none (or NULL-value in db).


Copyright © 2001-2008 Harald Krake, Bergstr. 48, 78098 Triberg, Germany, harald@krake.de