diff --git a/jetty-io/src/main/java/org/eclipse/jetty/io/ByteBufferAccumulator.java b/jetty-io/src/main/java/org/eclipse/jetty/io/ByteBufferAccumulator.java index 6de5baf3380..1ee285f4d07 100644 --- a/jetty-io/src/main/java/org/eclipse/jetty/io/ByteBufferAccumulator.java +++ b/jetty-io/src/main/java/org/eclipse/jetty/io/ByteBufferAccumulator.java @@ -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); diff --git a/jetty-io/src/main/java/org/eclipse/jetty/io/ByteBufferOutputStream2.java b/jetty-io/src/main/java/org/eclipse/jetty/io/ByteBufferOutputStream2.java index 0d18d617d3c..1addc9b1d09 100644 --- a/jetty-io/src/main/java/org/eclipse/jetty/io/ByteBufferOutputStream2.java +++ b/jetty-io/src/main/java/org/eclipse/jetty/io/ByteBufferOutputStream2.java @@ -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; diff --git a/jetty-io/src/main/java/org/eclipse/jetty/io/NullByteBufferPool.java b/jetty-io/src/main/java/org/eclipse/jetty/io/NullByteBufferPool.java index a68d4a28ffb..41938ae1af4 100644 --- a/jetty-io/src/main/java/org/eclipse/jetty/io/NullByteBufferPool.java +++ b/jetty-io/src/main/java/org/eclipse/jetty/io/NullByteBufferPool.java @@ -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; diff --git a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/extensions/compress/ByteAccumulator.java b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/extensions/compress/ByteAccumulator.java index 9863634b1ed..77dc5606a91 100644 --- a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/extensions/compress/ByteAccumulator.java +++ b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/extensions/compress/ByteAccumulator.java @@ -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); } diff --git a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/extensions/compress/CompressExtension.java b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/extensions/compress/CompressExtension.java index a18a07f2098..957d4e6477a 100644 --- a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/extensions/compress/CompressExtension.java +++ b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/extensions/compress/CompressExtension.java @@ -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); - } } }