1.1 --- a/emul/compact/src/main/java/java/io/ByteArrayOutputStream.java Mon Feb 25 19:00:08 2013 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,272 +0,0 @@
1.4 -/*
1.5 - * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
1.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1.7 - *
1.8 - * This code is free software; you can redistribute it and/or modify it
1.9 - * under the terms of the GNU General Public License version 2 only, as
1.10 - * published by the Free Software Foundation. Oracle designates this
1.11 - * particular file as subject to the "Classpath" exception as provided
1.12 - * by Oracle in the LICENSE file that accompanied this code.
1.13 - *
1.14 - * This code is distributed in the hope that it will be useful, but WITHOUT
1.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1.17 - * version 2 for more details (a copy is included in the LICENSE file that
1.18 - * accompanied this code).
1.19 - *
1.20 - * You should have received a copy of the GNU General Public License version
1.21 - * 2 along with this work; if not, write to the Free Software Foundation,
1.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1.23 - *
1.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1.25 - * or visit www.oracle.com if you need additional information or have any
1.26 - * questions.
1.27 - */
1.28 -
1.29 -package java.io;
1.30 -
1.31 -import java.util.Arrays;
1.32 -
1.33 -/**
1.34 - * This class implements an output stream in which the data is
1.35 - * written into a byte array. The buffer automatically grows as data
1.36 - * is written to it.
1.37 - * The data can be retrieved using <code>toByteArray()</code> and
1.38 - * <code>toString()</code>.
1.39 - * <p>
1.40 - * Closing a <tt>ByteArrayOutputStream</tt> has no effect. The methods in
1.41 - * this class can be called after the stream has been closed without
1.42 - * generating an <tt>IOException</tt>.
1.43 - *
1.44 - * @author Arthur van Hoff
1.45 - * @since JDK1.0
1.46 - */
1.47 -
1.48 -public class ByteArrayOutputStream extends OutputStream {
1.49 -
1.50 - /**
1.51 - * The buffer where data is stored.
1.52 - */
1.53 - protected byte buf[];
1.54 -
1.55 - /**
1.56 - * The number of valid bytes in the buffer.
1.57 - */
1.58 - protected int count;
1.59 -
1.60 - /**
1.61 - * Creates a new byte array output stream. The buffer capacity is
1.62 - * initially 32 bytes, though its size increases if necessary.
1.63 - */
1.64 - public ByteArrayOutputStream() {
1.65 - this(32);
1.66 - }
1.67 -
1.68 - /**
1.69 - * Creates a new byte array output stream, with a buffer capacity of
1.70 - * the specified size, in bytes.
1.71 - *
1.72 - * @param size the initial size.
1.73 - * @exception IllegalArgumentException if size is negative.
1.74 - */
1.75 - public ByteArrayOutputStream(int size) {
1.76 - if (size < 0) {
1.77 - throw new IllegalArgumentException("Negative initial size: "
1.78 - + size);
1.79 - }
1.80 - buf = new byte[size];
1.81 - }
1.82 -
1.83 - /**
1.84 - * Increases the capacity if necessary to ensure that it can hold
1.85 - * at least the number of elements specified by the minimum
1.86 - * capacity argument.
1.87 - *
1.88 - * @param minCapacity the desired minimum capacity
1.89 - * @throws OutOfMemoryError if {@code minCapacity < 0}. This is
1.90 - * interpreted as a request for the unsatisfiably large capacity
1.91 - * {@code (long) Integer.MAX_VALUE + (minCapacity - Integer.MAX_VALUE)}.
1.92 - */
1.93 - private void ensureCapacity(int minCapacity) {
1.94 - // overflow-conscious code
1.95 - if (minCapacity - buf.length > 0)
1.96 - grow(minCapacity);
1.97 - }
1.98 -
1.99 - /**
1.100 - * Increases the capacity to ensure that it can hold at least the
1.101 - * number of elements specified by the minimum capacity argument.
1.102 - *
1.103 - * @param minCapacity the desired minimum capacity
1.104 - */
1.105 - private void grow(int minCapacity) {
1.106 - // overflow-conscious code
1.107 - int oldCapacity = buf.length;
1.108 - int newCapacity = oldCapacity << 1;
1.109 - if (newCapacity - minCapacity < 0)
1.110 - newCapacity = minCapacity;
1.111 - if (newCapacity < 0) {
1.112 - if (minCapacity < 0) // overflow
1.113 - throw new OutOfMemoryError();
1.114 - newCapacity = Integer.MAX_VALUE;
1.115 - }
1.116 - buf = Arrays.copyOf(buf, newCapacity);
1.117 - }
1.118 -
1.119 - /**
1.120 - * Writes the specified byte to this byte array output stream.
1.121 - *
1.122 - * @param b the byte to be written.
1.123 - */
1.124 - public synchronized void write(int b) {
1.125 - ensureCapacity(count + 1);
1.126 - buf[count] = (byte) b;
1.127 - count += 1;
1.128 - }
1.129 -
1.130 - /**
1.131 - * Writes <code>len</code> bytes from the specified byte array
1.132 - * starting at offset <code>off</code> to this byte array output stream.
1.133 - *
1.134 - * @param b the data.
1.135 - * @param off the start offset in the data.
1.136 - * @param len the number of bytes to write.
1.137 - */
1.138 - public synchronized void write(byte b[], int off, int len) {
1.139 - if ((off < 0) || (off > b.length) || (len < 0) ||
1.140 - ((off + len) - b.length > 0)) {
1.141 - throw new IndexOutOfBoundsException();
1.142 - }
1.143 - ensureCapacity(count + len);
1.144 - System.arraycopy(b, off, buf, count, len);
1.145 - count += len;
1.146 - }
1.147 -
1.148 - /**
1.149 - * Writes the complete contents of this byte array output stream to
1.150 - * the specified output stream argument, as if by calling the output
1.151 - * stream's write method using <code>out.write(buf, 0, count)</code>.
1.152 - *
1.153 - * @param out the output stream to which to write the data.
1.154 - * @exception IOException if an I/O error occurs.
1.155 - */
1.156 - public synchronized void writeTo(OutputStream out) throws IOException {
1.157 - out.write(buf, 0, count);
1.158 - }
1.159 -
1.160 - /**
1.161 - * Resets the <code>count</code> field of this byte array output
1.162 - * stream to zero, so that all currently accumulated output in the
1.163 - * output stream is discarded. The output stream can be used again,
1.164 - * reusing the already allocated buffer space.
1.165 - *
1.166 - * @see java.io.ByteArrayInputStream#count
1.167 - */
1.168 - public synchronized void reset() {
1.169 - count = 0;
1.170 - }
1.171 -
1.172 - /**
1.173 - * Creates a newly allocated byte array. Its size is the current
1.174 - * size of this output stream and the valid contents of the buffer
1.175 - * have been copied into it.
1.176 - *
1.177 - * @return the current contents of this output stream, as a byte array.
1.178 - * @see java.io.ByteArrayOutputStream#size()
1.179 - */
1.180 - public synchronized byte toByteArray()[] {
1.181 - return Arrays.copyOf(buf, count);
1.182 - }
1.183 -
1.184 - /**
1.185 - * Returns the current size of the buffer.
1.186 - *
1.187 - * @return the value of the <code>count</code> field, which is the number
1.188 - * of valid bytes in this output stream.
1.189 - * @see java.io.ByteArrayOutputStream#count
1.190 - */
1.191 - public synchronized int size() {
1.192 - return count;
1.193 - }
1.194 -
1.195 - /**
1.196 - * Converts the buffer's contents into a string decoding bytes using the
1.197 - * platform's default character set. The length of the new <tt>String</tt>
1.198 - * is a function of the character set, and hence may not be equal to the
1.199 - * size of the buffer.
1.200 - *
1.201 - * <p> This method always replaces malformed-input and unmappable-character
1.202 - * sequences with the default replacement string for the platform's
1.203 - * default character set. The {@linkplain java.nio.charset.CharsetDecoder}
1.204 - * class should be used when more control over the decoding process is
1.205 - * required.
1.206 - *
1.207 - * @return String decoded from the buffer's contents.
1.208 - * @since JDK1.1
1.209 - */
1.210 - public synchronized String toString() {
1.211 - return new String(buf, 0, count);
1.212 - }
1.213 -
1.214 - /**
1.215 - * Converts the buffer's contents into a string by decoding the bytes using
1.216 - * the specified {@link java.nio.charset.Charset charsetName}. The length of
1.217 - * the new <tt>String</tt> is a function of the charset, and hence may not be
1.218 - * equal to the length of the byte array.
1.219 - *
1.220 - * <p> This method always replaces malformed-input and unmappable-character
1.221 - * sequences with this charset's default replacement string. The {@link
1.222 - * java.nio.charset.CharsetDecoder} class should be used when more control
1.223 - * over the decoding process is required.
1.224 - *
1.225 - * @param charsetName the name of a supported
1.226 - * {@linkplain java.nio.charset.Charset </code>charset<code>}
1.227 - * @return String decoded from the buffer's contents.
1.228 - * @exception UnsupportedEncodingException
1.229 - * If the named charset is not supported
1.230 - * @since JDK1.1
1.231 - */
1.232 - public synchronized String toString(String charsetName)
1.233 - throws UnsupportedEncodingException
1.234 - {
1.235 - return new String(buf, 0, count, charsetName);
1.236 - }
1.237 -
1.238 - /**
1.239 - * Creates a newly allocated string. Its size is the current size of
1.240 - * the output stream and the valid contents of the buffer have been
1.241 - * copied into it. Each character <i>c</i> in the resulting string is
1.242 - * constructed from the corresponding element <i>b</i> in the byte
1.243 - * array such that:
1.244 - * <blockquote><pre>
1.245 - * c == (char)(((hibyte & 0xff) << 8) | (b & 0xff))
1.246 - * </pre></blockquote>
1.247 - *
1.248 - * @deprecated This method does not properly convert bytes into characters.
1.249 - * As of JDK 1.1, the preferred way to do this is via the
1.250 - * <code>toString(String enc)</code> method, which takes an encoding-name
1.251 - * argument, or the <code>toString()</code> method, which uses the
1.252 - * platform's default character encoding.
1.253 - *
1.254 - * @param hibyte the high byte of each resulting Unicode character.
1.255 - * @return the current contents of the output stream, as a string.
1.256 - * @see java.io.ByteArrayOutputStream#size()
1.257 - * @see java.io.ByteArrayOutputStream#toString(String)
1.258 - * @see java.io.ByteArrayOutputStream#toString()
1.259 - */
1.260 - @Deprecated
1.261 - public synchronized String toString(int hibyte) {
1.262 - return new String(buf, hibyte, 0, count);
1.263 - }
1.264 -
1.265 - /**
1.266 - * Closing a <tt>ByteArrayOutputStream</tt> has no effect. The methods in
1.267 - * this class can be called after the stream has been closed without
1.268 - * generating an <tt>IOException</tt>.
1.269 - * <p>
1.270 - *
1.271 - */
1.272 - public void close() throws IOException {
1.273 - }
1.274 -
1.275 -}