emul/compact/src/main/java/java/io/ByteArrayOutputStream.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 06 Feb 2013 15:07:20 +0100
branchjdk7-b147
changeset 682 5d25a1df3540
permissions -rw-r--r--
More useful classes
jaroslav@682
     1
/*
jaroslav@682
     2
 * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
jaroslav@682
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@682
     4
 *
jaroslav@682
     5
 * This code is free software; you can redistribute it and/or modify it
jaroslav@682
     6
 * under the terms of the GNU General Public License version 2 only, as
jaroslav@682
     7
 * published by the Free Software Foundation.  Oracle designates this
jaroslav@682
     8
 * particular file as subject to the "Classpath" exception as provided
jaroslav@682
     9
 * by Oracle in the LICENSE file that accompanied this code.
jaroslav@682
    10
 *
jaroslav@682
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@682
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@682
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jaroslav@682
    14
 * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@682
    15
 * accompanied this code).
jaroslav@682
    16
 *
jaroslav@682
    17
 * You should have received a copy of the GNU General Public License version
jaroslav@682
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@682
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@682
    20
 *
jaroslav@682
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@682
    22
 * or visit www.oracle.com if you need additional information or have any
jaroslav@682
    23
 * questions.
jaroslav@682
    24
 */
jaroslav@682
    25
jaroslav@682
    26
package java.io;
jaroslav@682
    27
jaroslav@682
    28
import java.util.Arrays;
jaroslav@682
    29
jaroslav@682
    30
/**
jaroslav@682
    31
 * This class implements an output stream in which the data is
jaroslav@682
    32
 * written into a byte array. The buffer automatically grows as data
jaroslav@682
    33
 * is written to it.
jaroslav@682
    34
 * The data can be retrieved using <code>toByteArray()</code> and
jaroslav@682
    35
 * <code>toString()</code>.
jaroslav@682
    36
 * <p>
jaroslav@682
    37
 * Closing a <tt>ByteArrayOutputStream</tt> has no effect. The methods in
jaroslav@682
    38
 * this class can be called after the stream has been closed without
jaroslav@682
    39
 * generating an <tt>IOException</tt>.
jaroslav@682
    40
 *
jaroslav@682
    41
 * @author  Arthur van Hoff
jaroslav@682
    42
 * @since   JDK1.0
jaroslav@682
    43
 */
jaroslav@682
    44
jaroslav@682
    45
