public class CipherOutputStream
extends java.io.FilterOutputStream
This class extends the java.io.FilterOutputStream class for combining the functionality
of a OutputStream and a Cipher.
According to the operation mode the Cipher has been initialized with, data is
en/decrypted before writing it to the underlying stream when calling one of the
write()
methods.
This class works similar than javax.crypto.CipherOutputStream
except
for that method close()
throws an (IO)Exception when internally
calling cipher.doFinal()
causes an Exception (whereas
javax.crypto.CipherOutputStream
does not throw an exception
of cipher.doFinal
fails.
OutputStream
,
FilterOutputStream
,
Cipher
,
CipherInputStream
Constructor and Description |
---|
CipherOutputStream(java.io.OutputStream os)
Creates a CipherOutputStream only from an OutputStream.
|
CipherOutputStream(java.io.OutputStream os,
javax.crypto.Cipher cipher)
Creates a CipherOutputStream using an OutputStream and a Cipher initialized
either for encryption or decryption.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Calls the cipher for the last time and closes the output stream.
|
void |
write(byte[] b)
Writes the bytes from the given byte array to this output stream.
|
void |
write(byte[] b,
int off,
int len)
Writes a specified number of bytes from a given position of the given
byte array to this output stream.
|
void |
write(int b)
Writes a byte to the underlying output stream.
|
public CipherOutputStream(java.io.OutputStream os, javax.crypto.Cipher cipher)
os
- the output stream.cipher
- an initialized cipher.public CipherOutputStream(java.io.OutputStream os)
os
- the output streampublic void write(int b) throws java.io.IOException
write
in class java.io.FilterOutputStream
b
- the byte to be written to this streamjava.io.IOException
- if an I/O error occurspublic void write(byte[] b) throws java.io.IOException
write
in class java.io.FilterOutputStream
b
- the byte array containing the data to be written to this streamjava.io.IOException
- if an I/O error occurs.public void write(byte[] b, int off, int len) throws java.io.IOException
write
in class java.io.FilterOutputStream
b
- the byte array containing the data to be written to this streamoff
- the start offset indicating the position within the given byte array b
from which the data is to be taken for en/decrypting and writing it to the streamlen
- the number of bytes to write this streamjava.io.IOException
- if an I/O error occurspublic void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in class java.io.FilterOutputStream
java.io.IOException
- if an I/O error occurs