1 /*
2 * Copyright (c) 1994, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
52 protected byte buf[];
53
54 /**
55 * The number of valid bytes in the buffer.
56 */
57 protected int count;
58
59 /**
60 * Creates a new byte array output stream. The buffer capacity is
61 * initially 32 bytes, though its size increases if necessary.
62 */
63 public ByteArrayOutputStream() {
64 this(32);
65 }
66
67 /**
68 * Creates a new byte array output stream, with a buffer capacity of
69 * the specified size, in bytes.
70 *
71 * @param size the initial size.
72 * @exception IllegalArgumentException if size is negative.
73 */
74 public ByteArrayOutputStream(int size) {
75 if (size < 0) {
76 throw new IllegalArgumentException("Negative initial size: "
77 + size);
78 }
79 buf = new byte[size];
80 }
81
82 /**
83 * Increases the capacity if necessary to ensure that it can hold
84 * at least the number of elements specified by the minimum
85 * capacity argument.
86 *
87 * @param minCapacity the desired minimum capacity
88 * @throws OutOfMemoryError if {@code minCapacity < 0}. This is
89 * interpreted as a request for the unsatisfiably large capacity
90 * {@code (long) Integer.MAX_VALUE + (minCapacity - Integer.MAX_VALUE)}.
91 */
92 private void ensureCapacity(int minCapacity) {
129 }
130
131 /**
132 * Writes the specified byte to this byte array output stream.
133 *
134 * @param b the byte to be written.
135 */
136 public synchronized void write(int b) {
137 ensureCapacity(count + 1);
138 buf[count] = (byte) b;
139 count += 1;
140 }
141
142 /**
143 * Writes {@code len} bytes from the specified byte array
144 * starting at offset {@code off} to this byte array output stream.
145 *
146 * @param b the data.
147 * @param off the start offset in the data.
148 * @param len the number of bytes to write.
149 */
150 public synchronized void write(byte b[], int off, int len) {
151 Objects.checkFromIndexSize(off, len, b.length);
152 ensureCapacity(count + len);
153 System.arraycopy(b, off, buf, count, len);
154 count += len;
155 }
156
157 /**
158 * Writes the complete contents of this byte array output stream to
159 * the specified output stream argument, as if by calling the output
160 * stream's write method using {@code out.write(buf, 0, count)}.
161 *
162 * @param out the output stream to which to write the data.
163 * @exception IOException if an I/O error occurs.
164 */
165 public synchronized void writeTo(OutputStream out) throws IOException {
166 out.write(buf, 0, count);
167 }
168
169 /**
170 * Resets the {@code count} field of this byte array output
171 * stream to zero, so that all currently accumulated output in the
172 * output stream is discarded. The output stream can be used again,
173 * reusing the already allocated buffer space.
174 *
175 * @see java.io.ByteArrayInputStream#count
176 */
177 public synchronized void reset() {
178 count = 0;
179 }
180
181 /**
182 * Creates a newly allocated byte array. Its size is the current
183 * size of this output stream and the valid contents of the buffer
230 * <p> An invocation of this method of the form
231 *
232 * <pre> {@code
233 * ByteArrayOutputStream b = ...
234 * b.toString("UTF-8")
235 * }
236 * </pre>
237 *
238 * behaves in exactly the same way as the expression
239 *
240 * <pre> {@code
241 * ByteArrayOutputStream b = ...
242 * b.toString(StandardCharsets.UTF_8)
243 * }
244 * </pre>
245 *
246 *
247 * @param charsetName the name of a supported
248 * {@link java.nio.charset.Charset charset}
249 * @return String decoded from the buffer's contents.
250 * @exception UnsupportedEncodingException
251 * If the named charset is not supported
252 * @since 1.1
253 */
254 public synchronized String toString(String charsetName)
255 throws UnsupportedEncodingException
256 {
257 return new String(buf, 0, count, charsetName);
258 }
259
260 /**
261 * Converts the buffer's contents into a string by decoding the bytes using
262 * the specified {@link java.nio.charset.Charset charset}. The length of the new
263 * {@code String} is a function of the charset, and hence may not be equal
264 * to the length of the byte array.
265 *
266 * <p> This method always replaces malformed-input and unmappable-character
267 * sequences with the charset's default replacement string. The {@link
268 * java.nio.charset.CharsetDecoder} class should be used when more control
269 * over the decoding process is required.
270 *
|
1 /*
2 * Copyright (c) 1994, 2018, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
52 protected byte buf[];
53
54 /**
55 * The number of valid bytes in the buffer.
56 */
57 protected int count;
58
59 /**
60 * Creates a new byte array output stream. The buffer capacity is
61 * initially 32 bytes, though its size increases if necessary.
62 */
63 public ByteArrayOutputStream() {
64 this(32);
65 }
66
67 /**
68 * Creates a new byte array output stream, with a buffer capacity of
69 * the specified size, in bytes.
70 *
71 * @param size the initial size.
72 * @throws IllegalArgumentException if size is negative.
73 */
74 public ByteArrayOutputStream(int size) {
75 if (size < 0) {
76 throw new IllegalArgumentException("Negative initial size: "
77 + size);
78 }
79 buf = new byte[size];
80 }
81
82 /**
83 * Increases the capacity if necessary to ensure that it can hold
84 * at least the number of elements specified by the minimum
85 * capacity argument.
86 *
87 * @param minCapacity the desired minimum capacity
88 * @throws OutOfMemoryError if {@code minCapacity < 0}. This is
89 * interpreted as a request for the unsatisfiably large capacity
90 * {@code (long) Integer.MAX_VALUE + (minCapacity - Integer.MAX_VALUE)}.
91 */
92 private void ensureCapacity(int minCapacity) {
129 }
130
131 /**
132 * Writes the specified byte to this byte array output stream.
133 *
134 * @param b the byte to be written.
135 */
136 public synchronized void write(int b) {
137 ensureCapacity(count + 1);
138 buf[count] = (byte) b;
139 count += 1;
140 }
141
142 /**
143 * Writes {@code len} bytes from the specified byte array
144 * starting at offset {@code off} to this byte array output stream.
145 *
146 * @param b the data.
147 * @param off the start offset in the data.
148 * @param len the number of bytes to write.
149 * @throws NullPointerException if {@code b} is {@code null}.
150 * @throws IndexOutOfBoundsException if {@code off} is negative,
151 * {@code len} is negative, or {@code len} is greater than
152 * {@code b.length - off}
153 */
154 public synchronized void write(byte b[], int off, int len) {
155 Objects.checkFromIndexSize(off, len, b.length);
156 ensureCapacity(count + len);
157 System.arraycopy(b, off, buf, count, len);
158 count += len;
159 }
160
161 /**
162 * Writes the complete contents of the specified byte array
163 * to this byte array output stream.
164 *
165 * <p> This method is equivalent to {@link #write(byte[],int,int)
166 * write(b,0,b.length)}.
167 *
168 * @param b the data.
169 * @throws NullPointerException if {@code b} is {@code null}.
170 * @since 11
171 */
172 public void writeBytes(byte b[]) {
173 write(b, 0, b.length);
174 }
175
176 /**
177 * Writes the complete contents of this byte array output stream to
178 * the specified output stream argument, as if by calling the output
179 * stream's write method using {@code out.write(buf, 0, count)}.
180 *
181 * @param out the output stream to which to write the data.
182 * @throws NullPointerException if {@code out} is {@code null}.
183 * @throws IOException if an I/O error occurs.
184 */
185 public synchronized void writeTo(OutputStream out) throws IOException {
186 out.write(buf, 0, count);
187 }
188
189 /**
190 * Resets the {@code count} field of this byte array output
191 * stream to zero, so that all currently accumulated output in the
192 * output stream is discarded. The output stream can be used again,
193 * reusing the already allocated buffer space.
194 *
195 * @see java.io.ByteArrayInputStream#count
196 */
197 public synchronized void reset() {
198 count = 0;
199 }
200
201 /**
202 * Creates a newly allocated byte array. Its size is the current
203 * size of this output stream and the valid contents of the buffer
250 * <p> An invocation of this method of the form
251 *
252 * <pre> {@code
253 * ByteArrayOutputStream b = ...
254 * b.toString("UTF-8")
255 * }
256 * </pre>
257 *
258 * behaves in exactly the same way as the expression
259 *
260 * <pre> {@code
261 * ByteArrayOutputStream b = ...
262 * b.toString(StandardCharsets.UTF_8)
263 * }
264 * </pre>
265 *
266 *
267 * @param charsetName the name of a supported
268 * {@link java.nio.charset.Charset charset}
269 * @return String decoded from the buffer's contents.
270 * @throws UnsupportedEncodingException
271 * If the named charset is not supported
272 * @since 1.1
273 */
274 public synchronized String toString(String charsetName)
275 throws UnsupportedEncodingException
276 {
277 return new String(buf, 0, count, charsetName);
278 }
279
280 /**
281 * Converts the buffer's contents into a string by decoding the bytes using
282 * the specified {@link java.nio.charset.Charset charset}. The length of the new
283 * {@code String} is a function of the charset, and hence may not be equal
284 * to the length of the byte array.
285 *
286 * <p> This method always replaces malformed-input and unmappable-character
287 * sequences with the charset's default replacement string. The {@link
288 * java.nio.charset.CharsetDecoder} class should be used when more control
289 * over the decoding process is required.
290 *
|