Cleanup around FrameParser.copyBuffer()

This commit is contained in:
Joakim Erdfelt 2012-06-19 15:17:14 -07:00
parent dcfa524211
commit 8bab5beb2b
3 changed files with 16 additions and 2 deletions

View File

@ -54,11 +54,23 @@ public abstract class FrameParser<T extends BaseFrame>
this.settings = settings; this.settings = settings;
} }
/**
* Copy the bytes from one buffer to the other, demasking the content if necessary.
*
* @param src
* the source {@link ByteBuffer}
* @param dest
* the destination {@link ByteBuffer}
* @param length
* the length of bytes to worry about
* @return the number of bytes copied
*/
protected int copyBuffer(ByteBuffer src, ByteBuffer dest, int length) protected int copyBuffer(ByteBuffer src, ByteBuffer dest, int length)
{ {
int amt = Math.min(length,src.remaining()); int amt = Math.min(length,src.remaining());
if (getFrame().isMasked()) if (getFrame().isMasked())
{ {
// Demask the content
byte mask[] = getFrame().getMask(); byte mask[] = getFrame().getMask();
for (int i = 0; i < amt; i++) for (int i = 0; i < amt; i++)
{ {
@ -67,6 +79,7 @@ public abstract class FrameParser<T extends BaseFrame>
} }
else else
{ {
// Copy the content as-is
byte b[] = new byte[amt]; byte b[] = new byte[amt];
src.get(b,0,amt); src.get(b,0,amt);
dest.put(b,0,amt); dest.put(b,0,amt);

View File

@ -38,7 +38,8 @@ public class PingPayloadParser extends FrameParser<PingFrame>
payload = ByteBuffer.allocate(payloadLength); payload = ByteBuffer.allocate(payloadLength);
} }
copyBuffer(buffer,payload,payloadLength); copyBuffer(buffer,payload,payload.remaining());
if (payload.position() >= payloadLength) if (payload.position() >= payloadLength)
{ {
frame.setPayload(payload); frame.setPayload(payload);

View File

@ -35,7 +35,7 @@ public class TextPayloadParser extends FrameParser<TextFrame>
payload = ByteBuffer.allocate(payloadLength); payload = ByteBuffer.allocate(payloadLength);
} }
copyBuffer(buffer,payload,payloadLength); copyBuffer(buffer,payload,payload.remaining());
if (payload.position() >= payloadLength) if (payload.position() >= payloadLength)
{ {