11/*
2- * Some portions of this file have been modified by Robert Hanson hansonr.at.stolaf.edu 2012-2017
3- * for use in SwingJS via transpilation into JavaScript using Java2Script.
4- *
5- * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved.
63 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
74 *
85 * This code is free software; you can redistribute it and/or modify it
2825
2926package java .io ;
3027
31- import java .io . IOException ;
28+ import java .util . Arrays ;
3229
3330/**
3431 * This class implements an output stream in which the data is
@@ -96,6 +93,14 @@ private void ensureCapacity(int minCapacity) {
9693 grow (minCapacity );
9794 }
9895
96+ /**
97+ * The maximum size of array to allocate.
98+ * Some VMs reserve some header words in an array.
99+ * Attempts to allocate larger arrays may result in
100+ * OutOfMemoryError: Requested array size exceeds VM limit
101+ */
102+ private static final int MAX_ARRAY_SIZE = Integer .MAX_VALUE - 8 ;
103+
99104 /**
100105 * Increases the capacity to ensure that it can hold at least the
101106 * number of elements specified by the minimum capacity argument.
@@ -108,22 +113,19 @@ private void grow(int minCapacity) {
108113 int newCapacity = oldCapacity << 1 ;
109114 if (newCapacity - minCapacity < 0 )
110115 newCapacity = minCapacity ;
111- if (newCapacity < 0 ) {
112- if (minCapacity < 0 ) // overflow
113- throw new OutOfMemoryError ();
114- newCapacity = minCapacity ;
115- }
116- buf = arrayCopyByte (buf , newCapacity );
116+ if (newCapacity - MAX_ARRAY_SIZE > 0 )
117+ newCapacity = hugeCapacity (minCapacity );
118+ buf = Arrays .copyOf (buf , newCapacity );
117119 }
118120
119- private static byte [] arrayCopyByte (byte [] array , int newLength ) {
120- byte [] t = new byte [newLength ];
121- System .arraycopy (array , 0 , t , 0 , array .length < newLength ? array .length
122- : newLength );
123- return t ;
121+ private static int hugeCapacity (int minCapacity ) {
122+ if (minCapacity < 0 ) // overflow
123+ throw new OutOfMemoryError ();
124+ return (minCapacity > MAX_ARRAY_SIZE ) ?
125+ Integer .MAX_VALUE :
126+ MAX_ARRAY_SIZE ;
124127 }
125128
126-
127129 /**
128130 * Writes the specified byte to this byte array output stream.
129131 *
@@ -187,7 +189,7 @@ public synchronized void reset() {
187189 * @see java.io.ByteArrayOutputStream#size()
188190 */
189191 public synchronized byte toByteArray ()[] {
190- return (count == buf .length ? buf : arrayCopyByte (buf , count ));
192+ return (count == buf .length ? buf : Arrays . copyOf (buf , count )); // BH why copy if buf is fine?
191193 }
192194
193195 /**
@@ -221,39 +223,64 @@ public synchronized String toString() {
221223 return new String (buf , 0 , count );
222224 }
223225
224- // /**
225- // * Converts the buffer's contents into a string by decoding the bytes using
226- // * the specified {@link java.nio.charset.Charset charsetName}. The length of
227- // * the new <tt>String</tt> is a function of the charset, and hence may not be
228- // * equal to the length of the byte array.
229- // *
230- // * <p> This method always replaces malformed-input and unmappable-character
231- // * sequences with this charset's default replacement string. The {@link
232- // * java.nio.charset.CharsetDecoder} class should be used when more control
233- // * over the decoding process is required.
234- // *
235- // * @param charsetName the name of a supported
236- // * {@linkplain java.nio.charset.Charset </code>charset<code>}
237- // * @return String decoded from the buffer's contents.
238- // * @exception UnsupportedEncodingException
239- // * If the named charset is not supported
240- // * @since JDK1.1
241- // */
242- // public synchronized String toString(String charsetName)
243- // throws UnsupportedEncodingException
244- // {
245- // return new String(buf, 0, count, charsetName);
246- // }
226+ /**
227+ * Converts the buffer's contents into a string by decoding the bytes using
228+ * the named {@link java.nio.charset.Charset charset}. The length of the new
229+ * <tt>String</tt> is a function of the charset, and hence may not be equal
230+ * to the length of the byte array.
231+ *
232+ * <p> This method always replaces malformed-input and unmappable-character
233+ * sequences with this charset's default replacement string. The {@link
234+ * java.nio.charset.CharsetDecoder} class should be used when more control
235+ * over the decoding process is required.
236+ *
237+ * @param charsetName the name of a supported
238+ * {@link java.nio.charset.Charset charset}
239+ * @return String decoded from the buffer's contents.
240+ * @exception UnsupportedEncodingException
241+ * If the named charset is not supported
242+ * @since JDK1.1
243+ */
244+ public synchronized String toString (String charsetName )
245+ throws UnsupportedEncodingException
246+ {
247+ return new String (buf , 0 , count , charsetName );
248+ }
249+
250+ /**
251+ * Creates a newly allocated string. Its size is the current size of
252+ * the output stream and the valid contents of the buffer have been
253+ * copied into it. Each character <i>c</i> in the resulting string is
254+ * constructed from the corresponding element <i>b</i> in the byte
255+ * array such that:
256+ * <blockquote><pre>
257+ * c == (char)(((hibyte & 0xff) << 8) | (b & 0xff))
258+ * </pre></blockquote>
259+ *
260+ * @deprecated This method does not properly convert bytes into characters.
261+ * As of JDK 1.1, the preferred way to do this is via the
262+ * <code>toString(String enc)</code> method, which takes an encoding-name
263+ * argument, or the <code>toString()</code> method, which uses the
264+ * platform's default character encoding.
265+ *
266+ * @param hibyte the high byte of each resulting Unicode character.
267+ * @return the current contents of the output stream, as a string.
268+ * @see java.io.ByteArrayOutputStream#size()
269+ * @see java.io.ByteArrayOutputStream#toString(String)
270+ * @see java.io.ByteArrayOutputStream#toString()
271+ */
272+ @ Deprecated
273+ public synchronized String toString (int hibyte ) {
274+ return new String (buf , hibyte , 0 , count );
275+ }
247276
248277 /**
249278 * Closing a <tt>ByteArrayOutputStream</tt> has no effect. The methods in
250279 * this class can be called after the stream has been closed without
251280 * generating an <tt>IOException</tt>.
252- * <p>
253- *
254281 */
255282 @ Override
256283 public void close () throws IOException {
257284 }
258285
259- }
286+ }
0 commit comments