Improve performance of small websocket messages
Signed-off-by: Sergey Tselovalnikov <sergeicelov@gmail.com>
This commit is contained in:
parent
13458ab515
commit
d371c1498c
|
@ -23,20 +23,22 @@ import java.io.IOException;
|
|||
import java.nio.ByteBuffer;
|
||||
|
||||
import org.eclipse.jetty.util.BufferUtil;
|
||||
import org.eclipse.jetty.util.ByteArrayOutputStream2;
|
||||
import org.eclipse.jetty.websocket.common.events.EventDriver;
|
||||
|
||||
public class SimpleBinaryMessage implements MessageAppender
|
||||
{
|
||||
private static final ByteArrayOutputStream2 EMPTY_BUF = new ByteArrayOutputStream2(0);
|
||||
private static final int BUFFER_SIZE = 65535;
|
||||
private final EventDriver onEvent;
|
||||
protected final ByteArrayOutputStream out;
|
||||
protected ByteArrayOutputStream2 out;
|
||||
private int size;
|
||||
protected boolean finished;
|
||||
|
||||
public SimpleBinaryMessage(EventDriver onEvent)
|
||||
{
|
||||
this.onEvent = onEvent;
|
||||
this.out = new ByteArrayOutputStream(BUFFER_SIZE);
|
||||
this.out = EMPTY_BUF;
|
||||
finished = false;
|
||||
}
|
||||
|
||||
|
@ -57,6 +59,8 @@ public class SimpleBinaryMessage implements MessageAppender
|
|||
onEvent.getPolicy().assertValidBinaryMessageSize(size + payload.remaining());
|
||||
size += payload.remaining();
|
||||
|
||||
if (out == EMPTY_BUF)
|
||||
out = isLast ? new ByteArrayOutputStream2(size) : new ByteArrayOutputStream2(BUFFER_SIZE);
|
||||
BufferUtil.writeTo(payload, out);
|
||||
}
|
||||
|
||||
|
@ -64,7 +68,11 @@ public class SimpleBinaryMessage implements MessageAppender
|
|||
public void messageComplete()
|
||||
{
|
||||
finished = true;
|
||||
byte[] data = out.toByteArray();
|
||||
byte[] data;
|
||||
if (out.getCount() == out.getBuf().length)
|
||||
data = out.getBuf();
|
||||
else
|
||||
data = out.toByteArray();
|
||||
onEvent.onBinaryMessage(data);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue