97 /**
98 * Constructs a CipherOutputStream from an OutputStream without
99 * specifying a Cipher. This has the effect of constructing a
100 * CipherOutputStream using a NullCipher.
101 * <br>Note: if the specified output stream is null, a
102 * NullPointerException may be thrown later when it is used.
103 *
104 * @param os the OutputStream object
105 */
106 protected CipherOutputStream(OutputStream os) {
107 super(os);
108 output = os;
109 cipher = new NullCipher();
110 }
111
112 /**
113 * Writes the specified byte to this output stream.
114 *
115 * @param b the <code>byte</code>.
116 * @exception IOException if an I/O error occurs.
117 * @since JCE1.2
118 */
119 public void write(int b) throws IOException {
120 ibuffer[0] = (byte) b;
121 obuffer = cipher.update(ibuffer, 0, 1);
122 if (obuffer != null) {
123 output.write(obuffer);
124 obuffer = null;
125 }
126 };
127
128 /**
129 * Writes <code>b.length</code> bytes from the specified byte array
130 * to this output stream.
131 * <p>
132 * The <code>write</code> method of
133 * <code>CipherOutputStream</code> calls the <code>write</code>
134 * method of three arguments with the three arguments
135 * <code>b</code>, <code>0</code>, and <code>b.length</code>.
136 *
137 * @param b the data.
138 * @exception NullPointerException if <code>b</code> is null.
139 * @exception IOException if an I/O error occurs.
140 * @see javax.crypto.CipherOutputStream#write(byte[], int, int)
141 * @since JCE1.2
142 */
143 public void write(byte b[]) throws IOException {
144 write(b, 0, b.length);
145 }
146
147 /**
148 * Writes <code>len</code> bytes from the specified byte array
149 * starting at offset <code>off</code> to this output stream.
150 *
151 * @param b the data.
152 * @param off the start offset in the data.
153 * @param len the number of bytes to write.
154 * @exception IOException if an I/O error occurs.
155 * @since JCE1.2
156 */
157 public void write(byte b[], int off, int len) throws IOException {
158 obuffer = cipher.update(b, off, len);
159 if (obuffer != null) {
160 output.write(obuffer);
161 obuffer = null;
162 }
163 }
164
165 /**
166 * Flushes this output stream by forcing any buffered output bytes
167 * that have already been processed by the encapsulated cipher object
168 * to be written out.
169 *
170 * <p>Any bytes buffered by the encapsulated cipher
171 * and waiting to be processed by it will not be written out. For example,
172 * if the encapsulated cipher is a block cipher, and the total number of
173 * bytes written using one of the <code>write</code> methods is less than
174 * the cipher's block size, no bytes will be written out.
175 *
176 * @exception IOException if an I/O error occurs.
177 * @since JCE1.2
178 */
179 public void flush() throws IOException {
180 if (obuffer != null) {
181 output.write(obuffer);
182 obuffer = null;
183 }
184 output.flush();
185 }
186
187 /**
188 * Closes this output stream and releases any system resources
189 * associated with this stream.
190 * <p>
191 * This method invokes the <code>doFinal</code> method of the encapsulated
192 * cipher object, which causes any bytes buffered by the encapsulated
193 * cipher to be processed. The result is written out by calling the
194 * <code>flush</code> method of this output stream.
195 * <p>
196 * This method resets the encapsulated cipher object to its initial state
197 * and calls the <code>close</code> method of the underlying output
198 * stream.
199 *
200 * @exception IOException if an I/O error occurs.
201 * @since JCE1.2
202 */
203 public void close() throws IOException {
204 if (closed) {
205 return;
206 }
207
208 closed = true;
209 try {
210 obuffer = cipher.doFinal();
211 } catch (IllegalBlockSizeException | BadPaddingException e) {
212 obuffer = null;
213 }
214 try {
215 flush();
216 } catch (IOException ignored) {}
217 out.close();
218 }
219 }
|
97 /**
98 * Constructs a CipherOutputStream from an OutputStream without
99 * specifying a Cipher. This has the effect of constructing a
100 * CipherOutputStream using a NullCipher.
101 * <br>Note: if the specified output stream is null, a
102 * NullPointerException may be thrown later when it is used.
103 *
104 * @param os the OutputStream object
105 */
106 protected CipherOutputStream(OutputStream os) {
107 super(os);
108 output = os;
109 cipher = new NullCipher();
110 }
111
112 /**
113 * Writes the specified byte to this output stream.
114 *
115 * @param b the <code>byte</code>.
116 * @exception IOException if an I/O error occurs.
117 */
118 public void write(int b) throws IOException {
119 ibuffer[0] = (byte) b;
120 obuffer = cipher.update(ibuffer, 0, 1);
121 if (obuffer != null) {
122 output.write(obuffer);
123 obuffer = null;
124 }
125 };
126
127 /**
128 * Writes <code>b.length</code> bytes from the specified byte array
129 * to this output stream.
130 * <p>
131 * The <code>write</code> method of
132 * <code>CipherOutputStream</code> calls the <code>write</code>
133 * method of three arguments with the three arguments
134 * <code>b</code>, <code>0</code>, and <code>b.length</code>.
135 *
136 * @param b the data.
137 * @exception NullPointerException if <code>b</code> is null.
138 * @exception IOException if an I/O error occurs.
139 * @see javax.crypto.CipherOutputStream#write(byte[], int, int)
140 */
141 public void write(byte b[]) throws IOException {
142 write(b, 0, b.length);
143 }
144
145 /**
146 * Writes <code>len</code> bytes from the specified byte array
147 * starting at offset <code>off</code> to this output stream.
148 *
149 * @param b the data.
150 * @param off the start offset in the data.
151 * @param len the number of bytes to write.
152 * @exception IOException if an I/O error occurs.
153 */
154 public void write(byte b[], int off, int len) throws IOException {
155 obuffer = cipher.update(b, off, len);
156 if (obuffer != null) {
157 output.write(obuffer);
158 obuffer = null;
159 }
160 }
161
162 /**
163 * Flushes this output stream by forcing any buffered output bytes
164 * that have already been processed by the encapsulated cipher object
165 * to be written out.
166 *
167 * <p>Any bytes buffered by the encapsulated cipher
168 * and waiting to be processed by it will not be written out. For example,
169 * if the encapsulated cipher is a block cipher, and the total number of
170 * bytes written using one of the <code>write</code> methods is less than
171 * the cipher's block size, no bytes will be written out.
172 *
173 * @exception IOException if an I/O error occurs.
174 */
175 public void flush() throws IOException {
176 if (obuffer != null) {
177 output.write(obuffer);
178 obuffer = null;
179 }
180 output.flush();
181 }
182
183 /**
184 * Closes this output stream and releases any system resources
185 * associated with this stream.
186 * <p>
187 * This method invokes the <code>doFinal</code> method of the encapsulated
188 * cipher object, which causes any bytes buffered by the encapsulated
189 * cipher to be processed. The result is written out by calling the
190 * <code>flush</code> method of this output stream.
191 * <p>
192 * This method resets the encapsulated cipher object to its initial state
193 * and calls the <code>close</code> method of the underlying output
194 * stream.
195 *
196 * @exception IOException if an I/O error occurs.
197 */
198 public void close() throws IOException {
199 if (closed) {
200 return;
201 }
202
203 closed = true;
204 try {
205 obuffer = cipher.doFinal();
206 } catch (IllegalBlockSizeException | BadPaddingException e) {
207 obuffer = null;
208 }
209 try {
210 flush();
211 } catch (IOException ignored) {}
212 out.close();
213 }
214 }
|