Skip to content

Commit 269f992

Browse files
committed
[IO-816] UnsynchronizedBufferedInputStream.read(byte[], int, int) does
not use buffer Fix UnsynchronizedBufferedInputStreamTest to properly test UnsynchronizedBufferedInputStream
1 parent f388d71 commit 269f992

3 files changed

Lines changed: 52 additions & 101 deletions

File tree

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ The <action> type attribute can be add,update,fix,remove.
105105
MessageDigestCalculatingInputStream.MessageDigestCalculatingInputStream(InputStream, MessageDigest) now throws a NullPointerException
106106
if the MessageDigest is null.
107107
</action>
108+
<action issue="IO-816" dev="ggregory" type="fix" due-to="Andreas Loth, Gary Gregory">
109+
UnsynchronizedBufferedInputStream.read(byte[], int, int) does not use buffer.
110+
</action>
108111
<!-- ADD -->
109112
<action dev="ggregory" type="add" due-to="Gary Gregory">
110113
Add org.apache.commons.io.channels.FileChannels.

src/main/java/org/apache/commons/io/input/UnsynchronizedBufferedInputStream.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -276,23 +276,23 @@ public int read() throws IOException {
276276
* 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
277277
* directly into {@code buffer}.
278278
*
279-
* @param buffer the byte array in which to store the bytes read.
279+
* @param dest the byte array in which to store the bytes read.
280280
* @param offset the initial position in {@code buffer} to store the bytes read from this stream.
281281
* @param length the maximum number of bytes to store in {@code buffer}.
282282
* @return the number of bytes actually read or -1 if end of stream.
283283
* @throws IndexOutOfBoundsException if {@code offset < 0} or {@code length < 0}, or if {@code offset + length} is greater than the size of {@code buffer}.
284284
* @throws IOException if the stream is already closed or another IOException occurs.
285285
*/
286286
@Override
287-
public int read(final byte[] buffer, int offset, final int length) throws IOException {
287+
public int read(final byte[] dest, int offset, final int length) throws IOException {
288288
// Use local ref since buf may be invalidated by an unsynchronized
289289
// close()
290290
byte[] localBuf = buffer;
291291
if (localBuf == null) {
292292
throw new IOException("Stream is closed");
293293
}
294294
// avoid int overflow
295-
if (offset > buffer.length - length || offset < 0 || length < 0) {
295+
if (offset > dest.length - length || offset < 0 || length < 0) {
296296
throw new IndexOutOfBoundsException();
297297
}
298298
if (length == 0) {
@@ -307,7 +307,7 @@ public int read(final byte[] buffer, int offset, final int length) throws IOExce
307307
if (pos < count) {
308308
/* There are bytes available in the buffer. */
309309
final int copylength = count - pos >= length ? length : count - pos;
310-
System.arraycopy(localBuf, pos, buffer, offset, copylength);
310+
System.arraycopy(localBuf, pos, dest, offset, copylength);
311311
pos += copylength;
312312
if (copylength == length || localIn.available() == 0) {
313313
return copylength;
@@ -324,7 +324,7 @@ public int read(final byte[] buffer, int offset, final int length) throws IOExce
324324
* If we're not marked and the required size is greater than the buffer, simply read the bytes directly bypassing the buffer.
325325
*/
326326
if (markPos == IOUtils.EOF && required >= localBuf.length) {
327-
read = localIn.read(buffer, offset, required);
327+
read = localIn.read(dest, offset, required);
328328
if (read == IOUtils.EOF) {
329329
return required == length ? IOUtils.EOF : length - required;
330330
}
@@ -341,7 +341,7 @@ public int read(final byte[] buffer, int offset, final int length) throws IOExce
341341
}
342342

343343
read = count - pos >= required ? required : count - pos;
344-
System.arraycopy(localBuf, pos, buffer, offset, read);
344+
System.arraycopy(localBuf, pos, dest, offset, read);
345345
pos += read;
346346
}
347347
required -= read;

0 commit comments

Comments
 (0)