From 1381ee4eebe71a584529e77b39029f781a7e035a Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Mon, 16 Jul 2012 09:55:00 -0700 Subject: [PATCH] Adding copy constructor --- .../websocket/protocol/WebSocketFrame.java | 39 ++++++++++++++++--- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/protocol/WebSocketFrame.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/protocol/WebSocketFrame.java index 17f9d49d1a8..bbc7179046d 100644 --- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/protocol/WebSocketFrame.java +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/protocol/WebSocketFrame.java @@ -87,10 +87,13 @@ public class WebSocketFrame implements Frame private OpCode opcode = null; private boolean masked = false; private byte mask[]; + /** + * The payload data. + *

+ * It is assumed to always be in FLUSH mode (ready to read) in this object. + */ private ByteBuffer data; - private boolean continuation = false; - private int continuationIndex = 0; /** @@ -110,6 +113,33 @@ public class WebSocketFrame implements Frame this.opcode = opcode; } + /** + * Copy constructor for the websocket frame. + *

+ * Note: the underlying payload is merely a {@link ByteBuffer#slice()} of the input frame. + * + * @param copy + * the websocket to copy. + */ + public WebSocketFrame(WebSocketFrame copy) + { + fin = copy.rsv1; + rsv1 = copy.rsv2; + rsv2 = copy.rsv2; + rsv3 = copy.rsv3; + opcode = copy.opcode; + masked = copy.masked; + mask = null; + if (copy.mask != null) + { + mask = new byte[copy.mask.length]; + System.arraycopy(copy.mask,0,mask,0,mask.length); + } + data = copy.data.slice(); + continuationIndex = copy.continuationIndex; + continuation = copy.continuation; + } + public void assertValid() { if (opcode.isControlFrame()) @@ -341,7 +371,6 @@ public class WebSocketFrame implements Frame BufferUtil.clearToFill(data); data.put(buf,0,len); BufferUtil.flipToFlush(data,0); - BufferUtil.flipToFill(data); return this; } @@ -368,9 +397,9 @@ public class WebSocketFrame implements Frame } data = ByteBuffer.allocate(len); - BufferUtil.clearToFill(data); + BufferUtil.clear(data); data.put(buf,0,len); - BufferUtil.flipToFill(data); + BufferUtil.flipToFlush(data,0); return this; }