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.WritableByteChannel
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
Fields Modifier and Type Field Description private int
bufferSize
The buffer size.(package private) CryptoCipher
cipher
the CryptoCipher instanceprivate boolean
closed
Flag to mark whether the output stream is closed.(package private) java.nio.ByteBuffer
inBuffer
Input data buffer.(package private) java.security.Key
key
Crypto key for the cipher.private byte[]
oneByteBuf
(package private) java.nio.ByteBuffer
outBuffer
Encrypted data buffer.(package private) Output
output
The output.private java.security.spec.AlgorithmParameterSpec
params
the algorithm parameters
-
Constructor Summary
Constructors Modifier Constructor Description protected
CryptoOutputStream(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
.protected
CryptoOutputStream(java.nio.channels.WritableByteChannel channel, CryptoCipher cipher, int bufferSize, java.security.Key key, java.security.spec.AlgorithmParameterSpec params)
Constructs aCryptoOutputStream
.protected
CryptoOutputStream(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 void
checkStream()
Checks whether the stream is closed.void
close()
Overrides theOutputStream.close()
.protected void
encrypt()
protected void
encryptFinal()
Does final encryption of the last data.void
flush()
Overrides theOutputStream.flush()
.protected void
freeBuffers()
Forcibly free the direct buffers.protected int
getBufferSize()
Gets the buffer size.protected CryptoCipher
getCipher()
Gets the internal Cipher.protected java.nio.ByteBuffer
getInBuffer()
Gets the inBuffer.protected java.nio.ByteBuffer
getOutBuffer()
Gets the outBuffer.protected void
initCipher()
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(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.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:
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.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:
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.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:
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.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:
java.io.IOException
- if an I/O error occurs.
-
-
Method Detail
-
checkStream
protected void checkStream() throws java.io.IOException
Checks whether the stream is closed.- Throws:
java.io.IOException
- if an I/O error occurs.
-
close
public void close() throws java.io.IOException
Overrides theOutputStream.close()
. Closes this output stream and releases any system resources associated with this stream.- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.nio.channels.Channel
- Specified by:
close
in interfacejava.io.Closeable
- Overrides:
close
in 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.IOException
Does final encryption of the last data.- Throws:
java.io.IOException
- if an I/O error occurs.
-
flush
public void flush() throws java.io.IOException
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 interfacejava.io.Flushable
- Overrides:
flush
in 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.IOException
Initializes 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:
isOpen
in interfacejava.nio.channels.Channel
- Returns:
true
if, and only if, this channel is open
-
write
public void write(byte[] array, int off, int len) throws java.io.IOException
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 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.IOException
Overrides theWritableByteChannel.write(ByteBuffer)
. Writes a sequence of bytes to this channel from the given buffer.- Specified by:
write
in 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.IOException
Overrides theOutputStream.write(byte[])
. Writes the specified byte to this output stream.- Specified by:
write
in classjava.io.OutputStream
- Parameters:
b
- the data.- Throws:
java.io.IOException
- if an I/O error occurs.
-
-