src/java.base/share/classes/java/io/ByteArrayOutputStream.java
author igerasim
Wed, 16 Oct 2019 14:32:17 -0700
changeset 58653 71fef5fae9cc
parent 54971 4285b4d13471
permissions -rw-r--r--
8230407: SocketPermission and FilePermission action list allows leading comma Reviewed-by: chegar Contributed-by: Ivan Gerasimov <ivan.gerasimov@oracle.com>, Chris Hegarty <chris.hegarty@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
54971
4285b4d13471 8223593: Refactor code for reallocating storage
igerasim
parents: 49414
diff changeset
     2
 * Copyright (c) 1994, 2019, 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
48252
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
    28
import java.nio.charset.Charset;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Arrays;
48436
45a9a7a49379 8194649: Minor cleanup of parameter checking in ByteArrayOutputStream and ObjectInputStream
bpb
parents: 48252
diff changeset
    30
import java.util.Objects;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
54971
4285b4d13471 8223593: Refactor code for reallocating storage
igerasim
parents: 49414
diff changeset
    32
import jdk.internal.util.ArraysSupport;
4285b4d13471 8223593: Refactor code for reallocating storage
igerasim
parents: 49414
diff changeset
    33
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * This class implements an output stream in which the data is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * written into a byte array. The buffer automatically grows as data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * is written to it.
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 26217
diff changeset
    38
 * The data can be retrieved using {@code toByteArray()} and
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 26217
diff changeset
    39
 * {@code toString()}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <p>
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 26217
diff changeset
    41
 * Closing a {@code ByteArrayOutputStream} has no effect. The methods in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * this class can be called after the stream has been closed without
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 26217
diff changeset
    43
 * generating an {@code IOException}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @author  Arthur van Hoff
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 23010
diff changeset
    46
 * @since   1.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
public class ByteArrayOutputStream extends OutputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * The buffer where data is stored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    protected byte buf[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * The number of valid bytes in the buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    protected int count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /**
49414
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 48436
diff changeset
    62
     * Creates a new {@code ByteArrayOutputStream}. The buffer capacity is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * initially 32 bytes, though its size increases if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public ByteArrayOutputStream() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        this(32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /**
49414
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 48436
diff changeset
    70
     * Creates a new {@code ByteArrayOutputStream}, with a buffer capacity of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * the specified size, in bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     *
49414
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 48436
diff changeset
    73
     * @param  size   the initial size.
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 48436
diff changeset
    74
     * @throws IllegalArgumentException if size is negative.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    public ByteArrayOutputStream(int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        if (size < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            throw new IllegalArgumentException("Negative initial size: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                                               + size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        buf = new byte[size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /**
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2
diff changeset
    85
     * 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
    86
     * at least the number of elements specified by the minimum
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2
diff changeset
    87
     * capacity argument.
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2
diff changeset
    88
     *
54971
4285b4d13471 8223593: Refactor code for reallocating storage
igerasim
parents: 49414
diff changeset
    89
     * @param  minCapacity the desired minimum capacity.
4285b4d13471 8223593: Refactor code for reallocating storage
igerasim
parents: 49414
diff changeset
    90
     * @throws OutOfMemoryError if {@code minCapacity < 0} and
4285b4d13471 8223593: Refactor code for reallocating storage
igerasim
parents: 49414
diff changeset
    91
     * {@code minCapacity - buf.length > 0}.  This is interpreted as a
4285b4d13471 8223593: Refactor code for reallocating storage
igerasim
parents: 49414
diff changeset
    92
     * request for the unsatisfiably large capacity.
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2
diff changeset
    93
     * {@code (long) Integer.MAX_VALUE + (minCapacity - Integer.MAX_VALUE)}.
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
    private void ensureCapacity(int minCapacity) {
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2
diff changeset
    96
        // overflow-conscious code
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2
diff changeset
    97
        int oldCapacity = buf.length;
54971
4285b4d13471 8223593: Refactor code for reallocating storage
igerasim
parents: 49414
diff changeset
    98
        int minGrowth = minCapacity - oldCapacity;
4285b4d13471 8223593: Refactor code for reallocating storage
igerasim
parents: 49414
diff changeset
    99
        if (minGrowth > 0) {
4285b4d13471 8223593: Refactor code for reallocating storage
igerasim
parents: 49414
diff changeset
   100
            buf = Arrays.copyOf(buf, ArraysSupport.newLength(oldCapacity,
4285b4d13471 8223593: Refactor code for reallocating storage
igerasim
parents: 49414
diff changeset
   101
                    minGrowth, oldCapacity /* preferred growth */));
