Java Code Examples for org.jboss.netty.buffer.ChannelBuffers#EMPTY_BUFFER

The following examples show how to use org.jboss.netty.buffer.ChannelBuffers#EMPTY_BUFFER . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FrameDecoder.java    From android-netty with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the internal cumulative buffer of this decoder. You usually do
 * not need to access the internal buffer directly to write a decoder. Use
 * it only when you must use it at your own risk.
 */
protected ChannelBuffer internalBuffer() {
	ChannelBuffer buf = cumulation;
	if (buf == null) {
		return ChannelBuffers.EMPTY_BUFFER;
	}
	return buf;
}
 
Example 2
Source File: PagedBytesReference.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public ChannelBuffer toChannelBuffer() {
    // nothing to do
    if (length == 0) {
        return ChannelBuffers.EMPTY_BUFFER;
    }

    ChannelBuffer[] buffers;
    ChannelBuffer currentBuffer = null;
    BytesRef ref = new BytesRef();
    int pos = 0;

    // are we a slice?
    if (offset != 0) {
        // remaining size of page fragment at offset
        int fragmentSize = Math.min(length, PAGE_SIZE - (offset % PAGE_SIZE));
        bytearray.get(offset, fragmentSize, ref);
        currentBuffer = ChannelBuffers.wrappedBuffer(ref.bytes, ref.offset, fragmentSize);
        pos += fragmentSize;
    }

    // no need to create a composite buffer for a single page
    if (pos == length && currentBuffer != null) {
        return currentBuffer;
    }

    // a slice > pagesize will likely require extra buffers for initial/trailing fragments
    int numBuffers = countRequiredBuffers((currentBuffer != null ? 1 : 0), length - pos);

    buffers = new ChannelBuffer[numBuffers];
    int bufferSlot = 0;

    if (currentBuffer != null) {
        buffers[bufferSlot] = currentBuffer;
        bufferSlot++;
    }

    // handle remainder of pages + trailing fragment
    while (pos < length) {
        int remaining = length - pos;
        int bulkSize = (remaining > PAGE_SIZE) ? PAGE_SIZE : remaining;
        bytearray.get(offset + pos, bulkSize, ref);
        currentBuffer = ChannelBuffers.wrappedBuffer(ref.bytes, ref.offset, bulkSize);
        buffers[bufferSlot] = currentBuffer;
        bufferSlot++;
        pos += bulkSize;
    }

    // this would indicate that our numBuffer calculation is off by one.
    assert (numBuffers == bufferSlot);

    return ChannelBuffers.wrappedBuffer(NettyUtils.DEFAULT_GATHERING, buffers);
}
 
Example 3
Source File: Hybi10WebSocketFrameEncoder.java    From restcommander with Apache License 2.0 4 votes vote down vote up
@Override
 protected Object encode(ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception {

     if (msg instanceof DefaultWebSocketFrame) {
         final DefaultWebSocketFrame frame = (DefaultWebSocketFrame) msg;

         ChannelBuffer data = frame.getBinaryData();
if (data == null) {
	data = ChannelBuffers.EMPTY_BUFFER;
}

         byte opcode;
         // TODO: Close and CONTINUATION
         if(frame instanceof Ping) {
             opcode = OPCODE_PING;
         } else if(frame instanceof Pong) {
             opcode = OPCODE_PONG;
         } else {
             opcode = frame.isText() ? OPCODE_TEXT : OPCODE_BINARY;
         }


         int length = data.readableBytes();

int b0 = 0;
   b0 |= (1 << 7);
         // TODO: RSV, for now it is set to 0
b0 |= (0 % 8) << 4;
b0 |= opcode % 128;

         ChannelBuffer header;
         ChannelBuffer body;


         // TODO: if there is no mask
         int maskLength = 4;
         if (length <= 125) {
             header = ChannelBuffers.buffer(2 + maskLength);
             header.writeByte(b0);
             byte b = (byte) (0x80 | (byte) length);
             header.writeByte(b);
         } else if (length <= 0xFFFF) {
             header = ChannelBuffers.buffer(4 + maskLength);
             header.writeByte(b0);
             header.writeByte(0x80 | 126);
             header.writeByte((length >>> 8) & 0xFF);
             header.writeByte((length) & 0xFF);
         } else {
             header = ChannelBuffers.buffer(10 + maskLength);
             header.writeByte(b0);
             header.writeByte(0x80 | 127);
             header.writeLong(length);
         }

         Integer random = (int) (Math.random() * Integer.MAX_VALUE);
         byte[] mask = ByteBuffer.allocate(4).putInt(random).array();
         header.writeBytes(mask);

         body = ChannelBuffers.buffer(length);
         int counter = 0;
         while (data.readableBytes() > 0) {
             byte byteData = data.readByte();
             body.writeByte(byteData ^ mask[+counter++ % 4]);
         }
         return ChannelBuffers.wrappedBuffer(header, body);
     }

     return msg;
 }