--- old/src/java.base/share/classes/java/io/PrintStream.java 2019-07-15 08:52:05.000000000 -0700 +++ new/src/java.base/share/classes/java/io/PrintStream.java 2019-07-15 08:52:04.000000000 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 1996, 2017, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -410,6 +410,7 @@ * * @see java.io.OutputStream#flush() */ + @Override public void flush() { synchronized (this) { try { @@ -430,6 +431,7 @@ * * @see java.io.OutputStream#close() */ + @Override public void close() { synchronized (this) { if (! closing) { @@ -521,6 +523,7 @@ * @see #print(char) * @see #println(char) */ + @Override public void write(int b) { try { synchronized (this) { @@ -545,13 +548,14 @@ * *

Note that the bytes will be written as given; to write characters * that will be translated according to the platform's default character - * encoding, use the {@code print(char)} or {@code println(char)} + * encoding, use the {@code print(char[])} or {@code println(char[])} * methods. * * @param buf A byte array * @param off Offset from which to start taking bytes * @param len Number of bytes to write */ + @Override public void write(byte buf[], int off, int len) { try { synchronized (this) { @@ -569,6 +573,36 @@ } } + /** + * Writes all bytes from the specified byte array to this stream. + * If automatic flushing is enabled then the {@code flush} method + * will be invoked. + * + *

