Java Code Examples for org.jboss.netty.buffer.ChannelBuffer#readSlice()

The following examples show how to use org.jboss.netty.buffer.ChannelBuffer#readSlice() . 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: MongoMessageFrame.java    From usergrid with Apache License 2.0 6 votes vote down vote up
@Override
protected Object decode( ChannelHandlerContext ctx, Channel channel, ChannelBuffer buf ) throws Exception {

    if ( buf.readableBytes() < 4 ) {
        return null;
    }

    // logger.info("Mongo message decoding...");

    int length = buf.getInt( buf.readerIndex() );

    if ( length < 0 ) {
        return null;
    }

    if ( buf.readableBytes() < length ) {
        return null;
    }

    ChannelBuffer frame = buf.readSlice( length );
    return frame;
}
 
Example 2
Source File: RpcUtil.java    From hadoop with Apache License 2.0 5 votes vote down vote up
@Override
protected Object decode(ChannelHandlerContext ctx, Channel channel,
    ChannelBuffer buf) {

  if (buf.readableBytes() < 4)
    return null;

  buf.markReaderIndex();

  byte[] fragmentHeader = new byte[4];
  buf.readBytes(fragmentHeader);
  int length = XDR.fragmentSize(fragmentHeader);
  boolean isLast = XDR.isLastFragment(fragmentHeader);

  if (buf.readableBytes() < length) {
    buf.resetReaderIndex();
    return null;
  }

  ChannelBuffer newFragment = buf.readSlice(length);
  if (currentFrame == null) {
    currentFrame = newFragment;
  } else {
    currentFrame = ChannelBuffers.wrappedBuffer(currentFrame, newFragment);
  }

  if (isLast) {
    ChannelBuffer completeFrame = currentFrame;
    currentFrame = null;
    return completeFrame;
  } else {
    return null;
  }
}
 
Example 3
Source File: RpcUtil.java    From big-c with Apache License 2.0 5 votes vote down vote up
@Override
protected Object decode(ChannelHandlerContext ctx, Channel channel,
    ChannelBuffer buf) {

  if (buf.readableBytes() < 4)
    return null;

  buf.markReaderIndex();

  byte[] fragmentHeader = new byte[4];
  buf.readBytes(fragmentHeader);
  int length = XDR.fragmentSize(fragmentHeader);
  boolean isLast = XDR.isLastFragment(fragmentHeader);

  if (buf.readableBytes() < length) {
    buf.resetReaderIndex();
    return null;
  }

  ChannelBuffer newFragment = buf.readSlice(length);
  if (currentFrame == null) {
    currentFrame = newFragment;
  } else {
    currentFrame = ChannelBuffers.wrappedBuffer(currentFrame, newFragment);
  }

  if (isLast) {
    ChannelBuffer completeFrame = currentFrame;
    currentFrame = null;
    return completeFrame;
  } else {
    return null;
  }
}
 
Example 4
Source File: MongoMessageDecoder.java    From usergrid with Apache License 2.0 4 votes vote down vote up
@Override
protected Object decode( ChannelHandlerContext ctx, Channel channel, ChannelBuffer buf ) throws Exception {

    if ( buf.readableBytes() < 4 ) {
        logger.info( "Needed at least 4 bytes, only " + buf.readableBytes() + " available" );
        return null;
    }

    // logger.info("Mongo message decoding...");

    int length = buf.getInt( buf.readerIndex() );

    if ( length < 0 ) {
        logger.info( "Negative length " + length );
        return null;
    }

    if ( buf.readableBytes() < length ) {
        logger.info( "Needed " + length + " bytes, only " + buf.readableBytes() + " available" );
        return null;
    }

    // logger.info("Attempting to read " + length + " bytes");
    ChannelBuffer frame = buf.readSlice( length );

    int opCode = frame.getInt( frame.readerIndex() + 12 );

    // logger.info("Mongo message opcode " + opCode + " received");

    Message message = null;
    if ( opCode == Message.OP_DELETE ) {
        message = new OpDelete();
    }
    else if ( opCode == Message.OP_GET_MORE ) {
        message = new OpGetMore();
    }
    else if ( opCode == Message.OP_INSERT ) {
        message = new OpInsert();
    }
    else if ( opCode == Message.OP_KILL_CURSORS ) {
        message = new OpKillCursors();
    }
    else if ( opCode == Message.OP_MSG ) {
        message = new OpMsg();
    }
    else if ( opCode == Message.OP_QUERY ) {
        message = new OpQuery();
    }
    else if ( opCode == Message.OP_REPLY ) {
        message = new OpReply();
    }
    else if ( opCode == Message.OP_UPDATE ) {
        message = new OpUpdate();
    }

    if ( message != null ) {
        message.decode( frame );
    }
    else {
        logger.info( "Mongo unrecongnized message opcode " + opCode + " received" );
    }

    // logger.info(message);

    return message;
}