1 /*
2 * Copyright 1994-2006 Sun Microsystems, Inc. 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. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26 package java.io;
27
28 import java.util.Arrays;
29
30 /**
31 * This class implements an output stream in which the data is
32 * written into a byte array. The buffer automatically grows as data
33 * is written to it.
34 * The data can be retrieved using <code>toByteArray()</code> and
35 * <code>toString()</code>.
36 * <p>
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>.
40 *
41 * @author Arthur van Hoff
42 * @since JDK1.0
43 */
44
45 public class ByteArrayOutputStream extends OutputStream {
46
47 /**
48 * The buffer where data is stored.
49 */
50 protected byte buf[];
51
52 /**
53 * The number of valid bytes in the buffer.
54 */
55 protected int count;
56
57 /**
58 * Creates a new byte array output stream. The buffer capacity is
59 * initially 32 bytes, though its size increases if necessary.
60 */
61 public ByteArrayOutputStream() {
62 this(32);
63 }
64
65 /**
66 * Creates a new byte array output stream, with a buffer capacity of
67 * the specified size, in bytes.
68 *
69 * @param size the initial size.
70 * @exception IllegalArgumentException if size is negative.
71 */
72 public ByteArrayOutputStream(int size) {
73 if (size < 0) {
74 throw new IllegalArgumentException("Negative initial size: "
75 + size);
76 }
77 buf = new byte[size];
78 }
79
80 /**
81 * Writes the specified byte to this byte array output stream.
82 *
83 * @param b the byte to be written.
84 */
85 public synchronized void write(int b) {
86 int newcount = count + 1;
87 if (newcount > buf.length) {
88 buf = Arrays.copyOf(buf, Math.max(buf.length << 1, newcount));
89 }
90 buf[count] = (byte)b;
91 count = newcount;
92 }
93
94 /**
95 * Writes <code>len</code> bytes from the specified byte array
96 * starting at offset <code>off</code> to this byte array output stream.
97 *
98 * @param b the data.
99 * @param off the start offset in the data.
100 * @param len the number of bytes to write.
101 */
102 public synchronized void write(byte b[], int off, int len) {
103 if ((off < 0) || (off > b.length) || (len < 0) ||
104 ((off + len) > b.length) || ((off + len) < 0)) {
105 throw new IndexOutOfBoundsException();
106 } else if (len == 0) {
107 return;
108 }
109 int newcount = count + len;
110 if (newcount > buf.length) {
111 buf = Arrays.copyOf(buf, Math.max(buf.length << 1, newcount));
112 }
113 System.arraycopy(b, off, buf, count, len);
114 count = newcount;
115 }
116
117 /**
118 * Writes the complete contents of this byte array output stream to
119 * the specified output stream argument, as if by calling the output
120 * stream's write method using <code>out.write(buf, 0, count)</code>.
121 *
122 * @param out the output stream to which to write the data.
123 * @exception IOException if an I/O error occurs.
124 */
125 public synchronized void writeTo(OutputStream out) throws IOException {
126 out.write(buf, 0, count);
127 }
128
129 /**
130 * Resets the <code>count</code> field of this byte array output
131 * stream to zero, so that all currently accumulated output in the
132 * output stream is discarded. The output stream can be used again,
133 * reusing the already allocated buffer space.
134 *
135 * @see java.io.ByteArrayInputStream#count
136 */
137 public synchronized void reset() {
138 count = 0;
139 }
140
141 /**
142 * Creates a newly allocated byte array. Its size is the current
143 * size of this output stream and the valid contents of the buffer
144 * have been copied into it.
145 *
146 * @return the current contents of this output stream, as a byte array.
147 * @see java.io.ByteArrayOutputStream#size()
148 */
149 public synchronized byte toByteArray()[] {
150 return Arrays.copyOf(buf, count);
151 }
152
153 /**
154 * Returns the current size of the buffer.
155 *
156 * @return the value of the <code>count</code> field, which is the number
157 * of valid bytes in this output stream.
158 * @see java.io.ByteArrayOutputStream#count
159 */
160 public synchronized int size() {
161 return count;
162 }
163
164 /**
165 * Converts the buffer's contents into a string decoding bytes using the
166 * platform's default character set. The length of the new <tt>String</tt>
167 * is a function of the character set, and hence may not be equal to the
168 * size of the buffer.
169 *
170 * <p> This method always replaces malformed-input and unmappable-character
171 * sequences with the default replacement string for the platform's
172 * default character set. The {@linkplain java.nio.charset.CharsetDecoder}
173 * class should be used when more control over the decoding process is
174 * required.
175 *
176 * @return String decoded from the buffer's contents.
177 * @since JDK1.1
178 */
179 public synchronized String toString() {
180 return new String(buf, 0, count);
181 }
182
183 /**
184 * Converts the buffer's contents into a string by decoding the bytes using
185 * the specified {@link java.nio.charset.Charset charsetName}. The length of
186 * the new <tt>String</tt> is a function of the charset, and hence may not be
187 * equal to the length of the byte array.
188 *
189 * <p> This method always replaces malformed-input and unmappable-character
190 * sequences with this charset's default replacement string. The {@link
191 * java.nio.charset.CharsetDecoder} class should be used when more control
192 * over the decoding process is required.
193 *
194 * @param charsetName the name of a supported
195 * {@linkplain java.nio.charset.Charset </code>charset<code>}
196 * @return String decoded from the buffer's contents.
197 * @exception UnsupportedEncodingException
198 * If the named charset is not supported
199 * @since JDK1.1
200 */
201 public synchronized String toString(String charsetName)
202 throws UnsupportedEncodingException
203 {
204 return new String(buf, 0, count, charsetName);
205 }
206
207 /**
208 * Creates a newly allocated string. Its size is the current size of
209 * the output stream and the valid contents of the buffer have been
210 * copied into it. Each character <i>c</i> in the resulting string is
211 * constructed from the corresponding element <i>b</i> in the byte
212 * array such that:
213 * <blockquote><pre>
214 * c == (char)(((hibyte & 0xff) << 8) | (b & 0xff))
215 * </pre></blockquote>
216 *
217 * @deprecated This method does not properly convert bytes into characters.
218 * As of JDK 1.1, the preferred way to do this is via the
219 * <code>toString(String enc)</code> method, which takes an encoding-name
220 * argument, or the <code>toString()</code> method, which uses the
221 * platform's default character encoding.
222 *
223 * @param hibyte the high byte of each resulting Unicode character.
224 * @return the current contents of the output stream, as a string.
225 * @see java.io.ByteArrayOutputStream#size()
226 * @see java.io.ByteArrayOutputStream#toString(String)
227 * @see java.io.ByteArrayOutputStream#toString()
228 */
229 @Deprecated
230 public synchronized String toString(int hibyte) {
231 return new String(buf, hibyte, 0, count);
232 }
233
234 /**
235 * Closing a <tt>ByteArrayOutputStream</tt> has no effect. The methods in
236 * this class can be called after the stream has been closed without
237 * generating an <tt>IOException</tt>.
238 * <p>
239 *
240 */
241 public void close() throws IOException {
242 }
243
244 }