Java Code Examples for io.netty.buffer.ByteBuf.getUnsignedInt()

The following are Jave code examples for showing how to use getUnsignedInt() of the io.netty.buffer.ByteBuf class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: jsf-sdk   File: LengthFieldBasedFrameDecoder.java   Source Code and License Vote up 8 votes
/**
 * Decodes the specified region of the buffer into an unadjusted frame length.  The default implementation is
 * capable of decoding the specified region into an unsigned 8/16/24/32/64 bit integer.  Override this method to
 * decode the length field encoded differently.  Note that this method must not modify the state of the specified
 * buffer (e.g. {@code readerIndex}, {@code writerIndex}, and the content of the buffer.)
 *
 * @throws DecoderException
 *         if failed to decode the specified region
 */
protected long getUnadjustedFrameLength(ByteBuf buf, int offset, int length, ByteOrder order) {
    buf = buf.order(order);
    long frameLength;
    switch (length) {
        case 1:
            frameLength = buf.getUnsignedByte(offset);
            break;
        case 2:
            frameLength = buf.getUnsignedShort(offset);
            break;
        case 3:
            frameLength = buf.getUnsignedMedium(offset);
            break;
        case 4:
            frameLength = buf.getUnsignedInt(offset);
            break;
        case 8:
            frameLength = buf.getLong(offset);
            break;
        default:
            throw new DecoderException(
                    "unsupported lengthFieldLength: " + lengthFieldLength + " (expected: 1, 2, 3, 4, or 8)");
    }
    return frameLength;
}
 
Example 2
Project: JavaNRPC   File: NrpcChannel.java   Source Code and License Vote up 4 votes
public byte[] recvRpcMessage(Socket socket) throws IOException, java.net.SocketTimeoutException {
    BufferedInputStream dataInput = new BufferedInputStream(socket.getInputStream());
    do {
        byte[] temp = new byte[Constants.BUFFER_SIZE];
        int n = dataInput.read(temp);
        if(n>0){
            readBuffer.write(temp,0,n);
        }
        if (n < 0 || n < Constants.BUFFER_SIZE) {
            break;
        }
    } while (true);
    byte[] readContent = readBuffer.toByteArray();
    ByteBuf in = Unpooled.buffer(readContent.length);
    in.writeBytes(readContent);
    int readable = in.readableBytes();
    int pos = 0;
    if (readable >= Constants.HEADER_SIZE) {
        // 检查Magic number
        if (in.getByte(pos) == 'N' && in.getByte(pos + 1) == 'R' && in.getByte(pos + 2) == 'P' && in.getByte(pos + 3) == 'C') {
        } else {
            logger.error("Wrong magic number!");
            closeSocket();
            throw new IOException("Wrong magic number!");
        }
        int totalSize = (int)in.getUnsignedInt(pos + 4);
        byte type = in.getByte(pos + 8);
        if (type == 0) {
            logger.debug("one request");
        } else if (type == 1) {
            logger.debug("one response");
        }
        if (readable < totalSize) {
            logger.warn("Not enough data");
            closeSocket();
            throw new IOException("Not enough data!");
        }
        // 开始解码报文
        in.skipBytes(Constants.HEADER_SIZE);
        byte[] dest = new byte[totalSize - Constants.HEADER_SIZE];
        in.readBytes(dest);
        return dest;
    } else {
        logger.warn("Not enough data header");
        closeSocket();
        throw new IOException("Not enough data header!");
    }
}
 
Example 3
Project: JavaNRPC   File: NrpcDecoder.java   Source Code and License Vote up 4 votes
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    int readable = in.readableBytes();
    if (readable > Constants.MAX_FRAME_SIZE) {
        logger.error("Frame too big : {}, max is {}",new Object[]{readable,Constants.MAX_FRAME_SIZE});
        in.skipBytes(readable);
        ctx.close();
        throw new TooLongFrameException("Frame too big!");
    }
    int pos = 0;
    if(readable >= Constants.HEADER_SIZE){
        // 检查Magic number
        if(in.getByte(pos) == 'N' && in.getByte(pos+1) == 'R' && in.getByte(pos+2) == 'P' && in.getByte(pos+3) == 'C'){
        }else{
            logger.error("Wrong magic number!");
            in.skipBytes(readable);
            ctx.close();
            throw new WrongMagicNumException("Wrong magic number!");
        }
        int totalSize = (int)in.getUnsignedInt(pos+4);
        byte type = in.getByte(pos+8);
        if(type == 0){
            logger.debug("one request");
        }else if(type == 1){
            logger.debug("one response");
        }
        if(readable < totalSize ){
            return;
        }
        // 开始解码报文
        try{
            in.skipBytes(Constants.HEADER_SIZE);
            byte[] dest = new byte[totalSize-Constants.HEADER_SIZE];
            in.readBytes(dest);
            ByteArrayInputStream destByteInputStream = new ByteArrayInputStream(dest);
            NrpcMeta nrpcMeta = NrpcMeta.parseFrom(destByteInputStream);
            out.add(nrpcMeta);
        }catch(Exception ex) {
            logger.error(ex.getMessage(), ex);
            ctx.close();
            throw new DecodeException("NrpcMeta decode failed!");
        }
    }
}