Package org.apache.james.mime4j.storage
Class StorageOutputStream
- java.lang.Object
-
- java.io.OutputStream
-
- org.apache.james.mime4j.storage.StorageOutputStream
-
- All Implemented Interfaces:
java.io.Closeable,java.io.Flushable,java.lang.AutoCloseable
- Direct Known Subclasses:
CipherStorageProvider.CipherStorageOutputStream,MemoryStorageProvider.MemoryStorageOutputStream,TempFileStorageProvider.TempFileStorageOutputStream,ThresholdStorageProvider.ThresholdStorageOutputStream
public abstract class StorageOutputStream extends java.io.OutputStreamThis class implements an output stream that can be used to create aStorageobject. An instance of this class is obtained by callingStorageProvider.createStorageOutputStream(). The user can then write data to this instance and invoketoStorage()to retrieve aStorageobject that contains the data that has been written.Note that the
StorageOutputStreamdoes not have to be closed explicitly becausetoStorage()invokesclose()if necessary. Also note thattoStorage()may be invoked only once. OneStorageOutputStreamcan create only oneStorageinstance.
-
-
Field Summary
Fields Modifier and Type Field Description private booleanclosedprivate byte[]singleByteprivate booleanusedUp
-
Constructor Summary
Constructors Modifier Constructor Description protectedStorageOutputStream()Sole constructor.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidclose()Closes this output stream.StoragetoStorage()Closes this output stream if it has not already been closed and returns aStorageobject which contains the bytes that have been written to this output stream.protected abstract StoragetoStorage0()Has to be implemented by a concrete subclass to create aStorageobject from the bytes that have been written to thisStorageOutputStream.voidwrite(byte[] buffer)voidwrite(byte[] buffer, int offset, int length)voidwrite(int b)protected abstract voidwrite0(byte[] buffer, int offset, int length)Has to implemented by a concrete subclass to write bytes from the given byte array to thisStorageOutputStream.
-
-
-
Method Detail
-
toStorage
public final Storage toStorage() throws java.io.IOException
Closes this output stream if it has not already been closed and returns aStorageobject 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
Storageobject that can be read from and deleted independently (e.g. if the implementation writes to a file).- Returns:
- a
Storageobject as described above. - Throws:
java.io.IOException- if an I/O error occurs.java.lang.IllegalStateException- if this method has already been called.
-
write
public final void write(int b) throws java.io.IOException- Specified by:
writein classjava.io.OutputStream- Throws:
java.io.IOException
-
write
public final void write(byte[] buffer) throws java.io.IOException- Overrides:
writein classjava.io.OutputStream- Throws:
java.io.IOException
-
write
public final void write(byte[] buffer, int offset, int length) throws java.io.IOException- Overrides:
writein classjava.io.OutputStream- Throws:
java.io.IOException
-
close
public void close() throws java.io.IOExceptionCloses this output stream. Subclasses that override this method have to invokesuper.close().This implementation never throws an
IOExceptionbut a subclass might.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Overrides:
closein classjava.io.OutputStream- Throws:
java.io.IOException- if an I/O error occurs.
-
write0
protected abstract void write0(byte[] buffer, int offset, int length) throws java.io.IOExceptionHas to implemented by a concrete subclass to write bytes from the given byte array to thisStorageOutputStream. This method gets called bywrite(int),write(byte[])andwrite(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:
java.io.IOException- if an I/O error occurs.
-
toStorage0
protected abstract Storage toStorage0() throws java.io.IOException
Has to be implemented by a concrete subclass to create aStorageobject from the bytes that have been written to thisStorageOutputStream. This method gets called bytoStorage()after the preconditions have been checked. The implementation can also be sure that this methods gets invoked only once.- Returns:
- a
Storageobject as described above. - Throws:
java.io.IOException- if an I/O error occurs.
-
-