3
import java.io.OutputStream;
4
import java.io.IOException;
5
import java.io.FilterOutputStream;
8
* A CipherOutputStream is composed of an OutputStream and a Cipher so
9
* that write() methods first process the data before writing them out
10
* to the underlying OutputStream. The cipher must be fully
11
* initialized before being used by a CipherOutputStream.
13
* For example, if the cipher is initialized for encryption, the
14
* CipherOutputStream will attempt to encrypt data before writing out the
17
* This class adheres strictly to the semantics, especially the
18
* failure semantics, of its ancestor classes
19
* java.io.OutputStream and java.io.FilterOutputStream. This class
20
* has exactly those methods specified in its ancestor classes, and
21
* overrides them all. Moreover, this class catches all exceptions
22
* that are not thrown by its ancestor classes.
24
* It is crucial for a programmer using this class not to use
25
* methods that are not defined or overriden in this class (such as a
26
* new method or constructor that is later added to one of the super
27
* classes), because the design and implementation of those methods
28
* are unlikely to have considered security impact with regard to
33
* @see FilterOutputStream
35
* @see CipherInputStream
37
public class CipherOutputStream
38
extends FilterOutputStream
42
private byte[] oneByte = new byte[1];
45
* Constructs a CipherOutputStream from an OutputStream and a
48
public CipherOutputStream(
57
* Constructs a CipherOutputStream from an OutputStream without
58
* specifying a Cipher. This has the effect of constructing a
59
* CipherOutputStream using a NullCipher.
61
protected CipherOutputStream(
64
this(os, new NullCipher());
68
* Writes the specified byte to this output stream.
70
* @param b the <code>byte</code>.
71
* @exception IOException if an I/O error occurs.
80
byte[] bytes = c.update(oneByte, 0, 1);
84
out.write(bytes, 0, bytes.length);
89
* Writes <code>b.length</code> bytes from the specified byte array
90
* to this output stream.
92
* The <code>write</code> method of
93
* <code>CipherOutputStream</code> calls the <code>write</code>
94
* method of three arguments with the three arguments
95
* <code>b</code>, <code>0</code>, and <code>b.length</code>.
98
* @exception IOException if an I/O error occurs.
100
* @see #write(byte[], int, int)
106
write(b, 0, b.length);
110
* Writes <code>len</code> bytes from the specified byte array
111
* starting at offset <code>off</code> to this output stream.
114
* @param off the start offset in the data.
115
* @param len the number of bytes to write.
116
* @exception IOException if an I/O error occurs.
125
byte[] bytes = c.update(b, off, len);
129
out.write(bytes, 0, bytes.length);
134
* Flushes this output stream by forcing any buffered output bytes
135
* that have already been processed by the encapsulated cipher object
139
* Any bytes buffered by the encapsulated cipher
140
* and waiting to be processed by it will not be written out. For example,
141
* if the encapsulated cipher is a block cipher, and the total number of
142
* bytes written using one of the <code>write</code> methods is less than
143
* the cipher's block size, no bytes will be written out.
145
* @exception IOException if an I/O error occurs.
155
* Closes this output stream and releases any system resources
156
* associated with this stream.
158
* This method invokes the <code>doFinal</code> method of the encapsulated
159
* cipher object, which causes any bytes buffered by the encapsulated
160
* cipher to be processed. The result is written out by calling the
161
* <code>flush</code> method of this output stream.
163
* This method resets the encapsulated cipher object to its initial state
164
* and calls the <code>close</code> method of the underlying output
167
* @exception IOException if an I/O error occurs.
175
byte[] bytes = c.doFinal();
179
out.write(bytes, 0, bytes.length);
184
throw new IOException("Error closing stream: " + e.toString());