Skip to content

Commit

Permalink
[IO-816] UnsynchronizedBufferedInputStream.read(byte[], int, int) does
Browse files Browse the repository at this point in the history
not use buffer

Fix UnsynchronizedBufferedInputStreamTest to properly test
UnsynchronizedBufferedInputStream
  • Loading branch information
garydgregory committed Oct 20, 2023
1 parent f388d71 commit 269f992
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 101 deletions.
3 changes: 3 additions & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ The <action> type attribute can be add,update,fix,remove.
MessageDigestCalculatingInputStream.MessageDigestCalculatingInputStream(InputStream, MessageDigest) now throws a NullPointerException
if the MessageDigest is null.
</action>
<action issue="IO-816" dev="ggregory" type="fix" due-to="Andreas Loth, Gary Gregory">
UnsynchronizedBufferedInputStream.read(byte[], int, int) does not use buffer.
</action>
<!-- ADD -->
<action dev="ggregory" type="add" due-to="Gary Gregory">
Add org.apache.commons.io.channels.FileChannels.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,23 +276,23 @@ public int read() throws IOException {
* set and the requested number of bytes is larger than the receiver's buffer size, this implementation bypasses the buffer and simply places the results
* directly into {@code buffer}.
*
* @param buffer the byte array in which to store the bytes read.
* @param dest the byte array in which to store the bytes read.
* @param offset the initial position in {@code buffer} to store the bytes read from this stream.
* @param length the maximum number of bytes to store in {@code buffer}.
* @return the number of bytes actually read or -1 if end of stream.
* @throws IndexOutOfBoundsException if {@code offset < 0} or {@code length < 0}, or if {@code offset + length} is greater than the size of {@code buffer}.
* @throws IOException if the stream is already closed or another IOException occurs.
*/
@Override
public int read(final byte[] buffer, int offset, final int length) throws IOException {
public int read(final byte[] dest, int offset, final int length) throws IOException {
// Use local ref since buf may be invalidated by an unsynchronized
// close()
byte[] localBuf = buffer;
if (localBuf == null) {
throw new IOException("Stream is closed");
}
// avoid int overflow
if (offset > buffer.length - length || offset < 0 || length < 0) {
if (offset > dest.length - length || offset < 0 || length < 0) {
throw new IndexOutOfBoundsException();
}
if (length == 0) {
Expand All @@ -307,7 +307,7 @@ public int read(final byte[] buffer, int offset, final int length) throws IOExce
if (pos < count) {
/* There are bytes available in the buffer. */
final int copylength = count - pos >= length ? length : count - pos;
System.arraycopy(localBuf, pos, buffer, offset, copylength);
System.arraycopy(localBuf, pos, dest, offset, copylength);
pos += copylength;
if (copylength == length || localIn.available() == 0) {
return copylength;
Expand All @@ -324,7 +324,7 @@ public int read(final byte[] buffer, int offset, final int length) throws IOExce
* If we're not marked and the required size is greater than the buffer, simply read the bytes directly bypassing the buffer.
*/
if (markPos == IOUtils.EOF && required >= localBuf.length) {
read = localIn.read(buffer, offset, required);
read = localIn.read(dest, offset, required);
if (read == IOUtils.EOF) {
return required == length ? IOUtils.EOF : length - required;
}
Expand All @@ -341,7 +341,7 @@ public int read(final byte[] buffer, int offset, final int length) throws IOExce
}

read = count - pos >= required ? required : count - pos;
System.arraycopy(localBuf, pos, buffer, offset, read);
System.arraycopy(localBuf, pos, dest, offset, read);
pos += read;
}
required -= read;
Expand Down
Loading

0 comments on commit 269f992

Please sign in to comment.