allow writing directly into the ByteAccumulator
Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
This commit is contained in:
parent
a3c3e24cab
commit
7bcae9968b
|
@ -69,6 +69,22 @@ public class ByteBufferAccumulator implements AutoCloseable
|
|||
return buffer;
|
||||
}
|
||||
|
||||
public void copyBytes(byte[] buf, int offset, int length)
|
||||
{
|
||||
copyBuffer(BufferUtil.toBuffer(buf, offset, length));
|
||||
}
|
||||
|
||||
public void copyBuffer(ByteBuffer buffer)
|
||||
{
|
||||
while (buffer.hasRemaining())
|
||||
{
|
||||
ByteBuffer b = getBuffer(buffer.remaining());
|
||||
int pos = BufferUtil.flipToFill(b);
|
||||
BufferUtil.put(buffer, b);
|
||||
BufferUtil.flipToFlush(b, pos);
|
||||
}
|
||||
}
|
||||
|
||||
public void writeTo(ByteBuffer buffer)
|
||||
{
|
||||
int pos = BufferUtil.flipToFill(buffer);
|
||||
|
|
|
@ -1,19 +1,19 @@
|
|||
//
|
||||
// ========================================================================
|
||||
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others.
|
||||
// ========================================================================
|
||||
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others.
|
||||
// ------------------------------------------------------------------------
|
||||
// All rights reserved. This program and the accompanying materials
|
||||
// are made available under the terms of the Eclipse Public License v1.0
|
||||
// and Apache License v2.0 which accompanies this distribution.
|
||||
//
|
||||
// This program and the accompanying materials are made available under
|
||||
// the terms of the Eclipse Public License 2.0 which is available at
|
||||
// https://www.eclipse.org/legal/epl-2.0
|
||||
// The Eclipse Public License is available at
|
||||
// http://www.eclipse.org/legal/epl-v10.html
|
||||
//
|
||||
// This Source Code may also be made available under the following
|
||||
// Secondary Licenses when the conditions for such availability set
|
||||
// forth in the Eclipse Public License, v. 2.0 are satisfied:
|
||||
// the Apache License v2.0 which is available at
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
// The Apache License v2.0 is available at
|
||||
// http://www.opensource.org/licenses/apache2.0.php
|
||||
//
|
||||
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
|
||||
// ========================================================================
|
||||
// You may elect to redistribute this code under either of these licenses.
|
||||
// ========================================================================
|
||||
//
|
||||
|
||||
package org.eclipse.jetty.io;
|
||||
|
|
|
@ -1,19 +1,19 @@
|
|||
//
|
||||
// ========================================================================
|
||||
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others.
|
||||
// ========================================================================
|
||||
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others.
|
||||
// ------------------------------------------------------------------------
|
||||
// All rights reserved. This program and the accompanying materials
|
||||
// are made available under the terms of the Eclipse Public License v1.0
|
||||
// and Apache License v2.0 which accompanies this distribution.
|
||||
//
|
||||
// This program and the accompanying materials are made available under
|
||||
// the terms of the Eclipse Public License 2.0 which is available at
|
||||
// https://www.eclipse.org/legal/epl-2.0
|
||||
// The Eclipse Public License is available at
|
||||
// http://www.eclipse.org/legal/epl-v10.html
|
||||
//
|
||||
// This Source Code may also be made available under the following
|
||||
// Secondary Licenses when the conditions for such availability set
|
||||
// forth in the Eclipse Public License, v. 2.0 are satisfied:
|
||||
// the Apache License v2.0 which is available at
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
// The Apache License v2.0 is available at
|
||||
// http://www.opensource.org/licenses/apache2.0.php
|
||||
//
|
||||
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
|
||||
// ========================================================================
|
||||
// You may elect to redistribute this code under either of these licenses.
|
||||
// ========================================================================
|
||||
//
|
||||
|
||||
package org.eclipse.jetty.io;
|
||||
|
|
|
@ -44,7 +44,12 @@ public class ByteAccumulator implements AutoCloseable
|
|||
|
||||
public int getLength()
|
||||
{
|
||||
return length;
|
||||
return accumulator.getLength();
|
||||
}
|
||||
|
||||
public ByteBuffer getBuffer(int minAllocationSize)
|
||||
{
|
||||
return accumulator.getBuffer(minAllocationSize);
|
||||
}
|
||||
|
||||
public void copyChunk(byte[] buf, int offset, int length)
|
||||
|
@ -54,26 +59,23 @@ public class ByteAccumulator implements AutoCloseable
|
|||
|
||||
public void copyChunk(ByteBuffer buffer)
|
||||
{
|
||||
if (length + buffer.remaining() > maxSize)
|
||||
int remaining = buffer.remaining();
|
||||
if (getLength() + remaining > maxSize)
|
||||
{
|
||||
String err = String.format("Resulting message size [%d] is too large for configured max of [%d]", this.length + length, maxSize);
|
||||
String err = String.format("Resulting message size [%d] is too large for configured max of [%d]", length + remaining, maxSize);
|
||||
throw new MessageTooLargeException(err);
|
||||
}
|
||||
|
||||
while (buffer.hasRemaining())
|
||||
{
|
||||
ByteBuffer b = accumulator.getBuffer(buffer.remaining());
|
||||
int pos = BufferUtil.flipToFill(b);
|
||||
this.length += BufferUtil.put(buffer, b);
|
||||
BufferUtil.flipToFlush(b, pos);
|
||||
}
|
||||
length += remaining;
|
||||
accumulator.copyBuffer(buffer);
|
||||
}
|
||||
|
||||
public void transferTo(ByteBuffer buffer)
|
||||
{
|
||||
if (BufferUtil.space(buffer) < length)
|
||||
int availableSpace = BufferUtil.space(buffer);
|
||||
if (availableSpace < length)
|
||||
{
|
||||
String err = String.format("Not enough space in ByteBuffer remaining [%d] for accumulated buffers length [%d]", BufferUtil.space(buffer), length);
|
||||
String err = String.format("Not enough space in ByteBuffer remaining [%d] for accumulated buffers length [%d]", availableSpace, length);
|
||||
throw new IllegalArgumentException(err);
|
||||
}
|
||||
|
||||
|
|
|
@ -180,14 +180,10 @@ public abstract class CompressExtension extends AbstractExtension
|
|||
|
||||
protected void decompress(ByteAccumulator accumulator, ByteBuffer buf) throws DataFormatException
|
||||
{
|
||||
if ((buf == null) || (!buf.hasRemaining()))
|
||||
{
|
||||
if (BufferUtil.isEmpty(buf))
|
||||
return;
|
||||
}
|
||||
byte[] output = new byte[DECOMPRESS_BUF_SIZE];
|
||||
|
||||
Inflater inflater = getInflater();
|
||||
|
||||
while (buf.hasRemaining() && inflater.needsInput())
|
||||
{
|
||||
if (!supplyInput(inflater, buf))
|
||||
|
@ -197,22 +193,16 @@ public abstract class CompressExtension extends AbstractExtension
|
|||
return;
|
||||
}
|
||||
|
||||
int read;
|
||||
while ((read = inflater.inflate(output)) >= 0)
|
||||
while (true)
|
||||
{
|
||||
if (read == 0)
|
||||
{
|
||||
if (LOG.isDebugEnabled())
|
||||
LOG.debug("Decompress: read 0 {}", toDetail(inflater));
|
||||
ByteBuffer buffer = accumulator.getBuffer(DECOMPRESS_BUF_SIZE);
|
||||
int read = inflater.inflate(buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.capacity() - buffer.limit());
|
||||
buffer.limit(buffer.limit() + read);
|
||||
if (LOG.isDebugEnabled())
|
||||
LOG.debug("Decompressed {} bytes into buffer {} from {}", read, BufferUtil.toDetailString(buffer), toDetail(inflater));
|
||||
|
||||
if (read <= 0)
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
// do something with output
|
||||
if (LOG.isDebugEnabled())
|
||||
LOG.debug("Decompressed {} bytes: {}", read, toDetail(inflater));
|
||||
accumulator.copyChunk(output, 0, read);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue