Class CryptoOutputStream<K extends MasterKey<K>>
- Type Parameters:
K
- The type ofMasterKey
s used to manipulate the data.
- All Implemented Interfaces:
Closeable
,Flushable
,AutoCloseable
The CryptoOutputStream wraps a provided OutputStream object and performs cryptographic transformation of the bytes written to it. The transformed bytes are then written to the wrapped OutputStream. It uses the cryptography handler provided during construction to invoke methods that perform the cryptographic transformations.
In short, writing to the CryptoOutputStream results in those bytes being cryptographically transformed and written to the wrapped OutputStream.
For example, if the crypto handler provides methods for decryption, the CryptoOutputStream will decrypt the provided ciphertext bytes and write the plaintext bytes to the wrapped OutputStream.
This class adheres strictly to the semantics, especially the failure semantics, of its ancestor class java.io.OutputStream. This class overrides all the methods specified in its ancestor class.
To instantiate an instance of this class, please see AwsCrypto
.
-
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Closes this output stream and releases any system resources associated with this stream.Returns the result of the cryptographic operations including associate metadata.void
setMaxInputLength(long size)
Sets an upper bound on the size of the input data.void
write(byte[] b)
void
write(byte[] b, int off, int len)
void
write(int b)
Methods inherited from class java.io.OutputStream
flush, nullOutputStream
-
Method Details
-
write
- Overrides:
write
in classOutputStream
- Throws:
BadCiphertextException
- This is thrown only during decryption if b contains invalid or corrupt ciphertext.IllegalArgumentException
IOException
-
write
public void write(byte[] b, int off, int len) throws IllegalArgumentException, IOException, BadCiphertextException- Overrides:
write
in classOutputStream
- Throws:
BadCiphertextException
- This is thrown only during decryption if b contains invalid or corrupt ciphertext.IllegalArgumentException
IOException
-
write
- Specified by:
write
in classOutputStream
- Throws:
BadCiphertextException
- This is thrown only during decryption if b contains invalid or corrupt ciphertext.IOException
-
close
Closes this output stream and releases any system resources associated with this stream.This method writes any final bytes to the underlying stream that complete the cyptographic transformation of the written bytes. It also calls close on the wrapped OutputStream.
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classOutputStream
- Throws:
IOException
- if an I/O error occurs.BadCiphertextException
- This is thrown only during decryption if b contains invalid or corrupt ciphertext.
-
setMaxInputLength
public void setMaxInputLength(long size)Sets an upper bound on the size of the input data. This method should be called before writing any data to the stream. If this method is not called prior to writing data, performance may be reduced (notably, it will not be possible to cache data keys when encrypting).Among other things, this size is used to enforce limits configured on the
CachingCryptoMaterialsManager
.If the size set here is exceeded, an exception will be thrown, and the encyption or decryption will fail.
If this method is called multiple times, the smallest bound will be used.
- Parameters:
size
- Maximum input size.
-
getCryptoResult
Returns the result of the cryptographic operations including associate metadata.
-