Issue #4571 - fix broken tests

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
This commit is contained in:
Lachlan Roberts 2020-02-19 14:44:33 +11:00
parent 71b11f0887
commit b2eddff228
5 changed files with 25 additions and 136 deletions

View File

@ -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<ByteBuffer> 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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);

View File

@ -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);