Package org.apache.commons.crypto.stream
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 int
The buffer size.(package private) final CryptoCipher
the CryptoCipher instanceprivate boolean
Flag to mark whether the output stream is closed.(package private) ByteBuffer
Input data buffer.(package private) final Key
Crypto key for the cipher.private final byte[]
(package private) ByteBuffer
Encrypted data buffer.(package private) final Output
The output.private final AlgorithmParameterSpec
the algorithm parameters -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
CryptoOutputStream
(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
.protected
CryptoOutputStream
(WritableByteChannel channel, CryptoCipher cipher, int bufferSize, Key key, AlgorithmParameterSpec params) Constructs aCryptoOutputStream
.protected
CryptoOutputStream
(Output output, CryptoCipher cipher, int bufferSize, Key key, AlgorithmParameterSpec params) Constructs aCryptoOutputStream
. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
Checks whether the stream is closed.void
close()
Overrides theOutputStream.close()
.protected void
encrypt()
protected void
Does final encryption of the last data.void
flush()
Overrides theOutputStream.flush()
.protected void
Forcibly free the direct buffers.protected int
Gets the buffer size.protected CryptoCipher
Gets the internal Cipher.protected ByteBuffer
Gets the inBuffer.protected ByteBuffer
Gets the outBuffer.protected void
Initializes the cipher.boolean
isOpen()
Overrides theChannel.isOpen()
.void
write
(byte[] array, int off, int len) Overrides theOutputStream.write(byte[], int, int)
.void
write
(int b) Overrides theOutputStream.write(byte[])
.int
write
(ByteBuffer src) Overrides theWritableByteChannel.write(ByteBuffer)
.Methods inherited from class java.io.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
- TheProperties
class 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
- TheProperties
class 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:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceChannel
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in 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:
flush
in interfaceFlushable
- Overrides:
flush
in 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
Gets the internal Cipher.- Returns:
- the cipher instance.
-
getInBuffer
Gets the inBuffer.- Returns:
- the inBuffer.
-
getOutBuffer
Gets the outBuffer.- Returns:
- the outBuffer.
-
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. IfinBuffer
is full, then do encryption and write data to the underlying stream.- Overrides:
write
in 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:
write
in 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:
write
in classOutputStream
- Parameters:
b
- the data.- Throws:
IOException
- if an I/O error occurs.
-