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