Class StorageOutputStream

java.lang.Object
java.io.OutputStream
org.apache.james.mime4j.storage.StorageOutputStream
All Implemented Interfaces:
Closeable, Flushable, AutoCloseable
Direct Known Subclasses:
CipherStorageProvider.CipherStorageOutputStream, MemoryStorageProvider.MemoryStorageOutputStream, TempFileStorageProvider.TempFileStorageOutputStream, ThresholdStorageProvider.ThresholdStorageOutputStream

public abstract class StorageOutputStream extends OutputStream
This class implements an output stream that can be used to create a Storage object. An instance of this class is obtained by calling StorageProvider.createStorageOutputStream(). The user can then write data to this instance and invoke toStorage() to retrieve a Storage object that contains the data that has been written.

Note that the StorageOutputStream does not have to be closed explicitly because toStorage() invokes close() if necessary. Also note that toStorage() may be invoked only once. One StorageOutputStream can create only one Storage instance.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private boolean
     
    private byte[]
     
    private boolean
     
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    Sole constructor.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Closes this output stream.
    final Storage
    Closes this output stream if it has not already been closed and returns a Storage object which contains the bytes that have been written to this output stream.
    protected abstract Storage
    Has to be implemented by a concrete subclass to create a Storage object from the bytes that have been written to this StorageOutputStream.
    final void
    write(byte[] buffer)
     
    final void
    write(byte[] buffer, int offset, int length)
     
    final void
    write(int b)
     
    protected abstract void
    write0(byte[] buffer, int offset, int length)
    Has to implemented by a concrete subclass to write bytes from the given byte array to this StorageOutputStream.

    Methods inherited from class java.io.OutputStream

    flush, nullOutputStream

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • singleByte

      private byte[] singleByte
    • closed

      private boolean closed
    • usedUp

      private boolean usedUp
  • Constructor Details

    • StorageOutputStream

      protected StorageOutputStream()
      Sole constructor.
  • Method Details

    • toStorage

      public final Storage toStorage() throws IOException
      Closes this output stream if it has not already been closed and returns a Storage object which contains the bytes that have been written to this output stream.

      Note that this method may not be invoked a second time. This is because for some implementations it is not possible to create another Storage object that can be read from and deleted independently (e.g. if the implementation writes to a file).

      Returns:
      a Storage object as described above.
      Throws:
      IOException - if an I/O error occurs.
      IllegalStateException - if this method has already been called.
    • write

      public final void write(int b) throws IOException
      Specified by:
      write in class OutputStream
      Throws:
      IOException
    • write

      public final void write(byte[] buffer) throws IOException
      Overrides:
      write in class OutputStream
      Throws:
      IOException
    • write

      public final void write(byte[] buffer, int offset, int length) throws IOException
      Overrides:
      write in class OutputStream
      Throws:
      IOException
    • close

      public void close() throws IOException
      Closes this output stream. Subclasses that override this method have to invoke super.close().

      This implementation never throws an IOException but a subclass might.

      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class OutputStream
      Throws:
      IOException - if an I/O error occurs.
    • write0

      protected abstract void write0(byte[] buffer, int offset, int length) throws IOException
      Has to implemented by a concrete subclass to write bytes from the given byte array to this StorageOutputStream. This method gets called by write(int), write(byte[]) and write(byte[], int, int). All the required preconditions have already been checked by these methods, including the check if the output stream has already been closed.
      Parameters:
      buffer - buffer containing bytes to write.
      offset - start offset in the buffer.
      length - number of bytes to write.
      Throws:
      IOException - if an I/O error occurs.
    • toStorage0

      protected abstract Storage toStorage0() throws IOException
      Has to be implemented by a concrete subclass to create a Storage object from the bytes that have been written to this StorageOutputStream. This method gets called by toStorage() after the preconditions have been checked. The implementation can also be sure that this methods gets invoked only once.
      Returns:
      a Storage object as described above.
      Throws:
      IOException - if an I/O error occurs.