mirror of
https://github.com/jetty/jetty.project.git
synced 2025-03-04 12:59:30 +00:00
Fixes #7688 - Read data to native memory from HttpInput
Added `HttpInput.read(ByteBuffer buffer)`, so that applications can provide a native memory mapped `ByteBuffer` to read into.
This commit is contained in:
parent
40e7d6a716
commit
5117a58974
@ -137,6 +137,26 @@ public class HttpInput extends ServletInputStream implements Runnable
|
||||
return consumed;
|
||||
}
|
||||
|
||||
private int get(Content content, ByteBuffer des)
|
||||
{
|
||||
var capacity = des.remaining();
|
||||
var src = content.getByteBuffer();
|
||||
if (src.remaining() > capacity)
|
||||
{
|
||||
int limit = src.limit();
|
||||
src.limit(src.position() + capacity);
|
||||
des.put(src);
|
||||
src.limit(limit);
|
||||
}
|
||||
else
|
||||
{
|
||||
des.put(src);
|
||||
}
|
||||
var consumed = capacity - des.remaining();
|
||||
_contentConsumed.add(consumed);
|
||||
return consumed;
|
||||
}
|
||||
|
||||
public long getContentConsumed()
|
||||
{
|
||||
return _contentConsumed.sum();
|
||||
@ -248,6 +268,16 @@ public class HttpInput extends ServletInputStream implements Runnable
|
||||
|
||||
@Override
|
||||
public int read(byte[] b, int off, int len) throws IOException
|
||||
{
|
||||
return read(null, b, off, len);
|
||||
}
|
||||
|
||||
public int read(ByteBuffer buffer) throws IOException
|
||||
{
|
||||
return read(buffer, null, -1, -1);
|
||||
}
|
||||
|
||||
private int read(ByteBuffer buffer, byte[] b, int off, int len) throws IOException
|
||||
{
|
||||
try (AutoLock lock = _contentProducer.lock())
|
||||
{
|
||||
@ -259,7 +289,7 @@ public class HttpInput extends ServletInputStream implements Runnable
|
||||
throw new IllegalStateException("read on unready input");
|
||||
if (!content.isSpecial())
|
||||
{
|
||||
int read = get(content, b, off, len);
|
||||
int read = buffer == null ? get(content, b, off, len) : get(content, buffer);
|
||||
if (LOG.isDebugEnabled())
|
||||
LOG.debug("read produced {} byte(s) {}", read, this);
|
||||
if (content.isEmpty())
|
||||
|
Loading…
x
Reference in New Issue
Block a user