Skip to content

Commit d147ce9

Browse files
authored
Implement the "len" part of contract for (#476)
java.io.InputStream.read(byte[], int, int) - If len is zero, then no bytes are read and 0 is returned. - Be explicit like we are for other invariants. - Moslty do not reply on fall-through behavior. - Tested with HTTP client git master at (current HEAD) commit 4009567af704530aa0578e34fc63dd8baa945d34 - Apply the same change to SessionInputBuffer and its implementation
1 parent e890aff commit d147ce9

File tree

13 files changed

+37
-7
lines changed

13 files changed

+37
-7
lines changed

httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/io/MultiByteArrayInputStream.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ public int read(final byte b[], final int off, final int len) throws IOException
7777
((off + len) > b.length) || ((off + len) < 0)) {
7878
throw new IndexOutOfBoundsException();
7979
}
80+
if (len == 0) {
81+
return 0;
82+
}
8083
advance();
8184
if (this.current == null) {
8285
return -1;

httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/ChunkedInputStream.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,13 @@ public int read() throws IOException {
170170
* @throws IOException in case of an I/O error
171171
*/
172172
@Override
173-
public int read (final byte[] b, final int off, final int len) throws IOException {
174-
173+
public int read(final byte[] b, final int off, final int len) throws IOException {
175174
if (closed) {
176175
throw new StreamClosedException();
177176
}
178-
177+
if (len == 0) {
178+
return 0;
179+
}
179180
if (eof) {
180181
return -1;
181182
}
@@ -206,7 +207,7 @@ public int read (final byte[] b, final int off, final int len) throws IOExceptio
206207
* @throws IOException in case of an I/O error
207208
*/
208209
@Override
209-
public int read (final byte[] b) throws IOException {
210+
public int read(final byte[] b) throws IOException {
210211
return read(b, 0, b.length);
211212
}
212213

httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/ContentLengthInputStream.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,12 @@ public int read(final byte[] b, final int off, final int len) throws java.io.IOE
161161
if (closed) {
162162
throw new StreamClosedException();
163163
}
164-
164+
if (len == 0) {
165+
return 0;
166+
}
165167
if (pos >= contentLength) {
166168
return -1;
167169
}
168-
169170
int chunk = len;
170171
if (pos + len > contentLength) {
171172
chunk = (int) (contentLength - pos);

httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/EmptyInputStream.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ public int read(final byte[] buf) {
9696
*/
9797
@Override
9898
public int read(final byte[] buf, final int off, final int len) {
99+
if (len == 0) {
100+
return 0;
101+
}
99102
return -1;
100103
}
101104

httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/IdentityInputStream.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ public int read(final byte[] b, final int off, final int len) throws IOException
9696
if (this.closed) {
9797
throw new StreamClosedException();
9898
}
99+
if (len == 0) {
100+
return 0;
101+
}
99102
return this.buffer.read(b, off, len, this.inputStream);
100103
}
101104

httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/SessionInputBufferImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ public int read(final InputStream inputStream) throws IOException {
180180
@Override
181181
public int read(final byte[] b, final int off, final int len, final InputStream inputStream) throws IOException {
182182
Args.notNull(inputStream, "Input stream");
183-
if (b == null) {
183+
if (b == null || b.length == 0 || len == 0) {
184184
return 0;
185185
}
186186
if (hasBufferedData()) {

httpcore5/src/main/java/org/apache/hc/core5/http/io/EofSensorInputStream.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ public int read() throws IOException {
128128

129129
@Override
130130
public int read(final byte[] b, final int off, final int len) throws IOException {
131+
if (len == 0) {
132+
return 0;
133+
}
131134
int readLen = -1;
132135

133136
if (isReadAllowed()) {

httpcore5/src/main/java/org/apache/hc/core5/http/io/SessionInputBuffer.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public interface SessionInputBuffer {
7676
* {@code off+len} is greater than the length of the array
7777
* {@code b}, then an {@code IndexOutOfBoundsException} is
7878
* thrown.
79+
* <p> If {@code len} is zero, then no bytes are read and 0 is returned.
7980
*
8081
* @param b the buffer into which the data is read.
8182
* @param off the start offset in array {@code b}

httpcore5/src/main/java/org/apache/hc/core5/http/io/entity/ByteBufferEntity.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ public int read() throws IOException {
109109

110110
@Override
111111
public int read(final byte[] bytes, final int off, final int len) throws IOException {
112+
if (len == 0) {
113+
return 0;
114+
}
112115
if (!buffer.hasRemaining()) {
113116
return -1;
114117
}

httpcore5/src/main/java/org/apache/hc/core5/http/io/entity/EmptyInputStream.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ public int read(final byte[] buf) {
9797
*/
9898
@Override
9999
public int read(final byte[] buf, final int off, final int len) {
100+
if (len == 0) {
101+
return 0;
102+
}
100103
return -1;
101104
}
102105

0 commit comments

Comments
 (0)