Skip to content

Commit

Permalink
Merge pull request #6194 from eclipse/jetty-9.4.x-FileBufferedRespons…
Browse files Browse the repository at this point in the history
…eHandler

Create FileBufferedResponseHandler to buffer responses into a file.
  • Loading branch information
lachlan-roberts committed Apr 21, 2021
2 parents 39fe2ec + eca8edc commit a4124b4
Show file tree
Hide file tree
Showing 7 changed files with 995 additions and 95 deletions.
Expand Up @@ -325,8 +325,9 @@ public static boolean hasNoBody(int status)
switch (status)
{
case NO_CONTENT_204:
case NOT_MODIFIED_304:
case RESET_CONTENT_205:
case PARTIAL_CONTENT_206:
case NOT_MODIFIED_304:
return true;

default:
Expand Down
20 changes: 14 additions & 6 deletions jetty-io/src/main/java/org/eclipse/jetty/io/ByteArrayEndPoint.java
Expand Up @@ -47,6 +47,7 @@ public class ByteArrayEndPoint extends AbstractEndPoint
static final Logger LOG = Log.getLogger(ByteArrayEndPoint.class);
static final InetAddress NOIP;
static final InetSocketAddress NOIPPORT;
private static final int MAX_BUFFER_SIZE = Integer.MAX_VALUE - 1024;

static
{
Expand Down Expand Up @@ -80,6 +81,7 @@ public void run()
private final Locker _locker = new Locker();
private final Condition _hasOutput = _locker.newCondition();
private final Queue<ByteBuffer> _inQ = new ArrayDeque<>();
private final int _outputSize;
private ByteBuffer _out;
private boolean _growOutput;

Expand Down Expand Up @@ -129,7 +131,8 @@ public ByteArrayEndPoint(Scheduler timer, long idleTimeoutMs, ByteBuffer input,
super(timer);
if (BufferUtil.hasContent(input))
addInput(input);
_out = output == null ? BufferUtil.allocate(1024) : output;
_outputSize = (output == null) ? 1024 : output.capacity();
_out = output == null ? BufferUtil.allocate(_outputSize) : output;
setIdleTimeout(idleTimeoutMs);
onOpen();
}
Expand Down Expand Up @@ -296,7 +299,7 @@ public ByteBuffer takeOutput()
try (Locker.Lock lock = _locker.lock())
{
b = _out;
_out = BufferUtil.allocate(b.capacity());
_out = BufferUtil.allocate(_outputSize);
}
getWriteFlusher().completeWrite();
return b;
Expand All @@ -322,7 +325,7 @@ public ByteBuffer waitForOutput(long time, TimeUnit unit) throws InterruptedExce
return null;
}
b = _out;
_out = BufferUtil.allocate(b.capacity());
_out = BufferUtil.allocate(_outputSize);
}
getWriteFlusher().completeWrite();
return b;
Expand Down Expand Up @@ -436,9 +439,14 @@ public boolean flush(ByteBuffer... buffers) throws IOException
BufferUtil.compact(_out);
if (b.remaining() > BufferUtil.space(_out))
{
ByteBuffer n = BufferUtil.allocate(_out.capacity() + b.remaining() * 2);
BufferUtil.append(n, _out);
_out = n;
// Don't grow larger than MAX_BUFFER_SIZE to avoid memory issues.
if (_out.capacity() < MAX_BUFFER_SIZE)
{
long newBufferCapacity = Math.min((long)(_out.capacity() + b.remaining() * 1.5), MAX_BUFFER_SIZE);
ByteBuffer n = BufferUtil.allocate(Math.toIntExact(newBufferCapacity));
BufferUtil.append(n, _out);
_out = n;
}
}
}

Expand Down
Expand Up @@ -642,6 +642,7 @@ public void close() throws IOException
catch (Throwable t)
{
onWriteComplete(true, t);
throw t;
}
}
}
Expand Down

0 comments on commit a4124b4

Please sign in to comment.