Package org.apache.commons.crypto.stream
Class CryptoOutputStream
- java.lang.Object
-
- java.io.OutputStream
-
- org.apache.commons.crypto.stream.CryptoOutputStream
-
- All Implemented Interfaces:
java.io.Closeable,java.io.Flushable,java.lang.AutoCloseable,java.nio.channels.Channel,java.nio.channels.WritableByteChannel
- Direct Known Subclasses:
CtrCryptoOutputStream
public class CryptoOutputStream extends java.io.OutputStream implements java.nio.channels.WritableByteChannelCryptoOutputStreamencrypts 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
Fields Modifier and Type Field Description private intbufferSizeThe buffer size.(package private) CryptoCiphercipherthe CryptoCipher instanceprivate booleanclosedFlag to mark whether the output stream is closed.(package private) java.nio.ByteBufferinBufferInput data buffer.(package private) java.security.KeykeyCrypto key for the cipher.private byte[]oneByteBuf(package private) java.nio.ByteBufferoutBufferEncrypted data buffer.(package private) OutputoutputThe output.private java.security.spec.AlgorithmParameterSpecparamsthe algorithm parameters
-
Constructor Summary
Constructors Modifier Constructor Description protectedCryptoOutputStream(java.io.OutputStream outputStream, CryptoCipher cipher, int bufferSize, java.security.Key key, java.security.spec.AlgorithmParameterSpec params)Constructs aCryptoOutputStream.CryptoOutputStream(java.lang.String transformation, java.util.Properties properties, java.io.OutputStream outputStream, java.security.Key key, java.security.spec.AlgorithmParameterSpec params)Constructs aCryptoOutputStream.CryptoOutputStream(java.lang.String transformation, java.util.Properties properties, java.nio.channels.WritableByteChannel out, java.security.Key key, java.security.spec.AlgorithmParameterSpec params)Constructs aCryptoOutputStream.protectedCryptoOutputStream(java.nio.channels.WritableByteChannel channel, CryptoCipher cipher, int bufferSize, java.security.Key key, java.security.spec.AlgorithmParameterSpec params)Constructs aCryptoOutputStream.protectedCryptoOutputStream(Output output, CryptoCipher cipher, int bufferSize, java.security.Key key, java.security.spec.AlgorithmParameterSpec params)Constructs aCryptoOutputStream.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidcheckStream()Checks whether the stream is closed.voidclose()Overrides theOutputStream.close().protected voidencrypt()protected voidencryptFinal()Does final encryption of the last data.voidflush()Overrides theOutputStream.flush().protected voidfreeBuffers()Forcibly free the direct buffers.protected intgetBufferSize()Gets the buffer size.protected CryptoCiphergetCipher()Gets the internal Cipher.protected java.nio.ByteBuffergetInBuffer()Gets the inBuffer.protected java.nio.ByteBuffergetOutBuffer()Gets the outBuffer.protected voidinitCipher()Initializes 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(java.nio.ByteBuffer src)Overrides theWritableByteChannel.write(ByteBuffer).
-
-
-
Field Detail
-
oneByteBuf
private final byte[] oneByteBuf
-
output
final Output output
The output.
-
cipher
final CryptoCipher cipher
the CryptoCipher instance
-
bufferSize
private final int bufferSize
The buffer size.
-
key
final java.security.Key key
Crypto key for the cipher.
-
params
private final java.security.spec.AlgorithmParameterSpec params
the algorithm parameters
-
closed
private boolean closed
Flag to mark whether the output stream is closed.
-
inBuffer
java.nio.ByteBuffer inBuffer
Input data buffer. The data starts at inBuffer.position() and ends at inBuffer.limit().
-
outBuffer
java.nio.ByteBuffer outBuffer
Encrypted data buffer. The data starts at outBuffer.position() and ends at outBuffer.limit().
-
-
Constructor Detail
-
CryptoOutputStream
protected CryptoOutputStream(Output output, CryptoCipher cipher, int bufferSize, java.security.Key key, java.security.spec.AlgorithmParameterSpec params) throws java.io.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:
java.io.IOException- if an I/O error occurs.
-
CryptoOutputStream
protected CryptoOutputStream(java.io.OutputStream outputStream, CryptoCipher cipher, int bufferSize, java.security.Key key, java.security.spec.AlgorithmParameterSpec params) throws java.io.IOExceptionConstructs aCryptoOutputStream.- Parameters:
outputStream- the output stream.cipher- the CryptoCipher instance.bufferSize- the bufferSize.key- crypto key for the cipher.params- the algorithm parameters.- Throws:
java.io.IOException- if an I/O error occurs.
-
CryptoOutputStream
public CryptoOutputStream(java.lang.String transformation, java.util.Properties properties, java.io.OutputStream outputStream, java.security.Key key, java.security.spec.AlgorithmParameterSpec params) throws java.io.IOExceptionConstructs 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:
java.io.IOException- if an I/O error occurs.
-
CryptoOutputStream
public CryptoOutputStream(java.lang.String transformation, java.util.Properties properties, java.nio.channels.WritableByteChannel out, java.security.Key key, java.security.spec.AlgorithmParameterSpec params) throws java.io.IOExceptionConstructs 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:
java.io.IOException- if an I/O error occurs.
-
CryptoOutputStream
protected CryptoOutputStream(java.nio.channels.WritableByteChannel channel, CryptoCipher cipher, int bufferSize, java.security.Key key, java.security.spec.AlgorithmParameterSpec params) throws java.io.IOExceptionConstructs aCryptoOutputStream.- Parameters:
channel- the WritableByteChannel instance.cipher- the cipher instance.bufferSize- the bufferSize.key- crypto key for the cipher.params- the algorithm parameters.- Throws:
java.io.IOException- if an I/O error occurs.
-
-
Method Detail
-
checkStream
protected void checkStream() throws java.io.IOExceptionChecks whether the stream is closed.- Throws:
java.io.IOException- if an I/O error occurs.
-
close
public void close() throws java.io.IOExceptionOverrides theOutputStream.close(). Closes this output stream and releases any system resources associated with this stream.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.nio.channels.Channel- Specified by:
closein interfacejava.io.Closeable- Overrides:
closein classjava.io.OutputStream- Throws:
java.io.IOException- if an I/O error occurs.
-
encrypt
protected void encrypt() throws java.io.IOException- Throws:
java.io.IOException- if an I/O error occurs.
-
encryptFinal
protected void encryptFinal() throws java.io.IOExceptionDoes final encryption of the last data.- Throws:
java.io.IOException- if an I/O error occurs.
-
flush
public void flush() throws java.io.IOExceptionOverrides 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 interfacejava.io.Flushable- Overrides:
flushin classjava.io.OutputStream- Throws:
java.io.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
protected CryptoCipher getCipher()
Gets the internal Cipher.- Returns:
- the cipher instance.
-
getInBuffer
protected java.nio.ByteBuffer getInBuffer()
Gets the inBuffer.- Returns:
- the inBuffer.
-
getOutBuffer
protected java.nio.ByteBuffer getOutBuffer()
Gets the outBuffer.- Returns:
- the outBuffer.
-
initCipher
protected void initCipher() throws java.io.IOExceptionInitializes the cipher.- Throws:
java.io.IOException- if an I/O error occurs.
-
isOpen
public boolean isOpen()
Overrides theChannel.isOpen(). Tells whether or not this channel is open.- Specified by:
isOpenin interfacejava.nio.channels.Channel- Returns:
trueif, and only if, this channel is open
-
write
public void write(byte[] array, int off, int len) throws java.io.IOExceptionOverrides 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 classjava.io.OutputStream- Parameters:
array- the data.off- the start offset in the data.len- the number of bytes to write.- Throws:
java.io.IOException- if an I/O error occurs.
-
write
public int write(java.nio.ByteBuffer src) throws java.io.IOExceptionOverrides theWritableByteChannel.write(ByteBuffer). Writes a sequence of bytes to this channel from the given buffer.- Specified by:
writein interfacejava.nio.channels.WritableByteChannel- Parameters:
src- The buffer from which bytes are to be retrieved.- Returns:
- The number of bytes written, possibly zero.
- Throws:
java.io.IOException- if an I/O error occurs.
-
write
public void write(int b) throws java.io.IOExceptionOverrides theOutputStream.write(byte[]). Writes the specified byte to this output stream.- Specified by:
writein classjava.io.OutputStream- Parameters:
b- the data.- Throws:
java.io.IOException- if an I/O error occurs.
-
-