Skip to content

Commit

Permalink
8297632: InputStream.transferTo() method should specify what the retu…
Browse files Browse the repository at this point in the history
…rn value should be when the number of bytes transfered is larger than Long.MAX_VALUE

Reviewed-by: alanb, lancea
  • Loading branch information
Brian Burkhalter committed Feb 14, 2023
1 parent f7dee77 commit 5b2d430
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 13 deletions.
6 changes: 5 additions & 1 deletion src/java.base/share/classes/java/io/BufferedInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,11 @@ private long implTransferTo(OutputStream out) throws IOException {
out.write(buffer);
pos = count;
}
return avail + getInIfOpen().transferTo(out);
try {
return Math.addExact(avail, getInIfOpen().transferTo(out));
} catch (ArithmeticException ignore) {
return Long.MAX_VALUE;
}
} else {
return super.transferTo(out);
}
Expand Down
8 changes: 6 additions & 2 deletions src/java.base/share/classes/java/io/FileInputStream.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1994, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1994, 2023, 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
Expand Down Expand Up @@ -392,7 +392,11 @@ public long transferTo(OutputStream out) throws IOException {
return transferred;
}
}
return transferred + super.transferTo(out);
try {
return Math.addExact(transferred, super.transferTo(out));
} catch (ArithmeticException ignore) {
return Long.MAX_VALUE;
}
}

private long length() throws IOException {
Expand Down
13 changes: 11 additions & 2 deletions src/java.base/share/classes/java/io/InputStream.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1994, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1994, 2023, 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
Expand Down Expand Up @@ -769,6 +769,9 @@ public boolean markSupported() {
* interrupted during the transfer, is highly input and output stream
* specific, and therefore not specified.
* <p>
* If the total number of bytes transferred is greater than {@linkplain
* Long#MAX_VALUE}, then {@code Long.MAX_VALUE} will be returned.
* <p>
* If an I/O error occurs reading from the input stream or writing to the
* output stream, then it may do so after some bytes have been read or
* written. Consequently the input stream may not be at end of stream and
Expand All @@ -789,7 +792,13 @@ public long transferTo(OutputStream out) throws IOException {
int read;
while ((read = this.read(buffer, 0, DEFAULT_BUFFER_SIZE)) >= 0) {
out.write(buffer, 0, read);
transferred += read;
if (transferred < Long.MAX_VALUE) {
try {
transferred = Math.addExact(transferred, read);
} catch (ArithmeticException ignore) {
transferred = Long.MAX_VALUE;
}
}
}
return transferred;
}
Expand Down
8 changes: 6 additions & 2 deletions src/java.base/share/classes/java/io/PushbackInputStream.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1994, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1994, 2023, 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
Expand Down Expand Up @@ -421,7 +421,11 @@ public long transferTo(OutputStream out) throws IOException {
out.write(buffer);
pos = buffer.length;
}
return avail + in.transferTo(out);
try {
return Math.addExact(avail, in.transferTo(out));
} catch (ArithmeticException ignore) {
return Long.MAX_VALUE;
}
} else {
return super.transferTo(out);
}
Expand Down
13 changes: 11 additions & 2 deletions src/java.base/share/classes/java/io/Reader.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 2023, 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
Expand Down Expand Up @@ -421,6 +421,9 @@ public void reset() throws IOException {
* interrupted during the transfer, is highly reader and writer
* specific, and therefore not specified.
* <p>
* If the total number of characters transferred is greater than {@linkplain
* Long#MAX_VALUE}, then {@code Long.MAX_VALUE} will be returned.
* <p>
* If an I/O error occurs reading from the reader or writing to the
* writer, then it may do so after some characters have been read or
* written. Consequently the reader may not be at end of the stream and
Expand All @@ -441,7 +444,13 @@ public long transferTo(Writer out) throws IOException {
int nRead;
while ((nRead = read(buffer, 0, TRANSFER_BUFFER_SIZE)) >= 0) {
out.write(buffer, 0, nRead);
transferred += nRead;
if (transferred < Long.MAX_VALUE) {
try {
transferred = Math.addExact(transferred, nRead);
} catch (ArithmeticException ignore) {
transferred = Long.MAX_VALUE;
}
}
}
return transferred;
}
Expand Down
12 changes: 9 additions & 3 deletions src/java.base/share/classes/java/io/SequenceInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -240,12 +240,18 @@ public void close() throws IOException {
public long transferTo(OutputStream out) throws IOException {
Objects.requireNonNull(out, "out");
if (getClass() == SequenceInputStream.class) {
long c = 0;
long transferred = 0;
while (in != null) {
c += in.transferTo(out);
if (transferred < Long.MAX_VALUE) {
try {
transferred = Math.addExact(transferred, in.transferTo(out));
} catch (ArithmeticException ignore) {
return Long.MAX_VALUE;
}
}
nextStream();
}
return c;
return transferred;
} else {
return super.transferTo(out);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 2023, 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
Expand Down Expand Up @@ -350,6 +350,9 @@ public void skipNBytes(long n) throws IOException {
* interrupted during the transfer, is highly input and output stream
* specific, and therefore not specified.
* <p>
* If the total number of bytes transferred is greater than {@linkplain
* Long#MAX_VALUE}, then {@code Long.MAX_VALUE} will be returned.
* <p>
* If an I/O error occurs reading from the input stream or writing to the
* output stream, then it may do so after some bytes have been read or
* written. Consequently, the input stream may not be at end of stream and
Expand Down

1 comment on commit 5b2d430

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.