-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reuse byte buffers in BufferingResponseListener #12691
base: jetty-12.0.x
Are you sure you want to change the base?
Conversation
a919e29
to
e6b700d
Compare
@sbordet ptal |
*/ | ||
public BufferingResponseListener(ByteBufferPool byteBufferPool, int maxLength) | ||
{ | ||
this.buffer = new ByteBufferAccumulator(requireNonNull(byteBufferPool, "byteBufferPool is null"), true); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this.buffer = new ByteBufferAccumulator(requireNonNull(byteBufferPool, "byteBufferPool is null"), true); | |
this.buffer = new ByteBufferAccumulator(Objects.requireNonNullElse(byteBufferPool, ByteBufferPool.NON_POOLING), true); |
@@ -55,6 +59,18 @@ public BufferingResponseListener() | |||
*/ | |||
public BufferingResponseListener(int maxLength) | |||
{ | |||
this(ByteBufferPool.NON_POOLING, maxLength); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this(ByteBufferPool.NON_POOLING, maxLength); | |
this(null, maxLength); |
See below
@@ -36,7 +39,8 @@ | |||
public abstract class BufferingResponseListener implements Listener | |||
{ | |||
private final int maxLength; | |||
private ByteBuffer buffer; | |||
private final ByteBufferAccumulator buffer; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This class is deprecated in 12.0. In 12.1 it should probably be a org.eclipse.jetty.io.RetainableByteBuffer.DynamicCapacity
Perhaps it is best to rebase this PR to 12.1 and use that class. 12.1 will soon be the main branch.
return new ByteArrayInputStream(toByteArray()); | ||
} | ||
|
||
private byte[] toByteArray() | ||
{ | ||
synchronized (buffer) | ||
{ | ||
try (ByteBufferAccumulator buffer = this.buffer) | ||
{ | ||
if (buffer.getLength() == 0) | ||
result = new byte[0]; | ||
else | ||
result = buffer.toByteArray(); | ||
} | ||
} | ||
return result; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ideally we would not have to copy all the retained buffers to a single buffer just to stream as a InputStream. We should have a mechanism to do this without this extra copy.
Fixes #12687