@NotThreadSafe public class CipherOutputStream extends DecoratingOutputStream
javax.crypto.CipherOutputStream
with some exceptions:
BufferedBlockCipher
for ciphering.
cipher
used for encryption or decryption is accessible to
subclasses.
flush()
method just flushes the underlying output stream
and has no effect on the cipher.
finish()
method has been added to allow finishing the output
(probably producing padding bytes) without closing the output.
This could be used in a subclass to produce a trailer with additional
information about the ciphered data (e.g. a MAC).
CipherReadOnlyFile
Modifier and Type | Field and Description |
---|---|
protected BufferedBlockCipher |
cipher
The buffered block cipher used for preprocessing the output.
|
delegate
Constructor and Description |
---|
CipherOutputStream(OutputStream out,
BufferedBlockCipher cipher)
Creates a new cipher output stream.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes this output stream and releases any resources associated with it.
|
protected void |
finish()
Finishes and voids this cipher output stream.
|
void |
write(byte[] buf,
int off,
int len)
Ciphers and writes the contents of the given byte array to the
underlying output stream.
|
void |
write(int b)
Ciphers and writes the given byte to the underlying output stream.
|
flush, toString, write
@Nullable protected BufferedBlockCipher cipher
@CreatesObligation public CipherOutputStream(@CheckForNull @WillCloseWhenClosed OutputStream out, @CheckForNull BufferedBlockCipher cipher)
javax.crypto.CipherOutputStream
,
the cipher does not need to be initialized before calling this
constructor.
However, the cipher must be initialized before anything is actually
written to this stream or before this stream is closed.out
- The output stream to write the encrypted or decrypted data to.
Maybe null
for subsequent initialization by a sub-class.cipher
- The cipher to use for encryption or decryption.
Maybe null
for subsequent initialization by a sub-class.public void close() throws IOException
finish()
gets called and
cipher
gets set to null
upon success.
Next, the DecoratingOutputStream.delegate
gets unconditionally
closed.close
in interface Closeable
close
in interface AutoCloseable
close
in class DecoratingOutputStream
IOException
- On any I/O failure.@OverridingMethodsMustInvokeSuper protected void finish() throws IOException
Note that after a call to this method only close()
may get
called on this cipher output stream
The result of calling any other method (including this one) is undefined!
IOException
- If out or cipher aren't properly initialized,
the stream has been closed, an I/O error occured the cipher
text is invalid, i.e. required padding information is missing.public void write(byte[] buf, int off, int len) throws IOException
write
in class DecoratingOutputStream
buf
- The buffer holding the data to cipher and write.off
- The start offset of the data in the buffer.len
- The number of bytes to cipher and write.IOException
- If out or cipher aren't properly initialized,
the stream has been closed or an I/O error occured.public void write(int b) throws IOException
write
in class DecoratingOutputStream
b
- The byte to cipher and write.IOException
- If out or cipher aren't properly initialized,
the stream has been closed or an I/O error occured.Copyright © 2005–2018 Schlichtherle IT Services. All rights reserved.