test/jdk/java/io/PrintStream/WriteBytes.java
author dholmes
Wed, 06 Nov 2019 21:18:42 -0500
changeset 58956 9a0a5e70eeb2
parent 58027 930551e8ac62
permissions -rw-r--r--
8233454: Test fails with assert(!is_init_completed(), "should only happen during init") after JDK-8229516 Reviewed-by: jiefu, dcubed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
58027
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
     1
/*
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
     4
 *
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
     8
 *
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    13
 * accompanied this code).
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    14
 *
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    18
 *
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    21
 * questions.
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    22
 */
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    23
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    24
/**
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    25
 * @test
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    26
 * @bug 8187898
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    27
 * @summary Test of writeBytes(byte[])
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    28
 */
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    29
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    30
import java.io.BufferedOutputStream;
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    31
import java.io.ByteArrayOutputStream;
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    32
import java.io.IOException;
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    33
import java.io.OutputStream;
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    34
import java.io.PrintStream;
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    35
import java.util.Arrays;
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    36
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    37
public class WriteBytes {
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    38
    public static void main(String[] args) throws Exception {
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    39
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    40
        OutputStream out = new BufferedOutputStream(baos, 512);
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    41
        PrintStream ps = new PrintStream(out, false);
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    42
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    43
        byte[] buf = new byte[128];
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    44
        for (int i = 0; i < buf.length; i++) {
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    45
            buf[i] = (byte)i;
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    46
        }
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    47
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    48
        ps.writeBytes(buf);
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    49
        assertTrue(baos.size() == 0, "Buffer should not have been flushed");
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    50
        ps.close();
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    51
        assertTrue(baos.size() == buf.length, "Stream size " + baos.size() +
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    52
            " but expected " + buf.length);
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    53
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    54
        ps = new PrintStream(out, true);
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    55
        ps.writeBytes(buf);
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    56
        assertTrue(baos.size() == 2*buf.length, "Stream size " + baos.size() +
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    57
            " but expected " + 2*buf.length);
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    58
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    59
        byte[] arr = baos.toByteArray();
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    60
        assertTrue(arr.length == 2*buf.length, "Array length " + arr.length +
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    61
            " but expected " + 2*buf.length);
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    62
        assertTrue(Arrays.equals(buf, 0, buf.length, arr, 0, buf.length),
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    63
            "First write not equal");
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    64
        assertTrue(Arrays.equals(buf, 0, buf.length, arr, buf.length,
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    65
            2*buf.length), "Second write not equal");
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    66
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    67
        ps.close();
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    68
        ps.writeBytes(buf);
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    69
        assertTrue(ps.checkError(), "Error condition should be true");
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    70
    }
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    71
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    72
    private static void assertTrue(boolean condition, String msg) {
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    73
        if (!condition)
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    74
            throw new RuntimeException(msg);
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    75
    }
930551e8ac62 8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
bpb
parents:
diff changeset
    76
}