Moving modules around so the runtime is under one master pom and can be built without building other modules that are in the repository
2 * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.
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
28 import java.util.Arrays;
31 * This class implements an output stream in which the data is
32 * written into a byte array. The buffer automatically grows as data
34 * The data can be retrieved using <code>toByteArray()</code> and
35 * <code>toString()</code>.
37 * Closing a <tt>ByteArrayOutputStream</tt> has no effect. The methods in
38 * this class can be called after the stream has been closed without
39 * generating an <tt>IOException</tt>.
41 * @author Arthur van Hoff
45 public class ByteArrayOutputStream extends OutputStream {
48 * The buffer where data is stored.
53 * The number of valid bytes in the buffer.
58 * Creates a new byte array output stream. The buffer capacity is
59 * initially 32 bytes, though its size increases if necessary.
61 public ByteArrayOutputStream() {
66 * Creates a new byte array output stream, with a buffer capacity of
67 * the specified size, in bytes.
69 * @param size the initial size.
70 * @exception IllegalArgumentException if size is negative.
72 public ByteArrayOutputStream(int size) {
74 throw new IllegalArgumentException("Negative initial size: "
81 * Increases the capacity if necessary to ensure that it can hold
82 * at least the number of elements specified by the minimum
85 * @param minCapacity the desired minimum capacity
86 * @throws OutOfMemoryError if {@code minCapacity < 0}. This is
87 * interpreted as a request for the unsatisfiably large capacity
88 * {@code (long) Integer.MAX_VALUE + (minCapacity - Integer.MAX_VALUE)}.
90 private void ensureCapacity(int minCapacity) {
91 // overflow-conscious code
92 if (minCapacity - buf.length > 0)
97 * Increases the capacity to ensure that it can hold at least the
98 * number of elements specified by the minimum capacity argument.
100 * @param minCapacity the desired minimum capacity
102 private void grow(int minCapacity) {
103 // overflow-conscious code
104 int oldCapacity = buf.length;
105 int newCapacity = oldCapacity << 1;
106 if (newCapacity - minCapacity < 0)
107 newCapacity = minCapacity;
108 if (newCapacity < 0) {
109 if (minCapacity < 0) // overflow
110 throw new OutOfMemoryError();
111 newCapacity = Integer.MAX_VALUE;
113 buf = Arrays.copyOf(buf, newCapacity);
117 * Writes the specified byte to this byte array output stream.
119 * @param b the byte to be written.
121 public synchronized void write(int b) {
122 ensureCapacity(count + 1);
123 buf[count] = (byte) b;
128 * Writes <code>len</code> bytes from the specified byte array
129 * starting at offset <code>off</code> to this byte array output stream.
132 * @param off the start offset in the data.
133 * @param len the number of bytes to write.
135 public synchronized void write(byte b[], int off, int len) {
136 if ((off < 0) || (off > b.length) || (len < 0) ||
137 ((off + len) - b.length > 0)) {
138 throw new IndexOutOfBoundsException();
140 ensureCapacity(count + len);
141 System.arraycopy(b, off, buf, count, len);
146 * Writes the complete contents of this byte array output stream to
147 * the specified output stream argument, as if by calling the output
148 * stream's write method using <code>out.write(buf, 0, count)</code>.
150 * @param out the output stream to which to write the data.
151 * @exception IOException if an I/O error occurs.
153 public synchronized void writeTo(OutputStream out) throws IOException {
154 out.write(buf, 0, count);
158 * Resets the <code>count</code> field of this byte array output
159 * stream to zero, so that all currently accumulated output in the
160 * output stream is discarded. The output stream can be used again,
161 * reusing the already allocated buffer space.
163 * @see java.io.ByteArrayInputStream#count
165 public synchronized void reset() {
170 * Creates a newly allocated byte array. Its size is the current
171 * size of this output stream and the valid contents of the buffer
172 * have been copied into it.
174 * @return the current contents of this output stream, as a byte array.
175 * @see java.io.ByteArrayOutputStream#size()
177 public synchronized byte toByteArray()[] {
178 return Arrays.copyOf(buf, count);
182 * Returns the current size of the buffer.
184 * @return the value of the <code>count</code> field, which is the number
185 * of valid bytes in this output stream.
186 * @see java.io.ByteArrayOutputStream#count
188 public synchronized int size() {
193 * Converts the buffer's contents into a string decoding bytes using the
194 * platform's default character set. The length of the new <tt>String</tt>
195 * is a function of the character set, and hence may not be equal to the
196 * size of the buffer.
198 * <p> This method always replaces malformed-input and unmappable-character
199 * sequences with the default replacement string for the platform's
200 * default character set. The {@linkplain java.nio.charset.CharsetDecoder}
201 * class should be used when more control over the decoding process is
204 * @return String decoded from the buffer's contents.
207 public synchronized String toString() {
208 return new String(buf, 0, count);
212 * Converts the buffer's contents into a string by decoding the bytes using
213 * the specified {@link java.nio.charset.Charset charsetName}. The length of
214 * the new <tt>String</tt> is a function of the charset, and hence may not be
215 * equal to the length of the byte array.
217 * <p> This method always replaces malformed-input and unmappable-character
218 * sequences with this charset's default replacement string. The {@link
219 * java.nio.charset.CharsetDecoder} class should be used when more control
220 * over the decoding process is required.
222 * @param charsetName the name of a supported
223 * {@linkplain java.nio.charset.Charset </code>charset<code>}
224 * @return String decoded from the buffer's contents.
225 * @exception UnsupportedEncodingException
226 * If the named charset is not supported
229 public synchronized String toString(String charsetName)
230 throws UnsupportedEncodingException
232 return new String(buf, 0, count, charsetName);
236 * Creates a newly allocated string. Its size is the current size of
237 * the output stream and the valid contents of the buffer have been
238 * copied into it. Each character <i>c</i> in the resulting string is
239 * constructed from the corresponding element <i>b</i> in the byte
242 * c == (char)(((hibyte & 0xff) << 8) | (b & 0xff))
243 * </pre></blockquote>
245 * @deprecated This method does not properly convert bytes into characters.
246 * As of JDK 1.1, the preferred way to do this is via the
247 * <code>toString(String enc)</code> method, which takes an encoding-name
248 * argument, or the <code>toString()</code> method, which uses the
249 * platform's default character encoding.
251 * @param hibyte the high byte of each resulting Unicode character.
252 * @return the current contents of the output stream, as a string.
253 * @see java.io.ByteArrayOutputStream#size()
254 * @see java.io.ByteArrayOutputStream#toString(String)
255 * @see java.io.ByteArrayOutputStream#toString()
258 public synchronized String toString(int hibyte) {
259 return new String(buf, hibyte, 0, count);
263 * Closing a <tt>ByteArrayOutputStream</tt> has no effect. The methods in
264 * this class can be called after the stream has been closed without
265 * generating an <tt>IOException</tt>.
269 public void close() throws IOException {