Package org.apache.james.mime4j.storage
Class StorageOutputStream
- java.lang.Object
-
- java.io.OutputStream
-
- org.apache.james.mime4j.storage.StorageOutputStream
-
- All Implemented Interfaces:
Closeable
,Flushable
,AutoCloseable
public abstract class StorageOutputStream extends OutputStream
This class implements an output stream that can be used to create aStorage
object. An instance of this class is obtained by callingStorageProvider.createStorageOutputStream()
. The user can then write data to this instance and invoketoStorage()
to retrieve aStorage
object that contains the data that has been written.Note that the
StorageOutputStream
does not have to be closed explicitly becausetoStorage()
invokesclose()
if necessary. Also note thattoStorage()
may be invoked only once. OneStorageOutputStream
can create only oneStorage
instance.
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
StorageOutputStream()
Sole constructor.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
close()
Closes this output stream.Storage
toStorage()
Closes this output stream if it has not already been closed and returns aStorage
object which contains the bytes that have been written to this output stream.protected abstract Storage
toStorage0()
Has to be implemented by a concrete subclass to create aStorage
object from the bytes that have been written to thisStorageOutputStream
.void
write(byte[] buffer)
void
write(byte[] buffer, int offset, int length)
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 thisStorageOutputStream
.-
Methods inherited from class java.io.OutputStream
flush, nullOutputStream
-
-
-
-
Method Detail
-
toStorage
public final Storage toStorage() throws IOException
Closes this output stream if it has not already been closed and returns aStorage
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 classOutputStream
- Throws:
IOException
-
write
public final void write(byte[] buffer) throws IOException
- Overrides:
write
in classOutputStream
- Throws:
IOException
-
write
public final void write(byte[] buffer, int offset, int length) throws IOException
- Overrides:
write
in classOutputStream
- Throws:
IOException
-
close
public void close() throws IOException
Closes this output stream. Subclasses that override this method have to invokesuper.close()
.This implementation never throws an
IOException
but a subclass might.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classOutputStream
- 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 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:
IOException
- if an I/O error occurs.
-
toStorage0
protected abstract Storage toStorage0() throws IOException
Has to be implemented by a concrete subclass to create aStorage
object 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
Storage
object as described above. - Throws:
IOException
- if an I/O error occurs.
-
-