blob: ae99401da5164cb2d27e716f43abfb629bf4e54f (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
package org.eclipse.jetty.websocket.io.message;
import java.io.IOException;
import java.nio.ByteBuffer;
import org.eclipse.jetty.io.ByteBufferPool;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.websocket.api.WebSocketPolicy;
import org.eclipse.jetty.websocket.driver.EventMethod;
import org.eclipse.jetty.websocket.io.WebSocketSession;
public class SimpleBinaryMessage implements MessageAppender
{
private final Object websocket;
private final EventMethod onEvent;
private final WebSocketSession session;
private final ByteBufferPool bufferPool;
private final WebSocketPolicy policy;
private final ByteBuffer buf;
private int size;
private boolean finished;
public SimpleBinaryMessage(Object websocket, EventMethod onEvent, WebSocketSession session, ByteBufferPool bufferPool, WebSocketPolicy policy)
{
this.websocket = websocket;
this.onEvent = onEvent;
this.session = session;
this.bufferPool = bufferPool;
this.policy = policy;
this.buf = bufferPool.acquire(policy.getBufferSize(),false);
BufferUtil.clearToFill(this.buf);
finished = false;
}
@Override
public void appendMessage(ByteBuffer payload) throws IOException
{
if (finished)
{
throw new IOException("Cannot append to finished buffer");
}
if (payload == null)
{
// empty payload is valid
return;
}
policy.assertValidBinaryMessageSize(size + payload.remaining());
size += payload.remaining();
// TODO: grow buffer till max binary message size?
BufferUtil.put(payload,buf);
}
@Override
public void messageComplete()
{
BufferUtil.flipToFlush(this.buf,0);
finished = true;
try
{
// notify event
byte data[] = BufferUtil.toArray(this.buf);
this.onEvent.call(websocket,session,data,0,data.length);
}
finally
{
// release buffer (we are done with it now)
bufferPool.release(this.buf);
}
}
}
|