jaroslav@682: /*
jaroslav@682: * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
jaroslav@682: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@682: *
jaroslav@682: * This code is free software; you can redistribute it and/or modify it
jaroslav@682: * under the terms of the GNU General Public License version 2 only, as
jaroslav@682: * published by the Free Software Foundation. Oracle designates this
jaroslav@682: * particular file as subject to the "Classpath" exception as provided
jaroslav@682: * by Oracle in the LICENSE file that accompanied this code.
jaroslav@682: *
jaroslav@682: * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@682: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@682: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jaroslav@682: * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@682: * accompanied this code).
jaroslav@682: *
jaroslav@682: * You should have received a copy of the GNU General Public License version
jaroslav@682: * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@682: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@682: *
jaroslav@682: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@682: * or visit www.oracle.com if you need additional information or have any
jaroslav@682: * questions.
jaroslav@682: */
jaroslav@682:
jaroslav@682: package java.io;
jaroslav@682:
jaroslav@682: import java.util.Arrays;
jaroslav@682:
jaroslav@682: /**
jaroslav@682: * This class implements an output stream in which the data is
jaroslav@682: * written into a byte array. The buffer automatically grows as data
jaroslav@682: * is written to it.
jaroslav@682: * The data can be retrieved using toByteArray()
and
jaroslav@682: * toString()
.
jaroslav@682: *
jaroslav@682: * Closing a ByteArrayOutputStream has no effect. The methods in
jaroslav@682: * this class can be called after the stream has been closed without
jaroslav@682: * generating an IOException.
jaroslav@682: *
jaroslav@682: * @author Arthur van Hoff
jaroslav@682: * @since JDK1.0
jaroslav@682: */
jaroslav@682:
jaroslav@682: public class ByteArrayOutputStream extends OutputStream {
jaroslav@682:
jaroslav@682: /**
jaroslav@682: * The buffer where data is stored.
jaroslav@682: */
jaroslav@682: protected byte buf[];
jaroslav@682:
jaroslav@682: /**
jaroslav@682: * The number of valid bytes in the buffer.
jaroslav@682: */
jaroslav@682: protected int count;
jaroslav@682:
jaroslav@682: /**
jaroslav@682: * Creates a new byte array output stream. The buffer capacity is
jaroslav@682: * initially 32 bytes, though its size increases if necessary.
jaroslav@682: */
jaroslav@682: public ByteArrayOutputStream() {
jaroslav@682: this(32);
jaroslav@682: }
jaroslav@682:
jaroslav@682: /**
jaroslav@682: * Creates a new byte array output stream, with a buffer capacity of
jaroslav@682: * the specified size, in bytes.
jaroslav@682: *
jaroslav@682: * @param size the initial size.
jaroslav@682: * @exception IllegalArgumentException if size is negative.
jaroslav@682: */
jaroslav@682: public ByteArrayOutputStream(int size) {
jaroslav@682: if (size < 0) {
jaroslav@682: throw new IllegalArgumentException("Negative initial size: "
jaroslav@682: + size);
jaroslav@682: }
jaroslav@682: buf = new byte[size];
jaroslav@682: }
jaroslav@682:
jaroslav@682: /**
jaroslav@682: * Increases the capacity if necessary to ensure that it can hold
jaroslav@682: * at least the number of elements specified by the minimum
jaroslav@682: * capacity argument.
jaroslav@682: *
jaroslav@682: * @param minCapacity the desired minimum capacity
jaroslav@682: * @throws OutOfMemoryError if {@code minCapacity < 0}. This is
jaroslav@682: * interpreted as a request for the unsatisfiably large capacity
jaroslav@682: * {@code (long) Integer.MAX_VALUE + (minCapacity - Integer.MAX_VALUE)}.
jaroslav@682: */
jaroslav@682: private void ensureCapacity(int minCapacity) {
jaroslav@682: // overflow-conscious code
jaroslav@682: if (minCapacity - buf.length > 0)
jaroslav@682: grow(minCapacity);
jaroslav@682: }
jaroslav@682:
jaroslav@682: /**
jaroslav@682: * Increases the capacity to ensure that it can hold at least the
jaroslav@682: * number of elements specified by the minimum capacity argument.
jaroslav@682: *
jaroslav@682: * @param minCapacity the desired minimum capacity
jaroslav@682: */
jaroslav@682: private void grow(int minCapacity) {
jaroslav@682: // overflow-conscious code
jaroslav@682: int oldCapacity = buf.length;
jaroslav@682: int newCapacity = oldCapacity << 1;
jaroslav@682: if (newCapacity - minCapacity < 0)
jaroslav@682: newCapacity = minCapacity;
jaroslav@682: if (newCapacity < 0) {
jaroslav@682: if (minCapacity < 0) // overflow
jaroslav@682: throw new OutOfMemoryError();
jaroslav@682: newCapacity = Integer.MAX_VALUE;
jaroslav@682: }
jaroslav@682: buf = Arrays.copyOf(buf, newCapacity);
jaroslav@682: }
jaroslav@682:
jaroslav@682: /**
jaroslav@682: * Writes the specified byte to this byte array output stream.
jaroslav@682: *
jaroslav@682: * @param b the byte to be written.
jaroslav@682: */
jaroslav@682: public synchronized void write(int b) {
jaroslav@682: ensureCapacity(count + 1);
jaroslav@682: buf[count] = (byte) b;
jaroslav@682: count += 1;
jaroslav@682: }
jaroslav@682:
jaroslav@682: /**
jaroslav@682: * Writes len
bytes from the specified byte array
jaroslav@682: * starting at offset off
to this byte array output stream.
jaroslav@682: *
jaroslav@682: * @param b the data.
jaroslav@682: * @param off the start offset in the data.
jaroslav@682: * @param len the number of bytes to write.
jaroslav@682: */
jaroslav@682: public synchronized void write(byte b[], int off, int len) {
jaroslav@682: if ((off < 0) || (off > b.length) || (len < 0) ||
jaroslav@682: ((off + len) - b.length > 0)) {
jaroslav@682: throw new IndexOutOfBoundsException();
jaroslav@682: }
jaroslav@682: ensureCapacity(count + len);
jaroslav@682: System.arraycopy(b, off, buf, count, len);
jaroslav@682: count += len;
jaroslav@682: }
jaroslav@682:
jaroslav@682: /**
jaroslav@682: * Writes the complete contents of this byte array output stream to
jaroslav@682: * the specified output stream argument, as if by calling the output
jaroslav@682: * stream's write method using out.write(buf, 0, count)
.
jaroslav@682: *
jaroslav@682: * @param out the output stream to which to write the data.
jaroslav@682: * @exception IOException if an I/O error occurs.
jaroslav@682: */
jaroslav@682: public synchronized void writeTo(OutputStream out) throws IOException {
jaroslav@682: out.write(buf, 0, count);
jaroslav@682: }
jaroslav@682:
jaroslav@682: /**
jaroslav@682: * Resets the count
field of this byte array output
jaroslav@682: * stream to zero, so that all currently accumulated output in the
jaroslav@682: * output stream is discarded. The output stream can be used again,
jaroslav@682: * reusing the already allocated buffer space.
jaroslav@682: *
jaroslav@682: * @see java.io.ByteArrayInputStream#count
jaroslav@682: */
jaroslav@682: public synchronized void reset() {
jaroslav@682: count = 0;
jaroslav@682: }
jaroslav@682:
jaroslav@682: /**
jaroslav@682: * Creates a newly allocated byte array. Its size is the current
jaroslav@682: * size of this output stream and the valid contents of the buffer
jaroslav@682: * have been copied into it.
jaroslav@682: *
jaroslav@682: * @return the current contents of this output stream, as a byte array.
jaroslav@682: * @see java.io.ByteArrayOutputStream#size()
jaroslav@682: */
jaroslav@682: public synchronized byte toByteArray()[] {
jaroslav@682: return Arrays.copyOf(buf, count);
jaroslav@682: }
jaroslav@682:
jaroslav@682: /**
jaroslav@682: * Returns the current size of the buffer.
jaroslav@682: *
jaroslav@682: * @return the value of the count
field, which is the number
jaroslav@682: * of valid bytes in this output stream.
jaroslav@682: * @see java.io.ByteArrayOutputStream#count
jaroslav@682: */
jaroslav@682: public synchronized int size() {
jaroslav@682: return count;
jaroslav@682: }
jaroslav@682:
jaroslav@682: /**
jaroslav@682: * Converts the buffer's contents into a string decoding bytes using the
jaroslav@682: * platform's default character set. The length of the new String
jaroslav@682: * is a function of the character set, and hence may not be equal to the
jaroslav@682: * size of the buffer.
jaroslav@682: *
jaroslav@682: *
This method always replaces malformed-input and unmappable-character jaroslav@682: * sequences with the default replacement string for the platform's jaroslav@682: * default character set. The {@linkplain java.nio.charset.CharsetDecoder} jaroslav@682: * class should be used when more control over the decoding process is jaroslav@682: * required. jaroslav@682: * jaroslav@682: * @return String decoded from the buffer's contents. jaroslav@682: * @since JDK1.1 jaroslav@682: */ jaroslav@682: public synchronized String toString() { jaroslav@682: return new String(buf, 0, count); jaroslav@682: } jaroslav@682: jaroslav@682: /** jaroslav@682: * Converts the buffer's contents into a string by decoding the bytes using jaroslav@682: * the specified {@link java.nio.charset.Charset charsetName}. The length of jaroslav@682: * the new String is a function of the charset, and hence may not be jaroslav@682: * equal to the length of the byte array. jaroslav@682: * jaroslav@682: *
This method always replaces malformed-input and unmappable-character
jaroslav@682: * sequences with this charset's default replacement string. The {@link
jaroslav@682: * java.nio.charset.CharsetDecoder} class should be used when more control
jaroslav@682: * over the decoding process is required.
jaroslav@682: *
jaroslav@682: * @param charsetName the name of a supported
jaroslav@682: * {@linkplain java.nio.charset.Charset charset
jaroslav@682: *
jaroslav@682: */
jaroslav@682: public void close() throws IOException {
jaroslav@682: }
jaroslav@682:
jaroslav@682: }
}
jaroslav@682: * @return String decoded from the buffer's contents.
jaroslav@682: * @exception UnsupportedEncodingException
jaroslav@682: * If the named charset is not supported
jaroslav@682: * @since JDK1.1
jaroslav@682: */
jaroslav@682: public synchronized String toString(String charsetName)
jaroslav@682: throws UnsupportedEncodingException
jaroslav@682: {
jaroslav@682: return new String(buf, 0, count, charsetName);
jaroslav@682: }
jaroslav@682:
jaroslav@682: /**
jaroslav@682: * Creates a newly allocated string. Its size is the current size of
jaroslav@682: * the output stream and the valid contents of the buffer have been
jaroslav@682: * copied into it. Each character c in the resulting string is
jaroslav@682: * constructed from the corresponding element b in the byte
jaroslav@682: * array such that:
jaroslav@682: *
jaroslav@682: *
jaroslav@682: * @deprecated This method does not properly convert bytes into characters.
jaroslav@682: * As of JDK 1.1, the preferred way to do this is via the
jaroslav@682: *
jaroslav@682: * c == (char)(((hibyte & 0xff) << 8) | (b & 0xff))
jaroslav@682: *
toString(String enc)
method, which takes an encoding-name
jaroslav@682: * argument, or the toString()
method, which uses the
jaroslav@682: * platform's default character encoding.
jaroslav@682: *
jaroslav@682: * @param hibyte the high byte of each resulting Unicode character.
jaroslav@682: * @return the current contents of the output stream, as a string.
jaroslav@682: * @see java.io.ByteArrayOutputStream#size()
jaroslav@682: * @see java.io.ByteArrayOutputStream#toString(String)
jaroslav@682: * @see java.io.ByteArrayOutputStream#toString()
jaroslav@682: */
jaroslav@682: @Deprecated
jaroslav@682: public synchronized String toString(int hibyte) {
jaroslav@682: return new String(buf, hibyte, 0, count);
jaroslav@682: }
jaroslav@682:
jaroslav@682: /**
jaroslav@682: * Closing a ByteArrayOutputStream has no effect. The methods in
jaroslav@682: * this class can be called after the stream has been closed without
jaroslav@682: * generating an IOException.
jaroslav@682: *