changes from review

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
This commit is contained in:
Lachlan Roberts 2020-11-06 14:09:13 +11:00
parent 6e9572215b
commit 3c44df0724
4 changed files with 42 additions and 23 deletions

View File

@ -52,17 +52,17 @@ public class ByteBufferAccumulator implements AutoCloseable
return length;
}
public ByteBuffer getBuffer()
public ByteBuffer ensureBuffer()
{
return getBuffer(DEFAULT_BUFFER_SIZE);
return ensureBuffer(DEFAULT_BUFFER_SIZE);
}
public ByteBuffer getBuffer(int minAllocationSize)
public ByteBuffer ensureBuffer(int minAllocationSize)
{
ByteBuffer buffer = _buffers.isEmpty() ? BufferUtil.EMPTY_BUFFER : _buffers.get(_buffers.size() - 1);
if (BufferUtil.space(buffer) <= MIN_SPACE)
{
buffer = _bufferPool.acquire(minAllocationSize, false);
buffer = _bufferPool.acquire(Math.max(DEFAULT_BUFFER_SIZE, minAllocationSize), false);
_buffers.add(buffer);
}
@ -78,7 +78,7 @@ public class ByteBufferAccumulator implements AutoCloseable
{
while (buffer.hasRemaining())
{
ByteBuffer b = getBuffer(buffer.remaining());
ByteBuffer b = ensureBuffer(buffer.remaining());
int pos = BufferUtil.flipToFill(b);
BufferUtil.put(buffer, b);
BufferUtil.flipToFlush(b, pos);

View File

@ -91,7 +91,7 @@ public class ByteBufferOutputStream2 extends OutputStream
public int size()
{
return _accumulator.getLength();
return _size;
}
@Override
@ -103,18 +103,19 @@ public class ByteBufferOutputStream2 extends OutputStream
@Override
public void write(byte[] b, int off, int len)
{
write(BufferUtil.toBuffer(b, off, len));
releaseAggregateBuffer();
_accumulator.copyBytes(b, off, len);
}
public void write(ByteBuffer buffer)
{
while (buffer.hasRemaining())
{
ByteBuffer lastBuffer = _accumulator.getBuffer(buffer.remaining());
int pos = BufferUtil.flipToFill(lastBuffer);
_size += BufferUtil.put(buffer, lastBuffer);
BufferUtil.flipToFlush(lastBuffer, pos);
}
releaseAggregateBuffer();
_accumulator.copyBuffer(buffer);
}
public void writeTo(ByteBuffer buffer)
{
_accumulator.writeTo(buffer);
}
public void writeTo(OutputStream out) throws IOException
@ -122,15 +123,19 @@ public class ByteBufferOutputStream2 extends OutputStream
_accumulator.writeTo(out);
}
@Override
public void close()
private void releaseAggregateBuffer()
{
if (_combinedByteBuffer != null)
{
_bufferPool.release(_combinedByteBuffer);
_combinedByteBuffer = null;
}
}
@Override
public void close()
{
releaseAggregateBuffer();
_accumulator.close();
_size = 0;
}

View File

@ -25,6 +25,10 @@ import org.eclipse.jetty.io.ByteBufferPool;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.websocket.api.MessageTooLargeException;
/**
* @deprecated use {@link ByteBufferAccumulator} instead.
*/
@Deprecated
public class ByteAccumulator implements AutoCloseable
{
private final ByteBufferAccumulator accumulator;
@ -47,9 +51,19 @@ public class ByteAccumulator implements AutoCloseable
return accumulator.getLength();
}
public ByteBuffer getBuffer(int minAllocationSize)
public ByteBuffer ensureBuffer(int minAllocationSize)
{
return accumulator.getBuffer(minAllocationSize);
return accumulator.ensureBuffer(minAllocationSize);
}
public void readBytes(int read)
{
length += read;
if (length > maxSize)
{
String err = String.format("Resulting message size [%d] is too large for configured max of [%d]", length, maxSize);
throw new MessageTooLargeException(err);
}
}
public void copyChunk(byte[] buf, int offset, int length)
@ -60,13 +74,12 @@ public class ByteAccumulator implements AutoCloseable
public void copyChunk(ByteBuffer buffer)
{
int remaining = buffer.remaining();
if (getLength() + remaining > maxSize)
int length = getLength();
if (length + remaining > maxSize)
{
String err = String.format("Resulting message size [%d] is too large for configured max of [%d]", length + remaining, maxSize);
throw new MessageTooLargeException(err);
}
length += remaining;
accumulator.copyBuffer(buffer);
}
@ -76,6 +89,7 @@ public class ByteAccumulator implements AutoCloseable
BufferUtil.flipToFlush(buffer, 0);
int availableSpace = BufferUtil.space(buffer);
int length = getLength();
if (availableSpace < length)
{
String err = String.format("Not enough space in ByteBuffer remaining [%d] for accumulated buffers length [%d]", availableSpace, length);
@ -89,7 +103,6 @@ public class ByteAccumulator implements AutoCloseable
@Override
public void close()
{
length = 0;
accumulator.close();
}
}

View File

@ -196,9 +196,10 @@ public abstract class CompressExtension extends AbstractExtension
while (true)
{
ByteBuffer buffer = accumulator.getBuffer(DECOMPRESS_BUF_SIZE);
ByteBuffer buffer = accumulator.ensureBuffer(DECOMPRESS_BUF_SIZE);
int read = inflater.inflate(buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.capacity() - buffer.limit());
buffer.limit(buffer.limit() + read);
accumulator.readBytes(read);
if (LOG.isDebugEnabled())
LOG.debug("Decompressed {} bytes into buffer {} from {}", read, BufferUtil.toDetailString(buffer), toDetail(inflater));