Class AssembledMessageState

  • All Implemented Interfaces:
    AutoCloseable

    public class AssembledMessageState
    extends Object
    implements AutoCloseable
    Maintains the state of an assembled message. This class is NOT thread-safe.
    Author:
    Thomas Pantelis
    • Constructor Detail

      • AssembledMessageState

        public AssembledMessageState​(Identifier identifier,
                                     int totalSlices,
                                     FileBackedOutputStreamFactory fileBackedStreamFactory,
                                     String logContext)
        Constructor.
        Parameters:
        identifier - the identifier for this instance
        totalSlices - the total number of slices to expect
        fileBackedStreamFactory - factory for creating the FileBackedOutputStream instance used for streaming
        logContext - the context for log messages
    • Method Detail

      • getIdentifier

        public Identifier getIdentifier()
        Returns the identifier of this instance.
        Returns:
        the identifier
      • addSlice

        public boolean addSlice​(int sliceIndex,
                                byte[] data,
                                int lastSliceHashCode)
                         throws MessageSliceException
        Adds a slice to the assembled stream.
        Parameters:
        sliceIndex - the index of the slice
        data - the sliced data
        lastSliceHashCode - the hash code of the last slice sent
        Returns:
        true if this is the last slice received, false otherwise
        Throws:
        MessageSliceException -
        • if the slice index is invalid
        • if the last slice hash code is invalid
        • if an error occurs writing the data to the stream
        In addition, this instance is automatically closed and can no longer be used.
        AssemblerSealedException - if this instance is already sealed (ie has received all the slices)
        AssemblerClosedException - if this instance is already closed
      • getAssembledBytes

        public ByteSource getAssembledBytes()
                                     throws IOException
        Returns the assembled bytes as a ByteSource. This method must only be called after this instance is sealed.
        Returns:
        a ByteSource containing the assembled bytes
        Throws:
        IOException - if an error occurs obtaining the assembled bytes
        IllegalStateException - is this instance is not sealed