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 package test.java.io.PrintStream; 30 31 import java.io.BufferedOutputStream; 32 import java.io.ByteArrayOutputStream; 33 import java.io.OutputStream; 34 import java.io.PrintStream; 35 import java.util.Arrays; 36 37 import org.testng.annotations.Test; 38 import static org.testng.Assert.assertEquals; 39 import static org.testng.Assert.assertTrue; 40 41 public class WriteBytes { 42 43 @Test testWriteBytes()44 public void testWriteBytes() { 45 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 46 OutputStream out = new BufferedOutputStream(baos, 512); 47 PrintStream ps = new PrintStream(out, false); 48 49 byte[] buf = new byte[128]; 50 for (int i = 0; i < buf.length; i++) { 51 buf[i] = (byte)i; 52 } 53 54 ps.writeBytes(buf); 55 assertEquals(baos.size(), 0, "Buffer should not have been flushed"); 56 ps.close(); 57 assertEquals(baos.size(), buf.length, "Stream size " + baos.size() + 58 " but expected " + buf.length); 59 60 ps = new PrintStream(out, true); 61 ps.writeBytes(buf); 62 assertEquals(baos.size(), 2*buf.length, "Stream size " + baos.size() + 63 " but expected " + 2*buf.length); 64 65 byte[] arr = baos.toByteArray(); 66 assertEquals(arr.length, 2*buf.length, "Array length " + arr.length + 67 " but expected " + 2*buf.length); 68 assertTrue(Arrays.equals(buf, 0, buf.length, arr, 0, buf.length), 69 "First write not equal"); 70 assertTrue(Arrays.equals(buf, 0, buf.length, arr, buf.length, 71 2*buf.length), "Second write not equal"); 72 73 ps.close(); 74 ps.writeBytes(buf); 75 assertTrue(ps.checkError(), "Error condition should be true"); 76 } 77 78 } 79