Class Binary<T extends Serializable>

java.lang.Object
org.tentackle.common.AbstractBinary<T>
org.tentackle.common.Binary<T>
Type Parameters:
T - the serializable type
All Implemented Interfaces:
Serializable, Cloneable, Freezable

public final class Binary<T extends Serializable> extends AbstractBinary<T> implements Serializable
Datatype for storing an object in a database.
Uses Java Object Serialization (JOS) under the hood.

It uses the XXXBinaryStream-methods of ResultSet 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.

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 further changes to the object are not reflected in the internal byte array.

See Also:
  • Constructor Details

    • Binary

      public Binary(T 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(InputStream stream, int bufSize, boolean frozen) throws IOException
      Creates a binary from an InputStream.
      Used to read a binary from the database. The stream is closed after creating the binary.
      Parameters:
      stream - the InputStream (associated with the database) to read from may be null
      bufSize - the initial buffer size, 0 = default size
      frozen - true if binary is frozen
      Throws:
      IOException - if reading from the input stream failed
    • Binary

      public Binary()
      Creates an empty binary.
  • Method Details

    • create

      public static <T extends Serializable> Binary<T> create(InputStream stream, int bufSize, boolean frozen) 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.
      Type Parameters:
      T - the serializable type
      Parameters:
      stream - the InputStream (associated with the database) to read from may be null
      bufSize - the initial buffer size, 0 = default size
      frozen - true if Binary is frozen
      Returns:
      the new Binary or null if the stream is null or empty.
      Throws:
      IOException - if reading from the input stream failed
    • clone

      public Binary<T> clone()

      Cloned binaries are always not frozen.

      Cloned binaries are always not frozen.

      Overrides:
      clone in class AbstractBinary<T extends Serializable>
    • getObject

      public T getObject() throws IOException, ClassNotFoundException
      Retrieves the object encapsulated by the Binary.
      Specified by:
      getObject in class AbstractBinary<T extends Serializable>
      Returns:
      the deserialized object (retrieved from the db), or null if NULL-value
      Throws:
      IOException - if reading the object failed
      ClassNotFoundException - if the class of a serialized object cannot be found.
    • setObject

      public void setObject(T object) throws IOException
      Sets the serializable.
      Specified by:
      setObject in class AbstractBinary<T extends Serializable>
      Parameters:
      object - the serializable object.
      Throws:
      IOException - if the object could not be serialized