Class Binary<T extends java.io.Serializable>

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

    See Also:
    Serialized Form
    • Constructor Summary

      Constructors 
      Constructor Description
      Binary()
      Creates an empty binary.
      Binary​(T object)
      Creates a Binary from a Serializable.
    • 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.
      boolean equals​(java.lang.Object o)  
      void freeze()
      Makes this object immutable.
      java.io.InputStream getInputStream()
      Gets the stream to store the Binary in the database.
      int getLength()
      Gets the length in bytes of this Binary.
      T getObject()
      Retrieves the object encapsulated by the Binary.
      int hashCode()  
      void setObject​(T object)
      Sets the serializable.
      • Methods inherited from class java.lang.Object

        finalize, getClass, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Binary

        public Binary​(T object)
               throws java.io.IOException
        Creates a Binary from a Serializable.
        Parameters:
        object - the Serializable to 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: Freezable
        Makes this object immutable.
        Specified by:
        freeze in interface Freezable
      • clone

        public Binary<T> clone()

        Cloned binaries are always not frozen.

        Overrides:
        clone in class java.lang.Object
      • equals

        public boolean equals​(java.lang.Object o)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.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 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 to the database) to read from, may be null
        bufSize - 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 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 java.io.InputStream getInputStream()
        Gets the stream to store the Binary in 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 the Binary.
        Returns:
        the de-serialized object (retrieved from the db), or null if NULL-value
        Throws:
        java.io.IOException - if reading the object failed
        java.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