4285b4d13471 8223593: Refactor code for reallocating storage
igerasim
parents: 49414
diff changeset
   102
        }
26217
e037c9104788 8055949: ByteArrayOutputStream capacity should be maximal array size permitted by VM
martin
parents: 25859
diff changeset
   103
    }
e037c9104788 8055949: ByteArrayOutputStream capacity should be maximal array size permitted by VM
martin
parents: 25859
diff changeset
   104
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2
diff changeset
   105
    /**
49414
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 48436
diff changeset
   106
     * Writes the specified byte to this {@code ByteArrayOutputStream}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @param   b   the byte to be written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public synchronized void write(int b) {
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2
diff changeset
   111
        ensureCapacity(count + 1);
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2
diff changeset
   112
        buf[count] = (byte) b;
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2
diff changeset
   113
        count += 1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /**
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 26217
diff changeset
   117
     * Writes {@code len} bytes from the specified byte array
49414
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 48436
diff changeset
   118
     * starting at offset {@code off} to this {@code ByteArrayOutputStream}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @param   b     the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @param   off   the start offset in the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @param   len   the number of bytes to write.
49414
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 48436
diff changeset
   123
     * @throws  NullPointerException if {@code b} is {@code null}.
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 48436
diff changeset
   124
     * @throws  IndexOutOfBoundsException if {@code off} is negative,
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 48436
diff changeset
   125
     * {@code len} is negative, or {@code len} is greater than
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 48436
diff changeset
   126
     * {@code b.length - off}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    public synchronized void write(byte b[], int off, int len) {
48436
45a9a7a49379 8194649: Minor cleanup of parameter checking in ByteArrayOutputStream and ObjectInputStream
bpb
parents: 48252
diff changeset
   129
        Objects.checkFromIndexSize(off, len, b.length);
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2
diff changeset
   130
        ensureCapacity(count + len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        System.arraycopy(b, off, buf, count, len);
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2
diff changeset
   132
        count += len;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
49414
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 48436
diff changeset
   136
     * Writes the complete contents of the specified byte array
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 48436
diff changeset
   137
     * to this {@code ByteArrayOutputStream}.
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 48436
diff changeset
   138
     *
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 48436
diff changeset
   139
     * @apiNote
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 48436
diff changeset
   140
     * This method is equivalent to {@link #write(byte[],int,int)
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 48436
diff changeset
   141
     * write(b, 0, b.length)}.
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 48436
diff changeset
   142
     *
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 48436
diff changeset
   143
     * @param   b     the data.
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 48436
diff changeset
   144
     * @throws  NullPointerException if {@code b} is {@code null}.
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 48436
diff changeset
   145
     * @since   11
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 48436
diff changeset
   146
     */
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 48436
diff changeset
   147
    public void writeBytes(byte b[]) {
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 48436
diff changeset
   148
        write(b, 0, b.length);
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 48436
diff changeset
   149
    }
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 48436
diff changeset
   150
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 48436
diff changeset
   151
    /**
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 48436
diff changeset
   152
     * Writes the complete contents of this {@code ByteArrayOutputStream} to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * the specified output stream argument, as if by calling the output
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 26217
diff changeset
   154
     * stream's write method using {@code out.write(buf, 0, count)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *
49414
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 48436
diff changeset
   156
     * @param   out   the output stream to which to write the data.
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 48436
diff changeset
   157
     * @throws  NullPointerException if {@code out} is {@code null}.
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 48436
diff changeset
   158
     * @throws  IOException if an I/O error occurs.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    public synchronized void writeTo(OutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        out.write(buf, 0, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /**
49414
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 48436
diff changeset
   165
     * Resets the {@code count} field of this {@code ByteArrayOutputStream}
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 48436
diff changeset
   166
     * to zero, so that all currently accumulated output in the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * output stream is discarded. The output stream can be used again,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * reusing the already allocated buffer space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @see     java.io.ByteArrayInputStream#count
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    public synchronized void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * Creates a newly allocated byte array. Its size is the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * size of this output stream and the valid contents of the buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * have been copied into it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @return  the current contents of this output stream, as a byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @see     java.io.ByteArrayOutputStream#size()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     */
40410
5fd4a1f809f8 8163517: Various cleanup in java.io code
igerasim
parents: 32033
diff changeset
   184
    public synchronized byte[] toByteArray() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        return Arrays.copyOf(buf, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * Returns the current size of the buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 26217
diff changeset
   191
     * @return  the value of the {@code count} field, which is the number
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     *          of valid bytes in this output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @see     java.io.ByteArrayOutputStream#count
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    public synchronized int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * Converts the buffer's contents into a string decoding bytes using the
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 26217
diff changeset
   201
     * platform's default character set. The length of the new {@code String}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * is a function of the character set, and hence may not be equal to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * size of the buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * <p> This method always replaces malformed-input and unmappable-character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * sequences with the default replacement string for the platform's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * default character set. The {@linkplain java.nio.charset.CharsetDecoder}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * class should be used when more control over the decoding process is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * @return String decoded from the buffer's contents.
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 23010
diff changeset
   212
     * @since  1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    public synchronized String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        return new String(buf, 0, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * Converts the buffer's contents into a string by decoding the bytes using
48252
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   220
     * the named {@link java.nio.charset.Charset charset}.
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   221
     *
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   222
     * <p> This method is equivalent to {@code #toString(charset)} that takes a
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   223
     * {@link java.nio.charset.Charset charset}.
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   224
     *
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   225
     * <p> An invocation of this method of the form
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     *
48252
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   227
     * <pre> {@code
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   228
     *      ByteArrayOutputStream b = ...
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   229
     *      b.toString("UTF-8")
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   230
     *      }
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   231
     * </pre>
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   232
     *
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   233
     * behaves in exactly the same way as the expression
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   234
     *
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   235
     * <pre> {@code
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   236
     *      ByteArrayOutputStream b = ...
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   237
     *      b.toString(StandardCharsets.UTF_8)
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   238
     *      }
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   239
     * </pre>
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   240
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *
49414
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 48436
diff changeset
   242
     * @param  charsetName  the name of a supported
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 48436
diff changeset
   243
     *         {@link java.nio.charset.Charset charset}
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 48436
diff changeset
   244
     * @return String decoded from the buffer's contents.
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 48436
diff changeset
   245
     * @throws UnsupportedEncodingException
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 48436
diff changeset
   246
     *         If the named charset is not supported
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 48436
diff changeset
   247
     * @since  1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    public synchronized String toString(String charsetName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        throws UnsupportedEncodingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        return new String(buf, 0, count, charsetName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    /**
48252
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   256
     * Converts the buffer's contents into a string by decoding the bytes using
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   257
     * the specified {@link java.nio.charset.Charset charset}. The length of the new
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   258
     * {@code String} is a function of the charset, and hence may not be equal
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   259
     * to the length of the byte array.
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   260
     *
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   261
     * <p> This method always replaces malformed-input and unmappable-character
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   262
     * sequences with the charset's default replacement string. The {@link
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   263
     * java.nio.charset.CharsetDecoder} class should be used when more control
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   264
     * over the decoding process is required.
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   265
     *
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   266
     * @param      charset  the {@linkplain java.nio.charset.Charset charset}
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   267
     *             to be used to decode the {@code bytes}
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   268
     * @return     String decoded from the buffer's contents.
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   269
     * @since      10
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   270
     */
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   271
    public synchronized String toString(Charset charset) {
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   272
        return new String(buf, 0, count, charset);
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   273
    }
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   274
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   275
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * Creates a newly allocated string. Its size is the current size of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * the output stream and the valid contents of the buffer have been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * copied into it. Each character <i>c</i> in the resulting string is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * constructed from the corresponding element <i>b</i> in the byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * array such that:
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 26217
diff changeset
   281
     * <blockquote><pre>{@code
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 26217
diff changeset
   282
     *     c == (char)(((hibyte & 0xff) << 8) | (b & 0xff))
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 26217
diff changeset
   283
     * }</pre></blockquote>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * @deprecated This method does not properly convert bytes into characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * As of JDK&nbsp;1.1, the preferred way to do this is via the
48252
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   287
     * {@link #toString(String charsetName)} or {@link #toString(Charset charset)}
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   288
     * method, which takes an encoding-name or charset argument,
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   289
     * or the {@code toString()} method, which uses the platform's default
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47216
diff changeset
   290
     * character encoding.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * @param      hibyte    the high byte of each resulting Unicode character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * @return     the current contents of the output stream, as a string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @see        java.io.ByteArrayOutputStream#size()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * @see        java.io.ByteArrayOutputStream#toString(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * @see        java.io.ByteArrayOutputStream#toString()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    public synchronized String toString(int hibyte) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        return new String(buf, hibyte, 0, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    /**
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 26217
diff changeset
   304
     * Closing a {@code ByteArrayOutputStream} has no effect. The methods in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * this class can be called after the stream has been closed without
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 26217
diff changeset
   306
     * generating an {@code IOException}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
}