1 /* |
1 /* |
2 * Copyright (c) 1994, 2017, Oracle and/or its affiliates. All rights reserved. |
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. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
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 |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. Oracle designates this |
7 * published by the Free Software Foundation. Oracle designates this |
55 * The number of valid bytes in the buffer. |
55 * The number of valid bytes in the buffer. |
56 */ |
56 */ |
57 protected int count; |
57 protected int count; |
58 |
58 |
59 /** |
59 /** |
60 * Creates a new byte array output stream. The buffer capacity is |
60 * Creates a new {@code ByteArrayOutputStream}. The buffer capacity is |
61 * initially 32 bytes, though its size increases if necessary. |
61 * initially 32 bytes, though its size increases if necessary. |
62 */ |
62 */ |
63 public ByteArrayOutputStream() { |
63 public ByteArrayOutputStream() { |
64 this(32); |
64 this(32); |
65 } |
65 } |
66 |
66 |
67 /** |
67 /** |
68 * Creates a new byte array output stream, with a buffer capacity of |
68 * Creates a new {@code ByteArrayOutputStream}, with a buffer capacity of |
69 * the specified size, in bytes. |
69 * the specified size, in bytes. |
70 * |
70 * |
71 * @param size the initial size. |
71 * @param size the initial size. |
72 * @exception IllegalArgumentException if size is negative. |
72 * @throws IllegalArgumentException if size is negative. |
73 */ |
73 */ |
74 public ByteArrayOutputStream(int size) { |
74 public ByteArrayOutputStream(int size) { |
75 if (size < 0) { |
75 if (size < 0) { |
76 throw new IllegalArgumentException("Negative initial size: " |
76 throw new IllegalArgumentException("Negative initial size: " |
77 + size); |
77 + size); |
82 /** |
82 /** |
83 * Increases the capacity if necessary to ensure that it can hold |
83 * Increases the capacity if necessary to ensure that it can hold |
84 * at least the number of elements specified by the minimum |
84 * at least the number of elements specified by the minimum |
85 * capacity argument. |
85 * capacity argument. |
86 * |
86 * |
87 * @param minCapacity the desired minimum capacity |
87 * @param minCapacity the desired minimum capacity |
88 * @throws OutOfMemoryError if {@code minCapacity < 0}. This is |
88 * @throws OutOfMemoryError if {@code minCapacity < 0}. This is |
89 * interpreted as a request for the unsatisfiably large capacity |
89 * interpreted as a request for the unsatisfiably large capacity |
90 * {@code (long) Integer.MAX_VALUE + (minCapacity - Integer.MAX_VALUE)}. |
90 * {@code (long) Integer.MAX_VALUE + (minCapacity - Integer.MAX_VALUE)}. |
91 */ |
91 */ |
92 private void ensureCapacity(int minCapacity) { |
92 private void ensureCapacity(int minCapacity) { |
139 count += 1; |
139 count += 1; |
140 } |
140 } |
141 |
141 |
142 /** |
142 /** |
143 * Writes {@code len} bytes from the specified byte array |
143 * Writes {@code len} bytes from the specified byte array |
144 * starting at offset {@code off} to this byte array output stream. |
144 * starting at offset {@code off} to this {@code ByteArrayOutputStream}. |
145 * |
145 * |
146 * @param b the data. |
146 * @param b the data. |
147 * @param off the start offset in the data. |
147 * @param off the start offset in the data. |
148 * @param len the number of bytes to write. |
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} |
149 */ |
153 */ |
150 public synchronized void write(byte b[], int off, int len) { |
154 public synchronized void write(byte b[], int off, int len) { |
151 Objects.checkFromIndexSize(off, len, b.length); |
155 Objects.checkFromIndexSize(off, len, b.length); |
152 ensureCapacity(count + len); |
156 ensureCapacity(count + len); |
153 System.arraycopy(b, off, buf, count, len); |
157 System.arraycopy(b, off, buf, count, len); |
154 count += len; |
158 count += len; |
155 } |
159 } |
156 |
160 |
157 /** |
161 /** |
158 * Writes the complete contents of this byte array output stream to |
162 * Writes the complete contents of the specified byte array |
|
163 * to this {@code ByteArrayOutputStream}. |
|
164 * |
|
165 * @apiNote |
|
166 * This method is equivalent to {@link #write(byte[],int,int) |
|
167 * write(b, 0, b.length)}. |
|
168 * |
|
169 * @param b the data. |
|
170 * @throws NullPointerException if {@code b} is {@code null}. |
|
171 * @since 11 |
|
172 */ |
|
173 public void writeBytes(byte b[]) { |
|
174 write(b, 0, b.length); |
|
175 } |
|
176 |
|
177 /** |
|
178 * Writes the complete contents of this {@code ByteArrayOutputStream} to |
159 * the specified output stream argument, as if by calling the output |
179 * the specified output stream argument, as if by calling the output |
160 * stream's write method using {@code out.write(buf, 0, count)}. |
180 * stream's write method using {@code out.write(buf, 0, count)}. |
161 * |
181 * |
162 * @param out the output stream to which to write the data. |
182 * @param out the output stream to which to write the data. |
163 * @exception IOException if an I/O error occurs. |
183 * @throws NullPointerException if {@code out} is {@code null}. |
|
184 * @throws IOException if an I/O error occurs. |
164 */ |
185 */ |
165 public synchronized void writeTo(OutputStream out) throws IOException { |
186 public synchronized void writeTo(OutputStream out) throws IOException { |
166 out.write(buf, 0, count); |
187 out.write(buf, 0, count); |
167 } |
188 } |
168 |
189 |
169 /** |
190 /** |
170 * Resets the {@code count} field of this byte array output |
191 * Resets the {@code count} field of this {@code ByteArrayOutputStream} |
171 * stream to zero, so that all currently accumulated output in the |
192 * to zero, so that all currently accumulated output in the |
172 * output stream is discarded. The output stream can be used again, |
193 * output stream is discarded. The output stream can be used again, |
173 * reusing the already allocated buffer space. |
194 * reusing the already allocated buffer space. |
174 * |
195 * |
175 * @see java.io.ByteArrayInputStream#count |
196 * @see java.io.ByteArrayInputStream#count |
176 */ |
197 */ |
242 * b.toString(StandardCharsets.UTF_8) |
263 * b.toString(StandardCharsets.UTF_8) |
243 * } |
264 * } |
244 * </pre> |
265 * </pre> |
245 * |
266 * |
246 * |
267 * |
247 * @param charsetName the name of a supported |
268 * @param charsetName the name of a supported |
248 * {@link java.nio.charset.Charset charset} |
269 * {@link java.nio.charset.Charset charset} |
249 * @return String decoded from the buffer's contents. |
270 * @return String decoded from the buffer's contents. |
250 * @exception UnsupportedEncodingException |
271 * @throws UnsupportedEncodingException |
251 * If the named charset is not supported |
272 * If the named charset is not supported |
252 * @since 1.1 |
273 * @since 1.1 |
253 */ |
274 */ |
254 public synchronized String toString(String charsetName) |
275 public synchronized String toString(String charsetName) |
255 throws UnsupportedEncodingException |
276 throws UnsupportedEncodingException |
256 { |
277 { |
257 return new String(buf, 0, count, charsetName); |
278 return new String(buf, 0, count, charsetName); |