8180410: ByteArrayOutputStream should not throw IOExceptions
Summary: Add ByteArrayOutputStream.writeBytes()
Reviewed-by: rriggs, smarks
--- a/src/java.base/share/classes/java/io/ByteArrayOutputStream.java Fri Mar 23 21:39:54 2018 +0000
+++ b/src/java.base/share/classes/java/io/ByteArrayOutputStream.java Fri Mar 23 15:05:43 2018 -0700
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 1994, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1994, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -57,7 +57,7 @@
protected int count;
/**
- * Creates a new byte array output stream. The buffer capacity is
+ * Creates a new {@code ByteArrayOutputStream}. The buffer capacity is
* initially 32 bytes, though its size increases if necessary.
*/
public ByteArrayOutputStream() {
@@ -65,11 +65,11 @@
}
/**
- * Creates a new byte array output stream, with a buffer capacity of
+ * Creates a new {@code ByteArrayOutputStream}, with a buffer capacity of
* the specified size, in bytes.
*
- * @param size the initial size.
- * @exception IllegalArgumentException if size is negative.
+ * @param size the initial size.
+ * @throws IllegalArgumentException if size is negative.
*/
public ByteArrayOutputStream(int size) {
if (size < 0) {
@@ -84,7 +84,7 @@
* at least the number of elements specified by the minimum
* capacity argument.
*
- * @param minCapacity the desired minimum capacity
+ * @param minCapacity the desired minimum capacity
* @throws OutOfMemoryError if {@code minCapacity < 0}. This is
* interpreted as a request for the unsatisfiably large capacity
* {@code (long) Integer.MAX_VALUE + (minCapacity - Integer.MAX_VALUE)}.
@@ -129,7 +129,7 @@
}
/**
- * Writes the specified byte to this byte array output stream.
+ * Writes the specified byte to this {@code ByteArrayOutputStream}.
*
* @param b the byte to be written.
*/
@@ -141,11 +141,15 @@
/**
* Writes {@code len} bytes from the specified byte array
- * starting at offset {@code off} to this byte array output stream.
+ * starting at offset {@code off} to this {@code ByteArrayOutputStream}.
*
* @param b the data.
* @param off the start offset in the data.
* @param len the number of bytes to write.
+ * @throws NullPointerException if {@code b} is {@code null}.
+ * @throws IndexOutOfBoundsException if {@code off} is negative,
+ * {@code len} is negative, or {@code len} is greater than
+ * {@code b.length - off}
*/
public synchronized void write(byte b[], int off, int len) {
Objects.checkFromIndexSize(off, len, b.length);
@@ -155,20 +159,37 @@
}
/**
- * Writes the complete contents of this byte array output stream to
+ * Writes the complete contents of the specified byte array
+ * to this {@code ByteArrayOutputStream}.
+ *
+ * @apiNote
+ * This method is equivalent to {@link #write(byte[],int,int)
+ * write(b, 0, b.length)}.
+ *
+ * @param b the data.
+ * @throws NullPointerException if {@code b} is {@code null}.
+ * @since 11
+ */
+ public void writeBytes(byte b[]) {
+ write(b, 0, b.length);
+ }
+
+ /**
+ * Writes the complete contents of this {@code ByteArrayOutputStream} to
* the specified output stream argument, as if by calling the output
* stream's write method using {@code out.write(buf, 0, count)}.
*
- * @param out the output stream to which to write the data.
- * @exception IOException if an I/O error occurs.
+ * @param out the output stream to which to write the data.
+ * @throws NullPointerException if {@code out} is {@code null}.
+ * @throws IOException if an I/O error occurs.
*/
public synchronized void writeTo(OutputStream out) throws IOException {
out.write(buf, 0, count);
}
/**
- * Resets the {@code count} field of this byte array output
- * stream to zero, so that all currently accumulated output in the
+ * Resets the {@code count} field of this {@code ByteArrayOutputStream}
+ * to zero, so that all currently accumulated output in the
* output stream is discarded. The output stream can be used again,
* reusing the already allocated buffer space.
*
@@ -244,12 +265,12 @@
* </pre>
*
*
- * @param charsetName the name of a supported
- * {@link java.nio.charset.Charset charset}
- * @return String decoded from the buffer's contents.
- * @exception UnsupportedEncodingException
- * If the named charset is not supported
- * @since 1.1
+ * @param charsetName the name of a supported
+ * {@link java.nio.charset.Charset charset}
+ * @return String decoded from the buffer's contents.
+ * @throws UnsupportedEncodingException
+ * If the named charset is not supported
+ * @since 1.1
*/
public synchronized String toString(String charsetName)
throws UnsupportedEncodingException
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/java/io/ByteArrayOutputStream/Write.java Fri Mar 23 15:05:43 2018 -0700
@@ -0,0 +1,142 @@
+/*
+ * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 4017158 8180410
+ * @library /test/lib
+ * @build jdk.test.lib.RandomFactory
+ * @run testng Write
+ * @summary Check for correct implementation of ByteArrayInputStream.write
+ * @key randomness
+ */
+
+import java.io.ByteArrayOutputStream;
+import java.util.Arrays;
+import java.util.Random;
+import jdk.test.lib.RandomFactory;
+import org.testng.annotations.Test;
+import static org.testng.Assert.*;
+
+public class Write {
+ private static void doBoundsTest(byte[] b, int off, int len,
+ ByteArrayOutputStream baos)
+ throws Exception {
+ if (b != null) {
+ System.out.println("ByteArrayOutStream.write: b.length = " +
+ b.length + " off = " + off + " len = " + len);
+ } else{
+ System.out.println("ByteArrayOutStream.write: b is null off = " +
+ off + " len = " + len);
+ }
+
+ try {
+ baos.write(b, off, len);
+ } catch (IndexOutOfBoundsException e) {
+ System.out.println("IndexOutOfBoundsException is thrown: OKAY");
+ } catch (NullPointerException e) {
+ System.out.println("NullPointerException is thrown: OKAY");
+ } catch (Throwable e){
+ throw new RuntimeException("Unexpected Exception is thrown", e);
+ }
+
+ if (b != null) {
+ System.out.println("ByteArrayOutStream.writeBytes: b.length = " +
+ b.length);
+ } else{
+ System.out.println("ByteArrayOutStream.writeBytes: b is null");
+ }
+
+ try {
+ baos.writeBytes(b);
+ } catch (NullPointerException e) {
+ System.out.println("NullPointerException is thrown: OKAY");
+ } catch (Throwable e){
+ throw new RuntimeException("Unexpected Exception is thrown", e);
+ }
+ }
+
+ @Test
+ public static void boundsTest() throws Exception {
+ byte array1[] = {1 , 2 , 3 , 4 , 5}; // Simple array
+
+ //Create new ByteArrayOutputStream object
+ ByteArrayOutputStream y1 = new ByteArrayOutputStream(5);
+
+ doBoundsTest(array1, 0, Integer.MAX_VALUE , y1);
+ doBoundsTest(array1, 0, array1.length+100, y1);
+ doBoundsTest(array1, -1, 2, y1);
+ doBoundsTest(array1, 0, -1, y1);
+ doBoundsTest(null, 0, 2, y1);
+ }
+
+ @Test
+ public static void writeTest() throws Exception {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ Random rnd = RandomFactory.getRandom();
+ final int size = 17 + rnd.nextInt(128);
+
+ byte[] b = new byte[size];
+ rnd.nextBytes(b);
+
+ int off1 = rnd.nextInt(size / 4) + 1;
+ int len1 = Math.min(rnd.nextInt(size / 4) + 1, size - off1);
+ int off2 = rnd.nextInt(size / 2) + 1;
+ int len2 = Math.min(rnd.nextInt(size / 2) + 1, size - off2);
+
+ System.out.format("size: %d, off1: %d, len1: %d, off2: %d, len2: %d%n",
+ size, off1, len1, off2, len2);
+
+ baos.write(b, off1, len1);
+ byte[] b1 = baos.toByteArray();
+ assertEquals(b1.length, len1, "Array length test 1 failed.");
+ assertEquals(b1, Arrays.copyOfRange(b, off1, off1 + len1),
+ "Array equality test 1 failed.");
+
+ baos.write(b, off2, len2);
+ byte[] b2 = baos.toByteArray();
+ assertEquals(b2.length, len1 + len2, "Array length test 2 failed.");
+ assertEquals(Arrays.copyOfRange(b2, 0, len1),
+ Arrays.copyOfRange(b, off1, off1 + len1),
+ "Array equality test 2A failed.");
+ assertEquals(Arrays.copyOfRange(b2, len1, len1 + len2),
+ Arrays.copyOfRange(b, off2, off2 + len2),
+ "Array equality test 2B failed.");
+
+ baos.writeBytes(b);
+ byte[] b3 = baos.toByteArray();
+ int len3 = len1 + len2 + b.length;
+ if (b3.length != len1 + len2 + b.length) {
+ throw new RuntimeException("Array length test 3 failed.");
+ }
+ assertEquals(b3.length, len3, "Array length test 3 failed.");
+ assertEquals(Arrays.copyOfRange(b3, 0, len1),
+ Arrays.copyOfRange(b, off1, off1 + len1),
+ "Array equality test 3A failed.");
+ assertEquals(Arrays.copyOfRange(b3, len1, len1 + len2),
+ Arrays.copyOfRange(b, off2, off2 + len2),
+ "Array equality test 3B failed.");
+ assertEquals(Arrays.copyOfRange(b3, len1 + len2, len3), b,
+ "Array equality test 3C failed.");
+ }
+}
--- a/test/jdk/java/io/ByteArrayOutputStream/WriteBounds.java Fri Mar 23 21:39:54 2018 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,76 +0,0 @@
-/*
- * Copyright (c) 1997, Oracle and/or its affiliates. All rights reserved.
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * This code is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 only, as
- * published by the Free Software Foundation.
- *
- * This code is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- *
- * You should have received a copy of the GNU General Public License version
- * 2 along with this work; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
- * or visit www.oracle.com if you need additional information or have any
- * questions.
- */
-
-/* @test
- @bug 4017158
- @summary Check for correct implementation of ByteArrayInputStream.write
- */
-
-import java.io.*;
-
-
-public class WriteBounds{
-
- private static void dotest(byte[] b, int off, int len,
- ByteArrayOutputStream baos)
- throws Exception
- {
-
- if (b != null) {
- System.err.println("ByteArrayOutStream.write -- b.length = " +
- b.length + " off = " + off + " len = " + len);
- }
- else{
- System.err.println("ByteArrayOutStream.write - b is null off = " +
- off + " len = " + len);
- }
-
- try {
- baos.write(b, off, len);
- } catch (IndexOutOfBoundsException e) {
- System.err.println("IndexOutOfBoundsException is thrown -- OKAY");
- } catch (NullPointerException e) {
- System.err.println("NullPointerException is thrown -- OKAY");
- } catch (Throwable e){
- throw new RuntimeException("Unexpected Exception is thrown");
- }
-
- }
-
- public static void main( String argv[] ) throws Exception {
-
- ByteArrayOutputStream y1;
- byte array1[]={1 , 2 , 3 , 4 , 5}; // Simple array
-
- //Create new ByteArrayOutputStream object
- y1 = new ByteArrayOutputStream(5);
-
- dotest(array1, 0, Integer.MAX_VALUE , y1);
- dotest(array1, 0, array1.length+100, y1);
- dotest(array1, -1, 2, y1);
- dotest(array1, 0, -1, y1);
- dotest(null, 0, 2, y1);
-
- }
-
-}