- java.lang.Object
-
- org.tentackle.common.Binary<T>
-
- Type Parameters:
T- the serializable type
- All Implemented Interfaces:
Serializable,Cloneable,Freezable
public class Binary<T extends Serializable> extends Object implements Serializable, Cloneable, Freezable
Datatype for storing arbitrary (binary) data in a database.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.
Binaryinstead provides a blob-like interface while remaining valid outside transactions.Furthermore, binaries can be frozen, i.e. made immutable.
Keep in mind that the wrapped object is serialized when the Binary is created or setObject is invoked. Any subsequent changes to the object are not reflected in the internal byte array.
- See Also:
- Serialized Form
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Binary<T>clone()static <T extends Serializable>
Binary<T>createBinary(InputStream stream, int bufSize, boolean frozen)Creates a Binary out of an InputStream.booleanequals(Object o)voidfreeze()Makes this object immutable.InputStreamgetInputStream()Gets the stream to store theBinaryin the database.intgetLength()Gets the length in bytes of thisBinary.TgetObject()Retrieves the object encapsulated by theBinary.inthashCode()voidsetObject(T object)Sets the serializable.
-
-
-
Constructor Detail
-
Binary
public Binary(T object) throws IOException
Creates a Binary from aSerializable.- Parameters:
object- theSerializableto be stored in the database- Throws:
IOException- if some serialization error.
-
Binary
public Binary()
Creates an empty binary.
-
-
Method Detail
-
freeze
public void freeze()
Description copied from interface:FreezableMakes this object immutable.
-
createBinary
public static <T extends Serializable> Binary<T> createBinary(InputStream stream, int bufSize, boolean frozen) throws IOException
Creates a Binary out of an InputStream. Used to read aBinaryfrom the database. Notice that stream.available() cannot be used according to sun's spec. The stream is closed after creating the Binary.- Type Parameters:
T- the serializable type- Parameters:
stream- the InputStream (associated to the database) to read from, may be nullbufSize- the initial buffer size, 0 = default size (bufSize)frozen- true if binary is frozen- 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 thisBinary. The length of aBinaryis either the length of a byte-array (ifBinary(byte[])used) or the length of the serialized object (ifBinary(Object)used)- Returns:
- the length of the
Binaryin bytes
-
getInputStream
public InputStream getInputStream()
Gets the stream to store theBinaryin the database.- Returns:
- the stream associated to the database.
-
getObject
public T getObject() throws IOException, ClassNotFoundException
Retrieves the object encapsulated by theBinary.- Returns:
- the de-serialized object (retrieved from the db), or null if NULL-value
- Throws:
IOException- if reading the object failedClassNotFoundException- if class of a serialized object cannot be found.
-
setObject
public void setObject(T object) throws IOException
Sets the serializable.- Parameters:
object- the serializable object.- Throws:
IOException- if object could not be serialized
-
-