public class ByteArrayOutputStream extends OutputStream {
jaroslav@682
    46
jaroslav@682
    47
    /**
jaroslav@682
    48
     * The buffer where data is stored.
jaroslav@682
    49
     */
jaroslav@682
    50
    protected byte buf[];
jaroslav@682
    51
jaroslav@682
    52
    /**
jaroslav@682
    53
     * The number of valid bytes in the buffer.
jaroslav@682
    54
     */
jaroslav@682
    55
    protected int count;
jaroslav@682
    56
jaroslav@682
    57
    /**
jaroslav@682
    58
     * Creates a new byte array output stream. The buffer capacity is
jaroslav@682
    59
     * initially 32 bytes, though its size increases if necessary.
jaroslav@682
    60
     */
jaroslav@682
    61
    public ByteArrayOutputStream() {
jaroslav@682
    62
        this(32);
jaroslav@682
    63
    }
jaroslav@682
    64
jaroslav@682
    65
    /**
jaroslav@682
    66
     * Creates a new byte array output stream, with a buffer capacity of
jaroslav@682
    67
     * the specified size, in bytes.
jaroslav@682
    68
     *
jaroslav@682
    69
     * @param   size   the initial size.
jaroslav@682
    70
     * @exception  IllegalArgumentException if size is negative.
jaroslav@682
    71
     */
jaroslav@682
    72
    public ByteArrayOutputStream(int size) {
jaroslav@682
    73
        if (size < 0) {
jaroslav@682
    74
            throw new IllegalArgumentException("Negative initial size: "
jaroslav@682
    75
                                               + size);
jaroslav@682
    76
        }
jaroslav@682
    77
        buf = new byte[size];
jaroslav@682
    78
    }
jaroslav@682
    79
jaroslav@682
    80
    /**
jaroslav@682
    81
     * Increases the capacity if necessary to ensure that it can hold
jaroslav@682
    82
     * at least the number of elements specified by the minimum
jaroslav@682
    83
     * capacity argument.
jaroslav@682
    84
     *
jaroslav@682
    85
     * @param minCapacity the desired minimum capacity
jaroslav@682
    86
     * @throws OutOfMemoryError if {@code minCapacity < 0}.  This is
jaroslav@682
    87
     * interpreted as a request for the unsatisfiably large capacity
jaroslav@682
    88
     * {@code (long) Integer.MAX_VALUE + (minCapacity - Integer.MAX_VALUE)}.
jaroslav@682
    89
     */
jaroslav@682
    90
    private void ensureCapacity(int minCapacity) {
jaroslav@682
    91
        // overflow-conscious code
jaroslav@682
    92
        if (minCapacity - buf.length > 0)
jaroslav@682
    93
            grow(minCapacity);
jaroslav@682
    94
    }
jaroslav@682
    95
jaroslav@682
    96
    /**
jaroslav@682
    97
     * Increases the capacity to ensure that it can hold at least the
jaroslav@682
    98
     * number of elements specified by the minimum capacity argument.
jaroslav@682
    99
     *
jaroslav@682
   100
     * @param minCapacity the desired minimum capacity
jaroslav@682
   101
     */
jaroslav@682
   102
    private void grow(int minCapacity) {
jaroslav@682
   103
        // overflow-conscious code
jaroslav@682
   104
        int oldCapacity = buf.length;
jaroslav@682
   105
        int newCapacity = oldCapacity << 1;
jaroslav@682
   106
        if (newCapacity - minCapacity < 0)
jaroslav@682
   107
            newCapacity = minCapacity;
jaroslav@682
   108
        if (newCapacity < 0) {
jaroslav@682
   109
            if (minCapacity < 0) // overflow
jaroslav@682
   110
                throw new OutOfMemoryError();
jaroslav@682
   111
            newCapacity = Integer.MAX_VALUE;
jaroslav@682
   112
        }
jaroslav@682
   113
        buf = Arrays.copyOf(buf, newCapacity);
jaroslav@682
   114
    }
jaroslav@682
   115
jaroslav@682
   116
    /**
jaroslav@682
   117
     * Writes the specified byte to this byte array output stream.
jaroslav@682
   118
     *
jaroslav@682
   119
     * @param   b   the byte to be written.
jaroslav@682
   120
     */
jaroslav@682
   121
    public synchronized void write(int b) {
jaroslav@682
   122
        ensureCapacity(count + 1);
jaroslav@682
   123
        buf[count] = (byte) b;
jaroslav@682
   124
        count += 1;
jaroslav@682
   125
    }
jaroslav@682
   126
jaroslav@682
   127
    /**
jaroslav@682
   128
     * Writes <code>len</code> bytes from the specified byte array
jaroslav@682
   129
     * starting at offset <code>off</code> to this byte array output stream.
jaroslav@682
   130
     *
jaroslav@682
   131
     * @param   b     the data.
jaroslav@682
   132
     * @param   off   the start offset in the data.
jaroslav@682
   133
     * @param   len   the number of bytes to write.
jaroslav@682
   134
     */
jaroslav@682
   135
    public synchronized void write(byte b[], int off, int len) {
jaroslav@682
   136
        if ((off < 0) || (off > b.length) || (len < 0) ||
jaroslav@682
   137
            ((off + len) - b.length > 0)) {
jaroslav@682
   138
            throw new IndexOutOfBoundsException();
jaroslav@682
   139
        }
jaroslav@682
   140
        ensureCapacity(count + len);
jaroslav@682
   141
        System.arraycopy(b, off, buf, count, len);
jaroslav@682
   142
        count += len;
jaroslav@682
   143
    }
jaroslav@682
   144
jaroslav@682
   145
    /**
jaroslav@682
   146
     * Writes the complete contents of this byte array output stream to
jaroslav@682
   147
     * the specified output stream argument, as if by calling the output
jaroslav@682
   148
     * stream's write method using <code>out.write(buf, 0, count)</code>.
jaroslav@682
   149
     *
jaroslav@682
   150
     * @param      out   the output stream to which to write the data.
jaroslav@682
   151
     * @exception  IOException  if an I/O error occurs.
jaroslav@682
   152
     */
jaroslav@682
   153
    public synchronized void writeTo(OutputStream out) throws IOException {
jaroslav@682
   154
        out.write(buf, 0, count);
jaroslav@682
   155
    }
jaroslav@682
   156
jaroslav@682
   157
    /**
jaroslav@682
   158
     * Resets the <code>count</code> field of this byte array output
jaroslav@682
   159
     * stream to zero, so that all currently accumulated output in the
jaroslav@682
   160
     * output stream is discarded. The output stream can be used again,
jaroslav@682
   161
     * reusing the already allocated buffer space.
jaroslav@682
   162
     *
jaroslav@682
   163
     * @see     java.io.ByteArrayInputStream#count
jaroslav@682
   164
     */
jaroslav@682
   165
    public synchronized void reset() {
jaroslav@682
   166
        count = 0;
jaroslav@682
   167
    }
jaroslav@682
   168
jaroslav@682
   169
    /**
jaroslav@682
   170
     * Creates a newly allocated byte array. Its size is the current
jaroslav@682
   171
     * size of this output stream and the valid contents of the buffer
jaroslav@682
   172
     * have been copied into it.
jaroslav@682
   173
     *
jaroslav@682
   174
     * @return  the current contents of this output stream, as a byte array.
jaroslav@682
   175
     * @see     java.io.ByteArrayOutputStream#size()
jaroslav@682
   176
     */
jaroslav@682
   177
    public synchronized byte toByteArray()[] {
jaroslav@682
   178
        return Arrays.copyOf(buf, count);
jaroslav@682
   179
    }
jaroslav@682
   180
jaroslav@682
   181
    /**
jaroslav@682
   182
     * Returns the current size of the buffer.
jaroslav@682
   183
     *
jaroslav@682
   184
     * @return  the value of the <code>count</code> field, which is the number
jaroslav@682
   185
     *          of valid bytes in this output stream.
jaroslav@682
   186
     * @see     java.io.ByteArrayOutputStream#count
jaroslav@682
   187
     */
jaroslav@682
   188
    public synchronized int size() {
jaroslav@682
   189
        return count;
jaroslav@682
   190
    }
jaroslav@682
   191
jaroslav@682
   192
    /**
jaroslav@682
   193
     * Converts the buffer's contents into a string decoding bytes using the
jaroslav@682
   194
     * platform's default character set. The length of the new <tt>String</tt>
jaroslav@682
   195
     * is a function of the character set, and hence may not be equal to the
jaroslav@682
   196
     * size of the buffer.
jaroslav@682
   197
     *
jaroslav@682
   198
     * <p> This method always replaces malformed-input and unmappable-character
jaroslav@682
   199
     * sequences with the default replacement string for the platform's
jaroslav@682
   200
     * default character set. The {@linkplain java.nio.charset.CharsetDecoder}
jaroslav@682
   201
     * class should be used when more control over the decoding process is
jaroslav@682
   202
     * required.
jaroslav@682
   203
     *
jaroslav@682
   204
     * @return String decoded from the buffer's contents.
jaroslav@682
   205
     * @since  JDK1.1
jaroslav@682
   206
     */
jaroslav@682
   207
    public synchronized String toString() {
jaroslav@682
   208
        return new String(buf, 0, count);
jaroslav@682
   209
    }
jaroslav@682
   210
jaroslav@682
   211
    /**
jaroslav@682
   212
     * Converts the buffer's contents into a string by decoding the bytes using
jaroslav@682
   213
     * the specified {@link java.nio.charset.Charset charsetName}. The length of
jaroslav@682
   214
     * the new <tt>String</tt> is a function of the charset, and hence may not be
jaroslav@682
   215
     * equal to the length of the byte array.
jaroslav@682
   216
     *
jaroslav@682
   217
     * <p> This method always replaces malformed-input and unmappable-character
jaroslav@682
   218
     * sequences with this charset's default replacement string. The {@link
jaroslav@682
   219
     * java.nio.charset.CharsetDecoder} class should be used when more control
jaroslav@682
   220
     * over the decoding process is required.
jaroslav@682
   221
     *
jaroslav@682
   222
     * @param  charsetName  the name of a supported
jaroslav@682
   223
     *              {@linkplain java.nio.charset.Charset </code>charset<code>}
jaroslav@682
   224
     * @return String decoded from the buffer's contents.
jaroslav@682
   225
     * @exception  UnsupportedEncodingException
jaroslav@682
   226
     *             If the named charset is not supported
jaroslav@682
   227
     * @since   JDK1.1
jaroslav@682
   228
     */
jaroslav@682
   229
    public synchronized String toString(String charsetName)
jaroslav@682
   230
        throws UnsupportedEncodingException
jaroslav@682
   231
    {
jaroslav@682
   232
        return new String(buf, 0, count, charsetName);
jaroslav@682
   233
    }
jaroslav@682
   234
jaroslav@682
   235
    /**
jaroslav@682
   236
     * Creates a newly allocated string. Its size is the current size of
jaroslav@682
   237
     * the output stream and the valid contents of the buffer have been
jaroslav@682
   238
     * copied into it. Each character <i>c</i> in the resulting string is
jaroslav@682
   239
     * constructed from the corresponding element <i>b</i> in the byte
jaroslav@682
   240
     * array such that:
jaroslav@682
   241
     * <blockquote><pre>
jaroslav@682
   242
     *     c == (char)(((hibyte &amp; 0xff) &lt;&lt; 8) | (b &amp; 0xff))
jaroslav@682
   243
     * </pre></blockquote>
jaroslav@682
   244
     *
jaroslav@682
   245
     * @deprecated This method does not properly convert bytes into characters.
jaroslav@682
   246
     * As of JDK&nbsp;1.1, the preferred way to do this is via the
jaroslav@682
   247
     * <code>toString(String enc)</code> method, which takes an encoding-name
jaroslav@682
   248
     * argument, or the <code>toString()</code> method, which uses the
jaroslav@682
   249
     * platform's default character encoding.
jaroslav@682
   250
     *
jaroslav@682
   251
     * @param      hibyte    the high byte of each resulting Unicode character.
jaroslav@682
   252
     * @return     the current contents of the output stream, as a string.
jaroslav@682
   253
     * @see        java.io.ByteArrayOutputStream#size()
jaroslav@682
   254
     * @see        java.io.ByteArrayOutputStream#toString(String)
jaroslav@682
   255
     * @see        java.io.ByteArrayOutputStream#toString()
jaroslav@682
   256
     */
jaroslav@682
   257
    @Deprecated
jaroslav@682
   258
    public synchronized String toString(int hibyte) {
jaroslav@682
   259
        return new String(buf, hibyte, 0, count);
jaroslav@682
   260
    }
jaroslav@682
   261
jaroslav@682
   262
    /**
jaroslav@682
   263
     * Closing a <tt>ByteArrayOutputStream</tt> has no effect. The methods in
jaroslav@682
   264
     * this class can be called after the stream has been closed without
jaroslav@682
   265
     * generating an <tt>IOException</tt>.
jaroslav@682
   266
     * <p>
jaroslav@682
   267
     *
jaroslav@682
   268
     */
jaroslav@682
   269
    public void close() throws IOException {
jaroslav@682
   270
    }
jaroslav@682
   271
jaroslav@682
   272
}