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