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 write(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 // Need to verify that:
  38 // . bytes are not flushed if auto-flush is not selected
  39 // . bytes are flushed if auto-flush is selected
  40 // . the bytes written are correct
  41 // . the error condition occurs if an internal IOException is thrown
  42 public class Write {
  43     public static void main(String[] args) throws Exception {
  44         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  45         OutputStream out = new BufferedOutputStream(baos, 512);
  46         PrintStream ps = new PrintStream(out, false);
  47 
  48         byte[] buf = new byte[128];
  49         for (int i = 0; i < buf.length; i++) {
  50             buf[i] = (byte)i;
  51         }
  52 
  53         ps.write(buf);
  54         assertTrue(baos.size() == 0, "Buffer should not have been flushed");
  55         ps.close();
  56         assertTrue(baos.size() == buf.length, "Stream size " + baos.size() +
  57             " but expected " + buf.length);
  58 
  59         ps = new PrintStream(out, true);
  60         ps.write(buf);
  61         assertTrue(baos.size() == 2*buf.length, "Stream size " + baos.size() +
  62             " but expected " + 2*buf.length);
  63 
  64         byte[] arr = baos.toByteArray();
  65         assertTrue(arr.length == 2*buf.length, "Array length " + arr.length +
  66             " but expected " + 2*buf.length);
  67         assertTrue(Arrays.equals(buf, 0, buf.length, arr, 0, buf.length),
  68             "First write not equal");
  69         assertTrue(Arrays.equals(buf, 0, buf.length, arr, buf.length,
  70             2*buf.length), "Second write not equal");
  71 
  72         ps.close();
  73         ps.write(buf);
  74         assertTrue(ps.checkError(), "Error condition should be true");
  75     }
  76 
  77     private static void assertTrue(boolean condition, String msg) {
  78         if (!condition)
  79             throw new RuntimeException(msg);
  80     }
  81 }