001 /*
002 * Copyright 1994-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package java.io;
027
028 import java.util.Arrays;
029
030 /**
031 * This class implements an output stream in which the data is
032 * written into a byte array. The buffer automatically grows as data
033 * is written to it.
034 * The data can be retrieved using <code>toByteArray()</code> and
035 * <code>toString()</code>.
036 * <p>
037 * Closing a <tt>ByteArrayOutputStream</tt> has no effect. The methods in
038 * this class can be called after the stream has been closed without
039 * generating an <tt>IOException</tt>.
040 *
041 * @author Arthur van Hoff
042 * @version 1.59, 05/05/07
043 * @since JDK1.0
044 */
045
046 public class ByteArrayOutputStream extends OutputStream {
047
048 /**
049 * The buffer where data is stored.
050 */
051 protected byte buf[];
052
053 /**
054 * The number of valid bytes in the buffer.
055 */
056 protected int count;
057
058 /**
059 * Creates a new byte array output stream. The buffer capacity is
060 * initially 32 bytes, though its size increases if necessary.
061 */
062 public ByteArrayOutputStream() {
063 this (32);
064 }
065
066 /**
067 * Creates a new byte array output stream, with a buffer capacity of
068 * the specified size, in bytes.
069 *
070 * @param size the initial size.
071 * @exception IllegalArgumentException if size is negative.
072 */
073 public ByteArrayOutputStream(int size) {
074 if (size < 0) {
075 throw new IllegalArgumentException(
076 "Negative initial size: " + size);
077 }
078 buf = new byte[size];
079 }
080
081 /**
082 * Writes the specified byte to this byte array output stream.
083 *
084 * @param b the byte to be written.
085 */
086 public synchronized void write(int b) {
087 int newcount = count + 1;
088 if (newcount > buf.length) {
089 buf = Arrays.copyOf(buf, Math
090 .max(buf.length << 1, newcount));
091 }
092 buf[count] = (byte) b;
093 count = newcount;
094 }
095
096 /**
097 * Writes <code>len</code> bytes from the specified byte array
098 * starting at offset <code>off</code> to this byte array output stream.
099 *
100 * @param b the data.
101 * @param off the start offset in the data.
102 * @param len the number of bytes to write.
103 */
104 public synchronized void write(byte b[], int off, int len) {
105 if ((off < 0) || (off > b.length) || (len < 0)
106 || ((off + len) > b.length) || ((off + len) < 0)) {
107 throw new IndexOutOfBoundsException();
108 } else if (len == 0) {
109 return;
110 }
111 int newcount = count + len;
112 if (newcount > buf.length) {
113 buf = Arrays.copyOf(buf, Math
114 .max(buf.length << 1, newcount));
115 }
116 System.arraycopy(b, off, buf, count, len);
117 count = newcount;
118 }
119
120 /**
121 * Writes the complete contents of this byte array output stream to
122 * the specified output stream argument, as if by calling the output
123 * stream's write method using <code>out.write(buf, 0, count)</code>.
124 *
125 * @param out the output stream to which to write the data.
126 * @exception IOException if an I/O error occurs.
127 */
128 public synchronized void writeTo(OutputStream out)
129 throws IOException {
130 out.write(buf, 0, count);
131 }
132
133 /**
134 * Resets the <code>count</code> field of this byte array output
135 * stream to zero, so that all currently accumulated output in the
136 * output stream is discarded. The output stream can be used again,
137 * reusing the already allocated buffer space.
138 *
139 * @see java.io.ByteArrayInputStream#count
140 */
141 public synchronized void reset() {
142 count = 0;
143 }
144
145 /**
146 * Creates a newly allocated byte array. Its size is the current
147 * size of this output stream and the valid contents of the buffer
148 * have been copied into it.
149 *
150 * @return the current contents of this output stream, as a byte array.
151 * @see java.io.ByteArrayOutputStream#size()
152 */
153 public synchronized byte toByteArray()[] {
154 return Arrays.copyOf(buf, count);
155 }
156
157 /**
158 * Returns the current size of the buffer.
159 *
160 * @return the value of the <code>count</code> field, which is the number
161 * of valid bytes in this output stream.
162 * @see java.io.ByteArrayOutputStream#count
163 */
164 public synchronized int size() {
165 return count;
166 }
167
168 /**
169 * Converts the buffer's contents into a string decoding bytes using the
170 * platform's default character set. The length of the new <tt>String</tt>
171 * is a function of the character set, and hence may not be equal to the
172 * size of the buffer.
173 *
174 * <p> This method always replaces malformed-input and unmappable-character
175 * sequences with the default replacement string for the platform's
176 * default character set. The {@linkplain java.nio.charset.CharsetDecoder}
177 * class should be used when more control over the decoding process is
178 * required.
179 *
180 * @return String decoded from the buffer's contents.
181 * @since JDK1.1
182 */
183 public synchronized String toString() {
184 return new String(buf, 0, count);
185 }
186
187 /**
188 * Converts the buffer's contents into a string by decoding the bytes using
189 * the specified {@link java.nio.charset.Charset charsetName}. The length of
190 * the new <tt>String</tt> is a function of the charset, and hence may not be
191 * equal to the length of the byte array.
192 *
193 * <p> This method always replaces malformed-input and unmappable-character
194 * sequences with this charset's default replacement string. The {@link
195 * java.nio.charset.CharsetDecoder} class should be used when more control
196 * over the decoding process is required.
197 *
198 * @param charsetName the name of a supported
199 * {@linkplain java.nio.charset.Charset </code>charset<code>}
200 * @return String decoded from the buffer's contents.
201 * @exception UnsupportedEncodingException
202 * If the named charset is not supported
203 * @since JDK1.1
204 */
205 public synchronized String toString(String charsetName)
206 throws UnsupportedEncodingException {
207 return new String(buf, 0, count, charsetName);
208 }
209
210 /**
211 * Creates a newly allocated string. Its size is the current size of
212 * the output stream and the valid contents of the buffer have been
213 * copied into it. Each character <i>c</i> in the resulting string is
214 * constructed from the corresponding element <i>b</i> in the byte
215 * array such that:
216 * <blockquote><pre>
217 * c == (char)(((hibyte & 0xff) << 8) | (b & 0xff))
218 * </pre></blockquote>
219 *
220 * @deprecated This method does not properly convert bytes into characters.
221 * As of JDK 1.1, the preferred way to do this is via the
222 * <code>toString(String enc)</code> method, which takes an encoding-name
223 * argument, or the <code>toString()</code> method, which uses the
224 * platform's default character encoding.
225 *
226 * @param hibyte the high byte of each resulting Unicode character.
227 * @return the current contents of the output stream, as a string.
228 * @see java.io.ByteArrayOutputStream#size()
229 * @see java.io.ByteArrayOutputStream#toString(String)
230 * @see java.io.ByteArrayOutputStream#toString()
231 */
232 @Deprecated
233 public synchronized String toString(int hibyte) {
234 return new String(buf, hibyte, 0, count);
235 }
236
237 /**
238 * Closing a <tt>ByteArrayOutputStream</tt> has no effect. The methods in
239 * this class can be called after the stream has been closed without
240 * generating an <tt>IOException</tt>.
241 * <p>
242 *
243 */
244 public void close() throws IOException {
245 }
246
247 }
|