Skip to content

Commit

Permalink
Simplified buffer code
Browse files Browse the repository at this point in the history
  • Loading branch information
DavyLandman committed Sep 20, 2024
1 parent ea2101a commit 1bc04ad
Showing 1 changed file with 4 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1021,18 +1021,15 @@ public int read(char[] cbuf, int off, int len) throws IOException {
if (off < 0 || len < 0 || len > cbuf.length + off) {
throw new IndexOutOfBoundsException();
}
int written = 0;
while (written < len) {
// we try to read as much as we can from the chunks of the buffer
var target = CharBuffer.wrap(cbuf, off, len);
while (target.hasRemaining()) {
var actualBuffer = getBuffer();
if (!actualBuffer.hasRemaining()) {
break;
}
int toRead = Math.min(len - written, actualBuffer.remaining());
actualBuffer.get(cbuf, off + written, toRead);
written += toRead;
actualBuffer.read(target);
}
return written == 0 ? -1 : written;
return target.position() == off ? -1 : (len - target.remaining());
}

@Override
Expand Down

0 comments on commit 1bc04ad

Please sign in to comment.