Package javax.crypto
Class CipherOutputStream
- java.lang.Object
-
- java.io.OutputStream
-
- java.io.FilterOutputStream
-
- javax.crypto.CipherOutputStream
-
- All Implemented Interfaces:
Closeable,Flushable,AutoCloseable
public class CipherOutputStream extends FilterOutputStream
This class wraps an output stream and a cipher so thatwritemethods send the data through the cipher before writing them to the underlying output stream.The cipher must be initialized for the requested operation before being used by a
CipherOutputStream. For example, if a cipher initialized for encryption is used with aCipherOutputStream, theCipherOutputStreamtries to encrypt the data writing it out.
-
-
Field Summary
-
Fields inherited from class java.io.FilterOutputStream
out
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedCipherOutputStream(OutputStream os)Creates a newCipherOutputStreaminstance for anOutputStreamwithout a cipher.CipherOutputStream(OutputStream os, Cipher c)Creates a newCipherOutputStreaminstance for anOutputStreamand aCipher.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Close this cipher output stream.voidflush()Flushes this cipher output stream.voidwrite(byte[] b, int off, int len)Writes thelenbytes from bufferbstarting at offsetoffto this cipher output stream.voidwrite(int b)Writes the single byte to this cipher output stream.-
Methods inherited from class java.io.OutputStream
write
-
-
-
-
Constructor Detail
-
CipherOutputStream
public CipherOutputStream(OutputStream os, Cipher c)
Creates a newCipherOutputStreaminstance for anOutputStreamand aCipher.- Parameters:
os- the output stream to write data to.c- the cipher to process the data with.
-
CipherOutputStream
protected CipherOutputStream(OutputStream os)
Creates a newCipherOutputStreaminstance for anOutputStreamwithout a cipher.A
NullCipheris created to process the data.- Parameters:
os- the output stream to write the data to.
-
-
Method Detail
-
write
public void write(int b) throws IOExceptionWrites the single byte to this cipher output stream.- Overrides:
writein classFilterOutputStream- Parameters:
b- the byte to write.- Throws:
IOException- if an error occurs.
-
write
public void write(byte[] b, int off, int len) throws IOExceptionWrites thelenbytes from bufferbstarting at offsetoffto this cipher output stream.- Overrides:
writein classFilterOutputStream- Parameters:
b- the buffer.off- the offset to start at.len- the number of bytes.- Throws:
IOException- if an error occurs.
-
flush
public void flush() throws IOExceptionFlushes this cipher output stream.- Specified by:
flushin interfaceFlushable- Overrides:
flushin classFilterOutputStream- Throws:
IOException- if an error occurs
-
close
public void close() throws IOExceptionClose this cipher output stream.On the underlying cipher
doFinalwill be invoked, and any buffered bytes from the cipher are also written out, and the cipher is reset to its initial state. The underlying output stream is also closed.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classFilterOutputStream- Throws:
IOException- if an error occurs.
-
-