From 5117a58974a0ca9ea87380e554383dae68a56c96 Mon Sep 17 00:00:00 2001 From: jiang-cao Date: Sat, 26 Mar 2022 00:57:52 +0800 Subject: [PATCH] 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. --- .../org/eclipse/jetty/server/HttpInput.java | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/HttpInput.java b/jetty-server/src/main/java/org/eclipse/jetty/server/HttpInput.java index a2c06481759..fdcfa878182 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/HttpInput.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/HttpInput.java @@ -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())