Module org.tentackle.common
Package org.tentackle.common
Class Binary<T extends java.io.Serializable>
- java.lang.Object
-
- org.tentackle.common.Binary<T>
-
- Type Parameters:
T- the serializable type
- All Implemented Interfaces:
java.io.Serializable,java.lang.Cloneable,Freezable
public class Binary<T extends java.io.Serializable> extends java.lang.Object implements java.io.Serializable, java.lang.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 java.io.Serializable>
Binary<T>createBinary(java.io.InputStream stream, int bufSize, boolean frozen)Creates a Binary out of an InputStream.booleanequals(java.lang.Object o)voidfreeze()Makes this object immutable.java.io.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 java.io.IOException
Creates a Binary from aSerializable.- Parameters:
object- theSerializableto be stored in the database- Throws:
java.io.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.
-
clone
public Binary<T> clone()
Cloned binaries are always not frozen.
- Overrides:
clonein classjava.lang.Object
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equalsin classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
createBinary
public static <T extends java.io.Serializable> Binary<T> createBinary(java.io.InputStream stream, int bufSize, boolean frozen) throws java.io.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:
java.io.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 java.io.InputStream getInputStream()
Gets the stream to store theBinaryin the database.- Returns:
- the stream associated to the database.
-
getObject
public T getObject() throws java.io.IOException, java.lang.ClassNotFoundException
Retrieves the object encapsulated by theBinary.- Returns:
- the de-serialized object (retrieved from the db), or null if NULL-value
- Throws:
java.io.IOException- if reading the object failedjava.lang.ClassNotFoundException- if class of a serialized object cannot be found.
-
setObject
public void setObject(T object) throws java.io.IOException
Sets the serializable.- Parameters:
object- the serializable object.- Throws:
java.io.IOException- if object could not be serialized
-
-