public class CryptoOutputStream extends OutputStream implements 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.
Modifier | Constructor and Description |
---|---|
protected |
CryptoOutputStream(Output output,
CryptoCipher cipher,
int bufferSize,
Key key,
AlgorithmParameterSpec params)
Constructs a
CryptoOutputStream . |
protected |
CryptoOutputStream(OutputStream outputStream,
CryptoCipher cipher,
int bufferSize,
Key key,
AlgorithmParameterSpec params)
Constructs a
CryptoOutputStream . |
|
CryptoOutputStream(String transformation,
Properties properties,
OutputStream outputStream,
Key key,
AlgorithmParameterSpec params)
Constructs a
CryptoOutputStream . |
|
CryptoOutputStream(String transformation,
Properties properties,
WritableByteChannel out,
Key key,
AlgorithmParameterSpec params)
Constructs a
CryptoOutputStream . |
protected |
CryptoOutputStream(WritableByteChannel channel,
CryptoCipher cipher,
int bufferSize,
Key key,
AlgorithmParameterSpec params)
Constructs a
CryptoOutputStream . |
Modifier and Type | Method and Description |
---|---|
protected void |
checkStream()
Checks whether the stream is closed.
|
void |
close()
Overrides the
OutputStream.close() . |
protected void |
encrypt()
|
protected void |
encryptFinal()
Does final encryption of the last data.
|
void |
flush()
Overrides the
OutputStream.flush() . |
protected void |
freeBuffers()
Forcibly free the direct buffers.
|
protected int |
getBufferSize()
Gets the buffer size.
|
protected CryptoCipher |
getCipher()
Gets the internal Cipher.
|
protected ByteBuffer |
getInBuffer()
Gets the inBuffer.
|
protected ByteBuffer |
getOutBuffer()
Gets the outBuffer.
|
protected void |
initCipher()
Initializes the cipher.
|
boolean |
isOpen()
Overrides the
Channel.isOpen() . |
void |
write(byte[] array,
int off,
int len)
Overrides the
OutputStream.write(byte[], int, int) . |
int |
write(ByteBuffer src)
Overrides the
WritableByteChannel.write(ByteBuffer) . |
void |
write(int b)
Overrides the
OutputStream.write(byte[]) . |
write
protected CryptoOutputStream(Output output, CryptoCipher cipher, int bufferSize, Key key, AlgorithmParameterSpec params) throws IOException
CryptoOutputStream
.output
- the output stream.cipher
- the CryptoCipher instance.bufferSize
- the bufferSize.key
- crypto key for the cipher.params
- the algorithm parameters.IOException
- if an I/O error occurs.protected CryptoOutputStream(OutputStream outputStream, CryptoCipher cipher, int bufferSize, Key key, AlgorithmParameterSpec params) throws IOException
CryptoOutputStream
.outputStream
- the output stream.cipher
- the CryptoCipher instance.bufferSize
- the bufferSize.key
- crypto key for the cipher.params
- the algorithm parameters.IOException
- if an I/O error occurs.public CryptoOutputStream(String transformation, Properties properties, OutputStream outputStream, Key key, AlgorithmParameterSpec params) throws IOException
CryptoOutputStream
.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
- The Properties
class represents a set of
properties.outputStream
- the output stream.key
- crypto key for the cipher.params
- the algorithm parameters.IOException
- if an I/O error occurs.public CryptoOutputStream(String transformation, Properties properties, WritableByteChannel out, Key key, AlgorithmParameterSpec params) throws IOException
CryptoOutputStream
.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
- The Properties
class represents a set of
properties.out
- the WritableByteChannel instance.key
- crypto key for the cipher.params
- the algorithm parameters.IOException
- if an I/O error occurs.protected CryptoOutputStream(WritableByteChannel channel, CryptoCipher cipher, int bufferSize, Key key, AlgorithmParameterSpec params) throws IOException
CryptoOutputStream
.channel
- the WritableByteChannel instance.cipher
- the cipher instance.bufferSize
- the bufferSize.key
- crypto key for the cipher.params
- the algorithm parameters.IOException
- if an I/O error occurs.protected void checkStream() throws IOException
IOException
- if an I/O error occurs.public void close() throws IOException
OutputStream.close()
. Closes this output stream and
releases any system resources associated with this stream.close
in interface Closeable
close
in interface AutoCloseable
close
in interface Channel
close
in class OutputStream
IOException
- if an I/O error occurs.protected void encrypt() throws IOException
IOException
- if an I/O error occurs.protected void encryptFinal() throws IOException
IOException
- if an I/O error occurs.public void flush() throws IOException
OutputStream.flush()
. To flush, we need to encrypt
the data in the buffer and write to the underlying stream, then do the
flush.flush
in interface Flushable
flush
in class OutputStream
IOException
- if an I/O error occurs.protected void freeBuffers()
protected int getBufferSize()
protected CryptoCipher getCipher()
protected ByteBuffer getInBuffer()
protected ByteBuffer getOutBuffer()
protected void initCipher() throws IOException
IOException
- if an I/O error occurs.public boolean isOpen()
Channel.isOpen()
. Tells whether or not this channel
is open.public void write(byte[] array, int off, int len) throws IOException
OutputStream.write(byte[], int, int)
.
Encryption is buffer based. If there is enough room in inBuffer
,
then write to this buffer. If inBuffer
is full, then do
encryption and write data to the underlying stream.write
in class OutputStream
array
- the data.off
- the start offset in the data.len
- the number of bytes to write.IOException
- if an I/O error occurs.public int write(ByteBuffer src) throws IOException
WritableByteChannel.write(ByteBuffer)
. Writes a
sequence of bytes to this channel from the given buffer.write
in interface WritableByteChannel
src
- The buffer from which bytes are to be retrieved.IOException
- if an I/O error occurs.public void write(int b) throws IOException
OutputStream.write(byte[])
. Writes the
specified byte to this output stream.write
in class OutputStream
b
- the data.IOException
- if an I/O error occurs.Copyright © 2016–2022 The Apache Software Foundation. All rights reserved.