use of io.undertow.util.ImmediatePooledByteBuffer in project undertow by undertow-io.
the class WebSocket07FrameSinkChannel method createFrameHeader.
@Override
protected SendFrameHeader createFrameHeader() {
byte b0 = 0;
//if writes are shutdown this is the final fragment
if (isFinalFrameQueued()) {
// set FIN
b0 |= 1 << 7;
}
/*
Known extensions (i.e. compression) should not modify RSV bit on continuation bit.
*/
byte opCode = opCode();
int rsv = opCode == WebSocket07Channel.OPCODE_CONT ? 0 : getRsv();
b0 |= (rsv & 7) << 4;
b0 |= opCode & 0xf;
final ByteBuffer header = ByteBuffer.allocate(14);
byte maskKey = 0;
if (masker != null) {
maskKey |= 1 << 7;
}
long payloadSize = getBuffer().remaining();
if (payloadSize > 125 && opCode == WebSocket07Channel.OPCODE_PING) {
throw WebSocketMessages.MESSAGES.invalidPayloadLengthForPing(payloadSize);
}
if (payloadSize <= 125) {
header.put(b0);
header.put((byte) ((payloadSize | maskKey) & 0xFF));
} else if (payloadSize <= 0xFFFF) {
header.put(b0);
header.put((byte) ((126 | maskKey) & 0xFF));
header.put((byte) (payloadSize >>> 8 & 0xFF));
header.put((byte) (payloadSize & 0xFF));
} else {
header.put(b0);
header.put((byte) ((127 | maskKey) & 0xFF));
header.putLong(payloadSize);
}
if (masker != null) {
//generate a new key for this frame
int maskingKey = random.nextInt();
header.put((byte) ((maskingKey >> 24) & 0xFF));
header.put((byte) ((maskingKey >> 16) & 0xFF));
header.put((byte) ((maskingKey >> 8) & 0xFF));
header.put((byte) ((maskingKey & 0xFF)));
masker.setMaskingKey(maskingKey);
//do any required masking
ByteBuffer buf = getBuffer();
masker.beforeWrite(buf, buf.position(), buf.remaining());
}
header.flip();
return new SendFrameHeader(0, new ImmediatePooledByteBuffer(header));
}
use of io.undertow.util.ImmediatePooledByteBuffer in project undertow by undertow-io.
the class PerMessageDeflateFunction method toArrayBacked.
private PooledByteBuffer toArrayBacked(ByteBuffer buffer, ByteBufferPool pool) {
if (pool.getBufferSize() < buffer.remaining()) {
return new ImmediatePooledByteBuffer(ByteBuffer.wrap(Buffers.take(buffer)));
}
PooledByteBuffer newBuf = pool.getArrayBackedPool().allocate();
newBuf.getBuffer().put(buffer);
newBuf.getBuffer().flip();
return newBuf;
}
Aggregations