diff --git a/jetty-io/src/main/java/org/eclipse/jetty/io/ByteBufferPoolOutputStream.java b/jetty-io/src/main/java/org/eclipse/jetty/io/ByteBufferPoolOutputStream.java deleted file mode 100644 index 3dfa67791c6..00000000000 --- a/jetty-io/src/main/java/org/eclipse/jetty/io/ByteBufferPoolOutputStream.java +++ /dev/null @@ -1,111 +0,0 @@ -// -// ======================================================================== -// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others. -// -// 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 -// -// 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 -// -// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 -// ======================================================================== -// - -package org.eclipse.jetty.io; - -import java.io.IOException; -import java.io.OutputStream; -import java.nio.ByteBuffer; -import java.util.ArrayList; -import java.util.Objects; - -import org.eclipse.jetty.util.BufferUtil; - -public class ByteBufferPoolOutputStream extends OutputStream -{ - private final ByteBufferPool bufferPool; - private final ArrayList buffers; - private final boolean direct; - private final int acquireSize; - - private ByteBuffer aggregateBuffer; - private int size = 0; - - public ByteBufferPoolOutputStream(ByteBufferPool bufferPool, int acquireSize, boolean direct) - { - this.buffers = new ArrayList<>(); - this.direct = direct; - this.bufferPool = Objects.requireNonNull(bufferPool); - this.acquireSize = acquireSize; - if (acquireSize <= 0) - throw new IllegalArgumentException(); - - this.buffers.add(bufferPool.acquire(acquireSize, direct)); - } - - @Override - public void write(int b) throws IOException - { - write(new byte[]{(byte)b}, 0, 1); - } - - @Override - public void write(byte[] b, int off, int len) throws IOException - { - write(ByteBuffer.wrap(b, off, len)); - } - - public void write(ByteBuffer data) - { - while (data.hasRemaining()) - { - ByteBuffer buffer = buffers.get(buffers.size() - 1); - size += BufferUtil.append(buffer, data); - if (!buffer.hasRemaining()) - buffers.add(bufferPool.acquire(acquireSize, direct)); - } - } - - public int size() - { - return size; - } - - public ByteBuffer toByteBuffer() - { - releaseAggregate(); - aggregateBuffer = bufferPool.acquire(size, direct); - for (ByteBuffer data : buffers) - { - BufferUtil.append(aggregateBuffer, data); - } - return aggregateBuffer; - } - - public byte[] toByteArray() - { - return BufferUtil.toArray(toByteBuffer()); - } - - private void releaseAggregate() - { - if (aggregateBuffer != null) - { - bufferPool.release(aggregateBuffer); - aggregateBuffer = null; - } - } - - @Override - public void close() - { - releaseAggregate(); - for (ByteBuffer buffer : buffers) - bufferPool.release(buffer); - } -} diff --git a/jetty-websocket/websocket-javax-tests/src/test/java/org/eclipse/jetty/websocket/javax/tests/client/MessageReceivingTest.java b/jetty-websocket/websocket-javax-tests/src/test/java/org/eclipse/jetty/websocket/javax/tests/client/MessageReceivingTest.java index c901acc48d2..6da56e34586 100644 --- a/jetty-websocket/websocket-javax-tests/src/test/java/org/eclipse/jetty/websocket/javax/tests/client/MessageReceivingTest.java +++ b/jetty-websocket/websocket-javax-tests/src/test/java/org/eclipse/jetty/websocket/javax/tests/client/MessageReceivingTest.java @@ -477,7 +477,7 @@ public class MessageReceivingTest @Override public void onMessage(ByteBuffer message) { - final String stringResult = new String(message.array()); + final String stringResult = BufferUtil.toString(message); messageQueue.offer(stringResult); } } diff --git a/jetty-websocket/websocket-util/src/main/java/org/eclipse/jetty/websocket/util/messages/ByteArrayMessageSink.java b/jetty-websocket/websocket-util/src/main/java/org/eclipse/jetty/websocket/util/messages/ByteArrayMessageSink.java index 4763ee16946..6e02ef13c4b 100644 --- a/jetty-websocket/websocket-util/src/main/java/org/eclipse/jetty/websocket/util/messages/ByteArrayMessageSink.java +++ b/jetty-websocket/websocket-util/src/main/java/org/eclipse/jetty/websocket/util/messages/ByteArrayMessageSink.java @@ -56,6 +56,14 @@ public class ByteArrayMessageSink extends AbstractMessageSink { try { + size += frame.getPayloadLength(); + long maxBinaryMessageSize = session.getMaxBinaryMessageSize(); + if (maxBinaryMessageSize > 0 && size > maxBinaryMessageSize) + { + throw new MessageTooLargeException(String.format("Binary message too large: (actual) %,d > (configured max binary message size) %,d", + size, maxBinaryMessageSize)); + } + // If we are fin and no OutputStream has been created we don't need to aggregate. if (frame.isFin() && (out == null)) { @@ -71,7 +79,6 @@ public class ByteArrayMessageSink extends AbstractMessageSink return; } - aggregatePayload(frame); if (frame.isFin()) { @@ -100,17 +107,8 @@ public class ByteArrayMessageSink extends AbstractMessageSink if (frame.hasPayload()) { ByteBuffer payload = frame.getPayload(); - size += payload.remaining(); - long maxBinaryMessageSize = session.getMaxBinaryMessageSize(); - if (maxBinaryMessageSize > 0 && size > maxBinaryMessageSize) - { - throw new MessageTooLargeException(String.format("Binary message too large: (actual) %,d > (configured max binary message size) %,d", - size, maxBinaryMessageSize)); - } - if (out == null) out = new ByteArrayOutputStream(BUFFER_SIZE); - BufferUtil.writeTo(payload, out); } } diff --git a/jetty-websocket/websocket-util/src/main/java/org/eclipse/jetty/websocket/util/messages/ByteBufferMessageSink.java b/jetty-websocket/websocket-util/src/main/java/org/eclipse/jetty/websocket/util/messages/ByteBufferMessageSink.java index 1b4d6094701..85f9f8e2af3 100644 --- a/jetty-websocket/websocket-util/src/main/java/org/eclipse/jetty/websocket/util/messages/ByteBufferMessageSink.java +++ b/jetty-websocket/websocket-util/src/main/java/org/eclipse/jetty/websocket/util/messages/ByteBufferMessageSink.java @@ -57,6 +57,14 @@ public class ByteBufferMessageSink extends AbstractMessageSink { try { + size += frame.getPayloadLength(); + long maxBinaryMessageSize = session.getMaxBinaryMessageSize(); + if (maxBinaryMessageSize > 0 && size > maxBinaryMessageSize) + { + throw new MessageTooLargeException(String.format("Binary message too large: (actual) %,d > (configured max binary message size) %,d", + size, maxBinaryMessageSize)); + } + // If we are fin and no OutputStream has been created we don't need to aggregate. if (frame.isFin() && (out == null)) { @@ -95,13 +103,6 @@ public class ByteBufferMessageSink extends AbstractMessageSink if (frame.hasPayload()) { ByteBuffer payload = frame.getPayload(); - size += payload.remaining(); - long maxBinaryMessageSize = session.getMaxBinaryMessageSize(); - if (maxBinaryMessageSize > 0 && size > maxBinaryMessageSize) - { - throw new MessageTooLargeException(String.format("Binary message too large: (actual) %,d > (configured max binary message size) %,d", - size, maxBinaryMessageSize)); - } if (out == null) out = new ByteArrayOutputStream(BUFFER_SIZE); diff --git a/jetty-websocket/websocket-util/src/main/java/org/eclipse/jetty/websocket/util/messages/StringMessageSink.java b/jetty-websocket/websocket-util/src/main/java/org/eclipse/jetty/websocket/util/messages/StringMessageSink.java index bd029bc7914..10c2e59022e 100644 --- a/jetty-websocket/websocket-util/src/main/java/org/eclipse/jetty/websocket/util/messages/StringMessageSink.java +++ b/jetty-websocket/websocket-util/src/main/java/org/eclipse/jetty/websocket/util/messages/StringMessageSink.java @@ -45,6 +45,14 @@ public class StringMessageSink extends AbstractMessageSink { try { + size += frame.getPayloadLength(); + long maxTextMessageSize = session.getMaxTextMessageSize(); + if (maxTextMessageSize > 0 && size > maxTextMessageSize) + { + throw new MessageTooLargeException(String.format("Text message too large: (actual) %,d > (configured max text message size) %,d", + size, maxTextMessageSize)); + } + // If we are fin and out has not been created we don't need to aggregate. if (frame.isFin() && (out == null)) { @@ -83,13 +91,6 @@ public class StringMessageSink extends AbstractMessageSink if (frame.hasPayload()) { ByteBuffer payload = frame.getPayload(); - size += frame.getPayloadLength(); - long maxTextMessageSize = session.getMaxTextMessageSize(); - if (maxTextMessageSize > 0 && size > maxTextMessageSize) - { - throw new MessageTooLargeException(String.format("Text message too large: (actual) %,d > (configured max text message size) %,d", - size, maxTextMessageSize)); - } if (out == null) out = new Utf8StringBuilder(BUFFER_SIZE);