Class CryptoOutputStream
java.lang.Object
java.io.OutputStream
org.apache.commons.crypto.stream.CryptoOutputStream
- All Implemented Interfaces:
Closeable, Flushable, AutoCloseable, Channel, WritableByteChannel
- Direct Known Subclasses:
CtrCryptoOutputStream
CryptoOutputStream encrypts data and writes to the under layer
output. It supports any mode of operations such as AES CBC/CTR/GCM mode in
concept. It is not thread-safe.
This class should only be used with blocking sinks. Using this class to wrap a non-blocking sink may lead to high CPU usage.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final intThe buffer size.(package private) final CryptoCipherthe CryptoCipher instanceprivate booleanFlag to mark whether the output stream is closed.(package private) ByteBufferInput data buffer.(package private) final KeyCrypto key for the cipher.private final byte[](package private) ByteBufferEncrypted data buffer.(package private) final OutputThe output.private final AlgorithmParameterSpecthe algorithm parameters -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedCryptoOutputStream(OutputStream outputStream, CryptoCipher cipher, int bufferSize, Key key, AlgorithmParameterSpec params) Constructs aCryptoOutputStream.CryptoOutputStream(String transformation, Properties properties, OutputStream outputStream, Key key, AlgorithmParameterSpec params) Constructs aCryptoOutputStream.CryptoOutputStream(String transformation, Properties properties, WritableByteChannel out, Key key, AlgorithmParameterSpec params) Constructs aCryptoOutputStream.protectedCryptoOutputStream(WritableByteChannel channel, CryptoCipher cipher, int bufferSize, Key key, AlgorithmParameterSpec params) Constructs aCryptoOutputStream.protectedCryptoOutputStream(Output output, CryptoCipher cipher, int bufferSize, Key key, AlgorithmParameterSpec params) Constructs aCryptoOutputStream. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidChecks whether the stream is closed.voidclose()Overrides theOutputStream.close().protected voidencrypt()protected voidDoes final encryption of the last data.voidflush()Overrides theOutputStream.flush().protected voidForcibly free the direct buffers.protected intGets the buffer size.protected CryptoCipherGets the internal Cipher.protected ByteBufferGets the inBuffer.protected ByteBufferGets the outBuffer.protected voidInitializes the cipher.booleanisOpen()Overrides theChannel.isOpen().voidwrite(byte[] array, int off, int len) Overrides theOutputStream.write(byte[], int, int).voidwrite(int b) Overrides theOutputStream.write(byte[]).intwrite(ByteBuffer src) Overrides theWritableByteChannel.write(ByteBuffer).Methods inherited from class OutputStream
write
-
Field Details
-
oneByteBuf
private final byte[] oneByteBuf -
output
The output. -
cipher
the CryptoCipher instance -
bufferSize
private final int bufferSizeThe buffer size. -
key
Crypto key for the cipher. -
params
the algorithm parameters -
closed
private boolean closedFlag to mark whether the output stream is closed. -
inBuffer
ByteBuffer inBufferInput data buffer. The data starts at inBuffer.position() and ends at inBuffer.limit(). -
outBuffer
ByteBuffer outBufferEncrypted data buffer. The data starts at outBuffer.position() and ends at outBuffer.limit().
-
-
Constructor Details
-
CryptoOutputStream
protected CryptoOutputStream(Output output, CryptoCipher cipher, int bufferSize, Key key, AlgorithmParameterSpec params) throws IOException Constructs aCryptoOutputStream.- Parameters:
output- the output stream.cipher- the CryptoCipher instance.bufferSize- the bufferSize.key- crypto key for the cipher.params- the algorithm parameters.- Throws:
IOException- if an I/O error occurs.
-
CryptoOutputStream
protected CryptoOutputStream(OutputStream outputStream, CryptoCipher cipher, int bufferSize, Key key, AlgorithmParameterSpec params) throws IOException Constructs aCryptoOutputStream.- Parameters:
outputStream- the output stream.cipher- the CryptoCipher instance.bufferSize- the bufferSize.key- crypto key for the cipher.params- the algorithm parameters.- Throws:
IOException- if an I/O error occurs.
-
CryptoOutputStream
public CryptoOutputStream(String transformation, Properties properties, OutputStream outputStream, Key key, AlgorithmParameterSpec params) throws IOException Constructs aCryptoOutputStream.- Parameters:
transformation- the name of the transformation, e.g., AES/CBC/PKCS5Padding. See the Java Cryptography Architecture Standard Algorithm Name Documentation for information about standard transformation names.properties- ThePropertiesclass represents a set of properties.outputStream- the output stream.key- crypto key for the cipher.params- the algorithm parameters.- Throws:
IOException- if an I/O error occurs.
-
CryptoOutputStream
public CryptoOutputStream(String transformation, Properties properties, WritableByteChannel out, Key key, AlgorithmParameterSpec params) throws IOException Constructs aCryptoOutputStream.- Parameters:
transformation- the name of the transformation, e.g., AES/CBC/PKCS5Padding. See the Java Cryptography Architecture Standard Algorithm Name Documentation for information about standard transformation names.properties- ThePropertiesclass represents a set of properties.out- the WritableByteChannel instance.key- crypto key for the cipher.params- the algorithm parameters.- Throws:
IOException- if an I/O error occurs.
-
CryptoOutputStream
protected CryptoOutputStream(WritableByteChannel channel, CryptoCipher cipher, int bufferSize, Key key, AlgorithmParameterSpec params) throws IOException Constructs aCryptoOutputStream.- Parameters:
channel- the WritableByteChannel instance.cipher- the cipher instance.bufferSize- the bufferSize.key- crypto key for the cipher.params- the algorithm parameters.- Throws:
IOException- if an I/O error occurs.
-
-
Method Details
-
checkStream
Checks whether the stream is closed.- Throws:
IOException- if an I/O error occurs.
-
close
Overrides theOutputStream.close(). Closes this output stream and releases any system resources associated with this stream.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceChannel- Specified by:
closein interfaceCloseable- Overrides:
closein classOutputStream- Throws:
IOException- if an I/O error occurs.
-
encrypt
- Throws:
IOException- if an I/O error occurs.
-
encryptFinal
Does final encryption of the last data.- Throws:
IOException- if an I/O error occurs.
-
flush
Overrides theOutputStream.flush(). To flush, we need to encrypt the data in the buffer and write to the underlying stream, then do the flush.- Specified by:
flushin interfaceFlushable- Overrides:
flushin classOutputStream- Throws:
IOException- if an I/O error occurs.
-
freeBuffers
protected void freeBuffers()Forcibly free the direct buffers. -
getBufferSize
protected int getBufferSize()Gets the buffer size.- Returns:
- the buffer size.
-
getCipher
-
getInBuffer
-
getOutBuffer
-
initCipher
Initializes the cipher.- Throws:
IOException- if an I/O error occurs.
-
isOpen
public boolean isOpen()Overrides theChannel.isOpen(). Tells whether or not this channel is open. -
write
Overrides theOutputStream.write(byte[], int, int). Encryption is buffer based. If there is enough room ininBuffer, then write to this buffer. IfinBufferis full, then do encryption and write data to the underlying stream.- Overrides:
writein classOutputStream- Parameters:
array- the data.off- the start offset in the data.len- the number of bytes to write.- Throws:
IOException- if an I/O error occurs.
-
write
Overrides theWritableByteChannel.write(ByteBuffer). Writes a sequence of bytes to this channel from the given buffer.- Specified by:
writein interfaceWritableByteChannel- Parameters:
src- The buffer from which bytes are to be retrieved.- Returns:
- The number of bytes written, possibly zero.
- Throws:
IOException- if an I/O error occurs.
-
write
Overrides theOutputStream.write(byte[]). Writes the specified byte to this output stream.- Specified by:
writein classOutputStream- Parameters:
b- the data.- Throws:
IOException- if an I/O error occurs.
-