Skip to content
This repository was archived by the owner on Sep 2, 2022. It is now read-only.

Commit f46a917

Browse files
author
Brian Burkhalter
committed
6766844: ByteArrayInputStream#read with a byte array of length 0 not consistent with InputStream when at EOF
Reviewed-by: naoto, lancea, joehw
1 parent 9e75f92 commit f46a917

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1994, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1994, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -156,6 +156,10 @@ public synchronized int read() {
156156
* {@code b[off+k-1]} in the manner performed by {@code System.arraycopy}.
157157
* The value {@code k} is added into {@code pos} and {@code k} is returned.
158158
* <p>
159+
* Unlike the {@link InputStream#read(byte[],int,int) overridden method}
160+
* of {@code InputStream}, this method returns {@code -1} instead of zero
161+
* if the end of the stream has been reached and {@code len == 0}.
162+
* <p>
159163
* This {@code read} method cannot block.
160164
*
161165
* @param b the buffer into which the data is read.

test/jdk/java/io/ByteArrayInputStream/ReadAllReadNTransferTo.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -33,7 +33,7 @@
3333
* @library /test/lib
3434
* @build jdk.test.lib.RandomFactory
3535
* @run main ReadAllReadNTransferTo
36-
* @bug 8180451
36+
* @bug 6766844 8180451
3737
* @summary Verify ByteArrayInputStream readAllBytes, readNBytes, and transferTo
3838
* @key randomness
3939
*/
@@ -48,8 +48,16 @@ public static void main(String... args) throws IOException {
4848
int position = random.nextInt(SIZE/2);
4949
int size = random.nextInt(SIZE - position);
5050

51-
ByteArrayInputStream bais =
52-
new ByteArrayInputStream(buf, position, size);
51+
ByteArrayInputStream bais = new ByteArrayInputStream(buf);
52+
bais.readAllBytes();
53+
if (bais.read(new byte[0]) != -1) {
54+
throw new RuntimeException("read(byte[]) did not return -1");
55+
}
56+
if (bais.read(new byte[1], 0, 0) != -1) {
57+
throw new RuntimeException("read(byte[],int,int) did not return -1");
58+
}
59+
60+
bais = new ByteArrayInputStream(buf, position, size);
5361
int off = size < 2 ? 0 : random.nextInt(size / 2);
5462
int len = size - off < 1 ? 0 : random.nextInt(size - off);
5563

@@ -72,7 +80,6 @@ public static void main(String... args) throws IOException {
7280
throw new RuntimeException("readAllBytes content");
7381
}
7482

75-
// XXX transferTo()
7683
bais = new ByteArrayInputStream(buf);
7784
ByteArrayOutputStream baos = new ByteArrayOutputStream(buf.length);
7885
if (bais.transferTo(baos) != buf.length) {

0 commit comments

Comments
 (0)