test/jdk/java/io/ByteArrayOutputStream/Write.java
author bpb
Fri, 23 Mar 2018 15:05:43 -0700
changeset 49414 1f14faf358fb
parent 47216 test/jdk/java/io/ByteArrayOutputStream/WriteBounds.java@71c04702a3d5
child 49679 7084eec5c723
permissions -rw-r--r--
8180410: ByteArrayOutputStream should not throw IOExceptions Summary: Add ByteArrayOutputStream.writeBytes() Reviewed-by: rriggs, smarks
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
49414
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
     2
 * Copyright (c) 1997, 2018, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
49414
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
    24
/*
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
    25
 * @test
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
    26
 * @bug 4017158 8180410
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
    27
 * @library /test/lib
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
    28
 * @build jdk.test.lib.RandomFactory
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
    29
 * @run testng Write
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
    30
 * @summary Check for correct implementation of ByteArrayInputStream.write
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
    31
 * @key randomness
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
    32
 */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
49414
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
    34
import java.io.ByteArrayOutputStream;
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
    35
import java.util.Arrays;
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
    36
import java.util.Random;
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
    37
import jdk.test.lib.RandomFactory;
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
    38
import org.testng.annotations.Test;
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
    39
import static org.testng.Assert.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
49414
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
    41
public class Write {
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
    42
    private static void doBoundsTest(byte[] b, int off, int len,
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
    43
                                     ByteArrayOutputStream baos)
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
    44
        throws Exception {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        if (b != null) {
49414
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
    46
            System.out.println("ByteArrayOutStream.write: b.length = " +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
                               b.length + " off = " + off + " len = " + len);
49414
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
    48
        } else{
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
    49
            System.out.println("ByteArrayOutStream.write: b is null off = " +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
                               off + " len = " + len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            baos.write(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        } catch (IndexOutOfBoundsException e) {
49414
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
    56
            System.out.println("IndexOutOfBoundsException is thrown: OKAY");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        } catch (NullPointerException e) {
49414
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
    58
            System.out.println("NullPointerException is thrown: OKAY");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        } catch (Throwable e){
49414
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
    60
            throw new RuntimeException("Unexpected Exception is thrown", e);
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
    61
        }
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
    62
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
    63
        if (b != null) {
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
    64
            System.out.println("ByteArrayOutStream.writeBytes: b.length = " +
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
    65
                               b.length);
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
    66
        } else{
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
    67
            System.out.println("ByteArrayOutStream.writeBytes: b is null");
2
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: 47216
diff changeset
    70
        try {
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
    71
            baos.writeBytes(b);
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
    72
        } catch (NullPointerException e) {
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
    73
            System.out.println("NullPointerException is thrown: OKAY");
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
    74
        } catch (Throwable e){
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
    75
            throw new RuntimeException("Unexpected Exception is thrown", e);
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
    76
        }
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
    77
    }
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
    78
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
    79
    @Test
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
    80
    public static void boundsTest() throws Exception {
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
    81
        byte array1[] = {1 , 2 , 3 , 4 , 5};     // Simple array
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
    82
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
    83
        //Create new ByteArrayOutputStream object
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
    84
        ByteArrayOutputStream y1 = new ByteArrayOutputStream(5);
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
    85
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
    86
        doBoundsTest(array1, 0, Integer.MAX_VALUE , y1);
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
    87
        doBoundsTest(array1, 0, array1.length+100, y1);
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
    88
        doBoundsTest(array1, -1, 2, y1);
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
    89
        doBoundsTest(array1, 0, -1, y1);
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
    90
        doBoundsTest(null, 0, 2, y1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
49414
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
    93
    @Test
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
    94
    public static void writeTest() throws Exception {
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
    95
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
    96
        Random rnd = RandomFactory.getRandom();
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
    97
        final int size = 17 + rnd.nextInt(128);
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
    98
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
    99
        byte[] b = new byte[size];
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
   100
        rnd.nextBytes(b);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
49414
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
   102
        int off1 = rnd.nextInt(size / 4) + 1;
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
   103
        int len1 = Math.min(rnd.nextInt(size / 4) + 1, size - off1);
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
   104
        int off2 = rnd.nextInt(size / 2) + 1;
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
   105
        int len2 = Math.min(rnd.nextInt(size / 2) + 1, size - off2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
49414
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
   107
        System.out.format("size: %d, off1: %d, len1: %d, off2: %d, len2: %d%n",
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
   108
            size, off1, len1, off2, len2);
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
   109
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
   110
        baos.write(b, off1, len1);
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
   111
        byte[] b1 = baos.toByteArray();
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
   112
        assertEquals(b1.length, len1, "Array length test 1 failed.");
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
   113
        assertEquals(b1, Arrays.copyOfRange(b, off1, off1 + len1),
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
   114
            "Array equality test 1 failed.");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
49414
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
   116
        baos.write(b, off2, len2);
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
   117
        byte[] b2 = baos.toByteArray();
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
   118
        assertEquals(b2.length, len1 + len2, "Array length test 2 failed.");
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
   119
        assertEquals(Arrays.copyOfRange(b2, 0, len1),
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
   120
             Arrays.copyOfRange(b, off1, off1 + len1),
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
   121
            "Array equality test 2A failed.");
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
   122
        assertEquals(Arrays.copyOfRange(b2, len1, len1 + len2),
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
   123
            Arrays.copyOfRange(b, off2, off2 + len2),
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
   124
            "Array equality test 2B failed.");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
49414
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
   126
        baos.writeBytes(b);
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
   127
        byte[] b3 = baos.toByteArray();
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
   128
        int len3 = len1 + len2 + b.length;
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
   129
        if (b3.length != len1 + len2 + b.length) {
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
   130
            throw new RuntimeException("Array length test 3 failed.");
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
   131
        }
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
   132
        assertEquals(b3.length, len3, "Array length test 3 failed.");
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
   133
        assertEquals(Arrays.copyOfRange(b3, 0, len1),
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
   134
             Arrays.copyOfRange(b, off1, off1 + len1),
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
   135
            "Array equality test 3A failed.");
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
   136
        assertEquals(Arrays.copyOfRange(b3, len1, len1 + len2),
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
   137
            Arrays.copyOfRange(b, off2, off2 + len2),
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
   138
            "Array equality test 3B failed.");
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
   139
        assertEquals(Arrays.copyOfRange(b3, len1 + len2, len3), b,
1f14faf358fb 8180410: ByteArrayOutputStream should not throw IOExceptions
bpb
parents: 47216
diff changeset
   140
            "Array equality test 3C failed.");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
}