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:
jiang-cao 2022-03-26 00:57:52 +08:00 committed by GitHub
parent 40e7d6a716
commit 5117a58974
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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())