Note that the bytes will be written as given; to write characters + * that will be translated according to the platform's default character + * encoding, use the {@code print(char[])} or {@code println(char[])} + * methods. + * + * @param buf A byte array + */ + @Override + public void write(byte buf[]) { + try { + synchronized (this) { + ensureOpen(); + out.write(buf, 0, buf.length); + if (autoFlush) + out.flush(); + } + } + catch (InterruptedIOException x) { + Thread.currentThread().interrupt(); + } + catch (IOException x) { + trouble = true; + } + } + /* * The following private methods on the text- and character-output streams * always flush the stream buffers, so that writes to the underlying byte --- old/src/java.desktop/share/classes/sun/print/PSPrinterJob.java 2019-07-15 08:52:05.000000000 -0700 +++ new/src/java.desktop/share/classes/sun/print/PSPrinterJob.java 2019-07-15 08:52:05.000000000 -0700 @@ -947,33 +947,23 @@ int index = 0; byte[] rgbData = new byte[intSrcWidth * 3]; - try { - /* Skip the parts of the image that are not part - * of the source rectangle. - */ - index = (int) srcY * srcBitMapWidth; - - for(int i = 0; i < intSrcHeight; i++) { - - /* Skip the left part of the image that is not - * part of the source rectangle. - */ - index += (int) srcX; + /* Skip the parts of the image that are not part + * of the source rectangle. + */ + index = (int) srcY * srcBitMapWidth; - index = swapBGRtoRGB(bgrData, index, rgbData); - byte[] encodedData = rlEncode(rgbData); - byte[] asciiData = ascii85Encode(encodedData); - mPSStream.write(asciiData); - mPSStream.println(""); - } + for(int i = 0; i < intSrcHeight; i++) { - /* - * If there is an IOError we subvert it to a PrinterException. - * Fix: There has got to be a better way, maybe define - * a PrinterIOException and then throw that? + /* Skip the left part of the image that is not + * part of the source rectangle. */ - } catch (IOException e) { - //throw new PrinterException(e.toString()); + index += (int) srcX; + + index = swapBGRtoRGB(bgrData, index, rgbData); + byte[] encodedData = rlEncode(rgbData); + byte[] asciiData = ascii85Encode(encodedData); + mPSStream.write(asciiData); + mPSStream.println(""); } mPSStream.println(IMAGE_RESTORE); @@ -1023,17 +1013,16 @@ int index = 0; byte[] rgbData = new byte[width*3]; - try { - for(int i = 0; i < height; i++) { - index = swapBGRtoRGB(bgrData, index, rgbData); - byte[] encodedData = rlEncode(rgbData); - byte[] asciiData = ascii85Encode(encodedData); - mPSStream.write(asciiData); - mPSStream.println(""); - } + for(int i = 0; i < height; i++) { + index = swapBGRtoRGB(bgrData, index, rgbData); + byte[] encodedData = rlEncode(rgbData); + byte[] asciiData = ascii85Encode(encodedData); + mPSStream.write(asciiData); + mPSStream.println(""); + } - } catch (IOException e) { - throw new PrinterIOException(e); + if (mPSStream.checkError()) { + throw new PrinterException("Error in PrintStream"); } mPSStream.println(IMAGE_RESTORE); --- old/test/jdk/java/lang/System/LoggerFinder/internal/BaseDefaultLoggerFinderTest/BaseDefaultLoggerFinderTest.java 2019-07-15 08:52:06.000000000 -0700 +++ new/test/jdk/java/lang/System/LoggerFinder/internal/BaseDefaultLoggerFinderTest/BaseDefaultLoggerFinderTest.java 2019-07-15 08:52:06.000000000 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -184,7 +184,7 @@ } @Override - public void write(byte[] b) throws IOException { + public void write(byte[] b) { super.write(b); if (forward.get()) err.write(b); } --- old/test/jdk/java/lang/System/LoggerFinder/internal/LoggerFinderLoaderTest/LoggerFinderLoaderTest.java 2019-07-15 08:52:07.000000000 -0700 +++ new/test/jdk/java/lang/System/LoggerFinder/internal/LoggerFinderLoaderTest/LoggerFinderLoaderTest.java 2019-07-15 08:52:06.000000000 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -281,7 +281,7 @@ } @Override - public void write(byte[] b) throws IOException { + public void write(byte[] b) { super.write(b); if (forward.get()) err.write(b); } --- old/test/jdk/java/lang/System/LoggerFinder/internal/SimpleConsoleLoggerTest/SimpleConsoleLoggerTest.java 2019-07-15 08:52:07.000000000 -0700 +++ new/test/jdk/java/lang/System/LoggerFinder/internal/SimpleConsoleLoggerTest/SimpleConsoleLoggerTest.java 2019-07-15 08:52:07.000000000 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -111,7 +111,7 @@ } @Override - public void write(byte[] b) throws IOException { + public void write(byte[] b) { super.write(b); if (forward.get()) err.write(b); } --- /dev/null 2019-07-15 08:52:08.000000000 -0700 +++ new/test/jdk/java/io/PrintStream/Write.java 2019-07-15 08:52:08.000000000 -0700 @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/** + * @test + * @bug 8187898 + * @summary Test of write(byte[]) + */ + +import java.io.BufferedOutputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.io.PrintStream; +import java.util.Arrays; + +// Need to verify that: +// . bytes are not flushed if auto-flush is not selected +// . bytes are flushed if auto-flush is selected +// . the bytes written are correct +// . the error condition occurs if an internal IOException is thrown +public class Write { + public static void main(String[] args) throws Exception { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + OutputStream out = new BufferedOutputStream(baos, 512); + PrintStream ps = new PrintStream(out, false); + + byte[] buf = new byte[128]; + for (int i = 0; i < buf.length; i++) { + buf[i] = (byte)i; + } + + ps.write(buf); + assertTrue(baos.size() == 0, "Buffer should not have been flushed"); + ps.close(); + assertTrue(baos.size() == buf.length, "Stream size " + baos.size() + + " but expected " + buf.length); + + ps = new PrintStream(out, true); + ps.write(buf); + assertTrue(baos.size() == 2*buf.length, "Stream size " + baos.size() + + " but expected " + 2*buf.length); + + byte[] arr = baos.toByteArray(); + assertTrue(arr.length == 2*buf.length, "Array length " + arr.length + + " but expected " + 2*buf.length); + assertTrue(Arrays.equals(buf, 0, buf.length, arr, 0, buf.length), + "First write not equal"); + assertTrue(Arrays.equals(buf, 0, buf.length, arr, buf.length, + 2*buf.length), "Second write not equal"); + + ps.close(); + ps.write(buf); + assertTrue(ps.checkError(), "Error condition should be true"); + } + + private static void assertTrue(boolean condition, String msg) { + if (!condition) + throw new RuntimeException(msg); + } +}