Skip to content

Commit

Permalink
available and releaseByteBuf methods added
Browse files Browse the repository at this point in the history
Signed-off-by: Maxim Nesen <[email protected]>
  • Loading branch information
senivam committed Feb 18, 2020
1 parent c6d6efa commit eeb7d9a
Showing 1 changed file with 30 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ public int read(byte[] b, int off, int len) throws IOException {
}
buffer.get(b, off, len);
if (rem == len) {
current.release();
current = null;
buffer = null;
releaseByteBuf();
}

return len;
Expand All @@ -90,13 +88,19 @@ public int read() throws IOException {

@Override
public synchronized void close() {
if (current != null) {
current.release();
}

current = null;
buffer = null;
cleanup(true);
releaseByteBuf();

cleanup(true);
}

private void releaseByteBuf() {
if (current != null) {
current.release();
}

current = null;
buffer = null;
}

protected synchronized ByteBuffer awaitNext() {
Expand Down Expand Up @@ -137,6 +141,23 @@ protected void cleanup(boolean drain) {
}
}

@Override
public int available() throws IOException {
if (end || reading) {
return 0;
}
if (current != null) {
return current.readableBytes();
}
if (!isList.isEmpty()) {
final ByteBuf peek = isList.peek();
if (peek != null && peek.isReadable()) {
return peek.readableBytes();
}
}
return 0;
}

public synchronized void publish(ByteBuf content) {
if (end || content.nioBuffer().remaining() == 0) {
content.release();
Expand Down

1 comment on commit eeb7d9a

@olotenko
Copy link

@olotenko olotenko commented on eeb7d9a Feb 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is very likely an incorrect implementation of available. current.readableBytes() is not the same as buffer.remaining(). (as seen here: eeb7d9a#diff-5e270b08fc2f0e8a5b05af285dd84bbdR149-R151)

Don't try so hard. Just check if buffer is null. If not null, return remaining(); otherwise, return 0. There is no requirement to be very accurate.

For example, why return just the number of readableBytes of the first chunk, not all chunks? There is no justification, it is just an estimate.

Alternatively, you could maintain an atomic counter of running total of bytes in all byte buffers published (update at the time publish is called), and subtract the number of bytes consumed by read. That would be simple enough. (Although in my opinion an overkill)

Please sign in to comment.