< prev index next >
test/jdk/java/io/ByteArrayOutputStream/Write.java
Print this page
*** 1,7 ****
/*
! * 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.
--- 1,7 ----
/*
! * 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.
*** 19,48 ****
* 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 {
--- 19,46 ----
* 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 8180628
! * @summary Check for correct implementation of ByteArrayInputStream.write
*/
! import java.io.ByteArrayOutputStream;
! import java.util.Arrays;
! import java.util.Random;
! public class Write {
! private static void doBoundsTest(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 {
*** 53,76 ****
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);
}
}
--- 51,131 ----
System.err.println("NullPointerException is thrown -- OKAY");
} catch (Throwable e){
throw new RuntimeException("Unexpected Exception is thrown");
}
+ if (b != null) {
+ System.err.println("ByteArrayOutStream.writeBytes -- b.length = " +
+ b.length);
+ } else{
+ System.err.println("ByteArrayOutStream.writeBytes - b is null");
}
! try {
! baos.writeBytes(b);
! } catch (NullPointerException e) {
! System.err.println("NullPointerException is thrown -- OKAY");
! } catch (Throwable e){
! throw new RuntimeException("Unexpected Exception is thrown");
! }
! }
! private 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);
! }
!
! private static void writeTest() throws Exception {
! ByteArrayOutputStream baos = new ByteArrayOutputStream();
! Random rnd = new Random(System.nanoTime());
! 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);
!
! baos.write(b, off1, len1);
! byte[] b1 = baos.toByteArray();
! if (b1.length != len1) {
! throw new RuntimeException("Array length test 1 failed.");
! }
! if (!Arrays.equals(b, off1, off1 + len1, b1, 0, b1.length)) {
! throw new RuntimeException("Array equality test 1 failed.");
! }
! baos.write(b, off2, len2);
! byte[] b2 = baos.toByteArray();
! if (b2.length != len1 + len2) {
! throw new RuntimeException("Array length test 2 failed.");
! }
! if (!Arrays.equals(b, off2, off2 + len2, b2, len1, b2.length)) {
! throw new RuntimeException("Array equality test 2 failed.");
! }
+ baos.writeBytes(b);
+ byte[] b3 = baos.toByteArray();
+ if (b3.length != len1 + len2 + b.length) {
+ throw new RuntimeException("Array length test 3 failed.");
+ }
+ if (!Arrays.equals(b, 0, b.length, b3, len1 + len2,
+ len1 + len2 + b.length)) {
+ throw new RuntimeException("Array equality test 3 failed.");
+ }
}
+ public static void main( String argv[] ) throws Exception {
+ boundsTest();
+ writeTest();
+ }
}
< prev index next >