diff --git a/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/io/MultiByteArrayInputStream.java b/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/io/MultiByteArrayInputStream.java index 8f72d56aa..d4d0768f9 100644 --- a/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/io/MultiByteArrayInputStream.java +++ b/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/io/MultiByteArrayInputStream.java @@ -77,6 +77,9 @@ public int read(final byte b[], final int off, final int len) throws IOException ((off + len) > b.length) || ((off + len) < 0)) { throw new IndexOutOfBoundsException(); } + if (len == 0) { + return 0; + } advance(); if (this.current == null) { return -1; diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/ChunkedInputStream.java b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/ChunkedInputStream.java index 96a32e45f..45e8456c0 100644 --- a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/ChunkedInputStream.java +++ b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/ChunkedInputStream.java @@ -170,12 +170,13 @@ public int read() throws IOException { * @throws IOException in case of an I/O error */ @Override - public int read (final byte[] b, final int off, final int len) throws IOException { - + public int read(final byte[] b, final int off, final int len) throws IOException { if (closed) { throw new StreamClosedException(); } - + if (len == 0) { + return 0; + } if (eof) { return -1; } @@ -206,7 +207,7 @@ public int read (final byte[] b, final int off, final int len) throws IOExceptio * @throws IOException in case of an I/O error */ @Override - public int read (final byte[] b) throws IOException { + public int read(final byte[] b) throws IOException { return read(b, 0, b.length); } diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/ContentLengthInputStream.java b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/ContentLengthInputStream.java index 56d405f21..3581ebd56 100644 --- a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/ContentLengthInputStream.java +++ b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/ContentLengthInputStream.java @@ -161,11 +161,12 @@ public int read(final byte[] b, final int off, final int len) throws java.io.IOE if (closed) { throw new StreamClosedException(); } - + if (len == 0) { + return 0; + } if (pos >= contentLength) { return -1; } - int chunk = len; if (pos + len > contentLength) { chunk = (int) (contentLength - pos); diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/EmptyInputStream.java b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/EmptyInputStream.java index 1c3d2e442..9608ef6b1 100644 --- a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/EmptyInputStream.java +++ b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/EmptyInputStream.java @@ -96,6 +96,9 @@ public int read(final byte[] buf) { */ @Override public int read(final byte[] buf, final int off, final int len) { + if (len == 0) { + return 0; + } return -1; } diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/IdentityInputStream.java b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/IdentityInputStream.java index acc294fec..49f3ea965 100644 --- a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/IdentityInputStream.java +++ b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/IdentityInputStream.java @@ -96,6 +96,9 @@ public int read(final byte[] b, final int off, final int len) throws IOException if (this.closed) { throw new StreamClosedException(); } + if (len == 0) { + return 0; + } return this.buffer.read(b, off, len, this.inputStream); } diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/SessionInputBufferImpl.java b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/SessionInputBufferImpl.java index 491fe75e7..a0862b37d 100644 --- a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/SessionInputBufferImpl.java +++ b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/SessionInputBufferImpl.java @@ -180,7 +180,7 @@ public int read(final InputStream inputStream) throws IOException { @Override public int read(final byte[] b, final int off, final int len, final InputStream inputStream) throws IOException { Args.notNull(inputStream, "Input stream"); - if (b == null) { + if (b == null || b.length == 0 || len == 0) { return 0; } if (hasBufferedData()) { diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/io/EofSensorInputStream.java b/httpcore5/src/main/java/org/apache/hc/core5/http/io/EofSensorInputStream.java index a8af3a401..2da06ad06 100644 --- a/httpcore5/src/main/java/org/apache/hc/core5/http/io/EofSensorInputStream.java +++ b/httpcore5/src/main/java/org/apache/hc/core5/http/io/EofSensorInputStream.java @@ -128,6 +128,9 @@ public int read() throws IOException { @Override public int read(final byte[] b, final int off, final int len) throws IOException { + if (len == 0) { + return 0; + } int readLen = -1; if (isReadAllowed()) { diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/io/SessionInputBuffer.java b/httpcore5/src/main/java/org/apache/hc/core5/http/io/SessionInputBuffer.java index 566c20f56..3dd0b58a2 100644 --- a/httpcore5/src/main/java/org/apache/hc/core5/http/io/SessionInputBuffer.java +++ b/httpcore5/src/main/java/org/apache/hc/core5/http/io/SessionInputBuffer.java @@ -76,6 +76,7 @@ public interface SessionInputBuffer { * {@code off+len} is greater than the length of the array * {@code b}, then an {@code IndexOutOfBoundsException} is * thrown. + *
If {@code len} is zero, then no bytes are read and 0 is returned. * * @param b the buffer into which the data is read. * @param off the start offset in array {@code b} diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/io/entity/ByteBufferEntity.java b/httpcore5/src/main/java/org/apache/hc/core5/http/io/entity/ByteBufferEntity.java index 1f452c03c..f9f4b7560 100644 --- a/httpcore5/src/main/java/org/apache/hc/core5/http/io/entity/ByteBufferEntity.java +++ b/httpcore5/src/main/java/org/apache/hc/core5/http/io/entity/ByteBufferEntity.java @@ -109,6 +109,9 @@ public int read() throws IOException { @Override public int read(final byte[] bytes, final int off, final int len) throws IOException { + if (len == 0) { + return 0; + } if (!buffer.hasRemaining()) { return -1; } diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/io/entity/EmptyInputStream.java b/httpcore5/src/main/java/org/apache/hc/core5/http/io/entity/EmptyInputStream.java index 6e38e00cb..e08faa20c 100644 --- a/httpcore5/src/main/java/org/apache/hc/core5/http/io/entity/EmptyInputStream.java +++ b/httpcore5/src/main/java/org/apache/hc/core5/http/io/entity/EmptyInputStream.java @@ -97,6 +97,9 @@ public int read(final byte[] buf) { */ @Override public int read(final byte[] buf, final int off, final int len) { + if (len == 0) { + return 0; + } return -1; } diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/nio/support/classic/ContentInputStream.java b/httpcore5/src/main/java/org/apache/hc/core5/http/nio/support/classic/ContentInputStream.java index f3e567b95..0df79a5dc 100644 --- a/httpcore5/src/main/java/org/apache/hc/core5/http/nio/support/classic/ContentInputStream.java +++ b/httpcore5/src/main/java/org/apache/hc/core5/http/nio/support/classic/ContentInputStream.java @@ -54,6 +54,9 @@ public int available() throws IOException { @Override public int read(final byte[] b, final int off, final int len) throws IOException { + if (len == 0) { + return 0; + } return this.buffer.read(b, off, len); } diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/nio/support/classic/SharedInputBuffer.java b/httpcore5/src/main/java/org/apache/hc/core5/http/nio/support/classic/SharedInputBuffer.java index 45dea032c..cb3d30245 100644 --- a/httpcore5/src/main/java/org/apache/hc/core5/http/nio/support/classic/SharedInputBuffer.java +++ b/httpcore5/src/main/java/org/apache/hc/core5/http/nio/support/classic/SharedInputBuffer.java @@ -133,6 +133,9 @@ public int read() throws IOException { @Override public int read(final byte[] b, final int off, final int len) throws IOException { + if (len == 0) { + return 0; + } lock.lock(); try { setOutputMode(); diff --git a/httpcore5/src/test/java/org/apache/hc/core5/http/impl/io/TimeoutByteArrayInputStream.java b/httpcore5/src/test/java/org/apache/hc/core5/http/impl/io/TimeoutByteArrayInputStream.java index ba87dc567..50e6873b2 100644 --- a/httpcore5/src/test/java/org/apache/hc/core5/http/impl/io/TimeoutByteArrayInputStream.java +++ b/httpcore5/src/test/java/org/apache/hc/core5/http/impl/io/TimeoutByteArrayInputStream.java @@ -73,6 +73,9 @@ public int read(final byte b[], final int off, final int len) throws IOException ((off + len) > b.length) || ((off + len) < 0)) { throw new IndexOutOfBoundsException("off: "+off+" len: "+len+" b.length: "+b.length); } + if (len == 0) { + return 0; + } if (this.pos >= this.count) { return -1; }