Skip to content

Commit 4d70cda

Browse files
author
Brian Burkhalter
committed
8187898: PrintStream should override FilterOutputStream#write(byte[]) with a method that has no throws clause
Reviewed-by: alanb, rriggs, lancea, darcy
1 parent cebd13d commit 4d70cda

File tree

2 files changed

+140
-0
lines changed

2 files changed

+140
-0
lines changed

src/java.base/share/classes/java/io/PrintStream.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,7 @@ private void ensureOpen() throws IOException {
415415
*
416416
* @see java.io.OutputStream#flush()
417417
*/
418+
@Override
418419
public void flush() {
419420
synchronized (this) {
420421
try {
@@ -435,6 +436,7 @@ public void flush() {
435436
*
436437
* @see java.io.OutputStream#close()
437438
*/
439+
@Override
438440
public void close() {
439441
synchronized (this) {
440442
if (! closing) {
@@ -526,6 +528,7 @@ protected void clearError() {
526528
* @see #print(char)
527529
* @see #println(char)
528530
*/
531+
@Override
529532
public void write(int b) {
530533
try {
531534
synchronized (this) {
@@ -557,6 +560,7 @@ public void write(int b) {
557560
* @param off Offset from which to start taking bytes
558561
* @param len Number of bytes to write
559562
*/
563+
@Override
560564
public void write(byte buf[], int off, int len) {
561565
try {
562566
synchronized (this) {
@@ -574,6 +578,66 @@ public void write(byte buf[], int off, int len) {
574578
}
575579
}
576580

581+
/**
582+
* Writes all bytes from the specified byte array to this stream. If
583+
* automatic flushing is enabled then the {@code flush} method will be
584+
* invoked.
585+
*
586+
* <p> Note that the bytes will be written as given; to write characters
587+
* that will be translated according to the platform's default character
588+
* encoding, use the {@code print(char[])} or {@code println(char[])}
589+
* methods.
590+
*
591+
* @apiNote
592+
* Although declared to throw {@code IOException}, this method never
593+
* actually does so. Instead, like other methods that this class
594+
* overrides, it sets an internal flag which may be tested via the
595+
* {@link #checkError()} method. To write an array of bytes without having
596+
* to write a {@code catch} block for the {@code IOException}, use either
597+
* {@link #writeBytes(byte[] buf) writeBytes(buf)} or
598+
* {@link #write(byte[], int, int) write(buf, 0, buf.length)}.
599+
*
600+
* @implSpec
601+
* This method is equivalent to
602+
* {@link java.io.PrintStream#write(byte[],int,int)
603+
* this.write(buf, 0, buf.length)}.
604+
*
605+
* @param buf A byte array
606+
*
607+
* @throws IOException If an I/O error occurs.
608+
*
609+
* @see #writeBytes(byte[])
610+
* @see #write(byte[],int,int)
611+
*
612+
* @since 14
613+
*/
614+
@Override
615+
public void write(byte buf[]) throws IOException {
616+
this.write(buf, 0, buf.length);
617+
}
618+
619+
/**
620+
* Writes all bytes from the specified byte array to this stream.
621+
* If automatic flushing is enabled then the {@code flush} method
622+
* will be invoked.
623+
*
624+
* <p> Note that the bytes will be written as given; to write characters
625+
* that will be translated according to the platform's default character
626+
* encoding, use the {@code print(char[])} or {@code println(char[])}
627+
* methods.
628+
*
629+
* @implSpec
630+
* This method is equivalent to
631+
* {@link #write(byte[], int, int) this.write(buf, 0, buf.length)}.
632+
*
633+
* @param buf A byte array
634+
*
635+
* @since 14
636+
*/
637+
public void writeBytes(byte buf[]) {
638+
this.write(buf, 0, buf.length);
639+
}
640+
577641
/*
578642
* The following private methods on the text- and character-output streams
579643
* always flush the stream buffers, so that writes to the underlying byte
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
}

0 commit comments

Comments
 (0)