Skip to content

Commit

Permalink
Implement the "len" part of contract for (#476)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
garydgregory authored Aug 16, 2024
1 parent e890aff commit d147ce9
Show file tree
Hide file tree
Showing 13 changed files with 37 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* <p> 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}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit d147ce9

Please sign in to comment.