Remove isLast flag from WebSocketMessage

This commit is contained in:
Rossen Stoyanchev
2013-05-14 14:32:25 -04:00
parent 9ca03cf772
commit 278a5924cb
6 changed files with 12 additions and 90 deletions

View File

@@ -31,53 +31,24 @@ public final class BinaryMessage extends WebSocketMessage<ByteBuffer> {
private byte[] bytes;
private final boolean last;
/**
* Create a new {@link BinaryMessage} instance.
* @param payload a non-null payload
* @param isLast if the message is the last of a series of partial messages
*/
public BinaryMessage(ByteBuffer payload) {
this(payload, true);
}
/**
* Create a new {@link BinaryMessage} instance.
* @param payload a non-null payload
* @param isLast if the message is the last of a series of partial messages
*/
public BinaryMessage(ByteBuffer payload, boolean isLast) {
super(payload);
this.bytes = null;
this.last = isLast;
}
/**
* Create a new {@link BinaryMessage} instance.
* @param payload a non-null payload
* @param isLast if the message is the last of a series of partial messages
*/
public BinaryMessage(byte[] payload) {
this(payload, true);
}
/**
* Create a new {@link BinaryMessage} instance.
* @param payload a non-null payload
* @param isLast if the message is the last of a series of partial messages
*/
public BinaryMessage(byte[] payload, boolean isLast) {
this(payload, 0, (payload == null ? 0 : payload.length), isLast);
}
/**
* Create a new {@link BinaryMessage} instance by wrapping an existing byte array.
* @param payload a non-null payload, NOTE: this value is not copied so care must be
* taken not to modify the array.
* @param isLast if the message is the last of a series of partial messages
*/
public BinaryMessage(byte[] payload, int offset, int len) {
this(payload, offset, len, true);
this(payload, 0, (payload == null ? 0 : payload.length));
}
/**
@@ -88,20 +59,11 @@ public final class BinaryMessage extends WebSocketMessage<ByteBuffer> {
* @param len the length of the array considered for the payload
* @param isLast if the message is the last of a series of partial messages
*/
public BinaryMessage(byte[] payload, int offset, int len, boolean isLast) {
public BinaryMessage(byte[] payload, int offset, int len) {
super(payload != null ? ByteBuffer.wrap(payload, offset, len) : null);
if(offset == 0 && len == payload.length) {
this.bytes = payload;
}
this.last = isLast;
}
/**
* Returns if this is the last part in a series of partial messages. If this is
* not a partial message this method will return {@code true}.
*/
public boolean isLast() {
return this.last;
}
/**

View File

@@ -72,9 +72,4 @@ public interface WebSocketHandler {
*/
void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception;
/**
* Whether this WebSocketHandler wishes to receive messages broken up in parts.
*/
boolean isStreaming();
}

View File

@@ -75,24 +75,12 @@ public class StandardEndpointAdapter extends Endpoint {
handleTextMessage(session, message);
}
});
if (!this.handler.isStreaming()) {
session.addMessageHandler(new MessageHandler.Whole<ByteBuffer>() {
@Override
public void onMessage(ByteBuffer message) {
handleBinaryMessage(session, message, true);
}
});
}
else {
session.addMessageHandler(new MessageHandler.Partial<ByteBuffer>() {
@Override
public void onMessage(ByteBuffer messagePart, boolean isLast) {
handleBinaryMessage(session, messagePart, isLast);
}
});
}
session.addMessageHandler(new MessageHandler.Whole<ByteBuffer>() {
@Override
public void onMessage(ByteBuffer message) {
handleBinaryMessage(session, message);
}
});
}
private void handleTextMessage(javax.websocket.Session session, String payload) {
@@ -105,8 +93,8 @@ public class StandardEndpointAdapter extends Endpoint {
}
}
private void handleBinaryMessage(javax.websocket.Session session, ByteBuffer payload, boolean isLast) {
BinaryMessage binaryMessage = new BinaryMessage(payload, isLast);
private void handleBinaryMessage(javax.websocket.Session session, ByteBuffer payload) {
BinaryMessage binaryMessage = new BinaryMessage(payload);
try {
this.handler.handleMessage(this.wsSession, binaryMessage);
}

View File

@@ -68,9 +68,4 @@ public class WebSocketHandlerAdapter implements WebSocketHandler {
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
}
@Override
public boolean isStreaming() {
return false;
}
}

View File

@@ -57,22 +57,9 @@ public class PerConnectionWebSocketHandler implements WebSocketHandler, BeanFact
private final Map<WebSocketSession, WebSocketHandler> handlers =
new ConcurrentHashMap<WebSocketSession, WebSocketHandler>();
private final boolean streaming;
public PerConnectionWebSocketHandler(Class<? extends WebSocketHandler> handlerType) {
this(handlerType, false);
}
public PerConnectionWebSocketHandler(Class<? extends WebSocketHandler> handlerType, boolean isStreaming) {
this.provider = new BeanCreatingHandlerProvider<WebSocketHandler>(handlerType);
this.streaming = isStreaming;
}
@Override
public boolean isStreaming() {
return this.streaming;
}
@Override

View File

@@ -62,11 +62,6 @@ public class WebSocketHandlerDecorator implements WebSocketHandler {
this.delegate.afterConnectionClosed(session, closeStatus);
}
@Override
public boolean isStreaming() {
return this.delegate.isStreaming();
}
@Override
public String toString() {
return getClass().getSimpleName() + " [delegate=" + this.delegate + "]";