428232 - Rework batch mode / buffering in websocket.

Avoid to create a new ByteBuffer just to create the mask integer,
saves allocation cost.
This commit is contained in:
Simone Bordet 2014-02-18 13:59:53 +01:00
parent 61b4b0f029
commit 0786da6cd0
2 changed files with 11 additions and 4 deletions

View File

@ -284,7 +284,9 @@ public class Generator
{
byte[] mask = frame.getMask();
buffer.put(mask);
int maskInt = ByteBuffer.wrap(mask).getInt();
int maskInt = 0;
for (byte maskByte : mask)
maskInt = (maskInt << 8) + (maskByte & 0xFF);
// perform data masking here
ByteBuffer payload = frame.getPayload();

View File

@ -24,7 +24,8 @@ import org.eclipse.jetty.websocket.api.extensions.Frame;
public class DeMaskProcessor implements PayloadProcessor
{
private byte maskBytes[];
private byte[] maskBytes;
private int maskInt;
private int maskOffset;
@Override
@ -35,14 +36,14 @@ public class DeMaskProcessor implements PayloadProcessor
return;
}
int maskInt = ByteBuffer.wrap(maskBytes).getInt();
int maskInt = this.maskInt;
int start = payload.position();
int end = payload.limit();
int offset = this.maskOffset;
int remaining;
while ((remaining = end - start) > 0)
{
if (remaining >= 4 && (offset % 4) == 0)
if (remaining >= 4 && (offset & 3) == 0)
{
payload.putInt(start,payload.getInt(start) ^ maskInt);
start += 4;
@ -61,6 +62,10 @@ public class DeMaskProcessor implements PayloadProcessor
public void reset(byte mask[])
{
this.maskBytes = mask;
int maskInt = 0;
for (byte maskByte : maskBytes)
maskInt = (maskInt << 8) + (maskByte & 0xFF);
this.maskInt = maskInt;
this.maskOffset = 0;
}