Adding copy constructor

This commit is contained in:
Joakim Erdfelt 2012-07-16 09:55:00 -07:00
parent 1cf2035580
commit 1381ee4eeb
1 changed files with 34 additions and 5 deletions

View File

@ -87,10 +87,13 @@ public class WebSocketFrame implements Frame
private OpCode opcode = null;
private boolean masked = false;
private byte mask[];
/**
* The payload data.
* <p>
* 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.
* <p>
* 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;
}