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

The following examples show how to use io.netty.buffer.ByteBuf#getUnsignedInt() . 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: LengthFieldBasedFrameDecoder.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
/**
 * 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
 * 将缓冲区的指定区域解码为未调整的帧长度。默认实现可以将指定的区域解码为无符号的8/16/24/32/64位整数。重写此方法,以不同的方式解码长度字段。注意,此方法不能修改指定缓冲区的状态(例如readerIndex、writerIndex和缓冲区的内容)。
 */
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
Source File: HermesLengthFieldBasedFrameDecoder.java    From hermes with Apache License 2.0 6 votes vote down vote up
/**
 * 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 3
Source File: ByteBufUtils.java    From graylog-plugin-netflow with Apache License 2.0 6 votes vote down vote up
public static long getUnsignedInteger(final ByteBuf buf, final int offset, final int length) {
    switch (length) {
        case 1:
            return buf.getUnsignedByte(offset);
        case 2:
            return buf.getUnsignedShort(offset);
        case 3:
            return buf.getUnsignedMedium(offset);
        case 4:
            return buf.getUnsignedInt(offset);
        case 8:
            return buf.getLong(offset) & 0x00000000ffffffffL;
        default:
            return 0L;
    }
}
 
Example 4
Source File: LengthFieldBasedFrameDecoder.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
/**
 * 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 5
Source File: DiscardServerHandler.java    From HttpProxy with MIT License 5 votes vote down vote up
@Override
public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {

    ByteBuf buf=(ByteBuf)msg;
    long a= buf.getUnsignedInt(0);
    System.out.println(a);


    byte[] bytes=new byte[buf.readableBytes()];
    buf.readBytes(bytes);
    System.out.println(new String(bytes));
    // discard
}
 
Example 6
Source File: ProtobufDecoder.java    From ffwd with Apache License 2.0 5 votes vote down vote up
private void decodeOne(ChannelHandlerContext ctx, ByteBuf in, List<Object> out)
    throws Exception {
    final int version = (int) in.getUnsignedInt(0);
    final long totalLength = in.getUnsignedInt(4);

    if (totalLength > MAX_FRAME_SIZE) {
        log.error(
            "Received frame with length (" + totalLength + ") larger than maximum allowed ( " +
                MAX_FRAME_SIZE + ")");
        in.clear();
        return;
    }

    // datagram underflow
    if (in.readableBytes() < totalLength) {
        log.error(
            "Received frame of shorter length (" + in.readableBytes() + ") than reported (" +
                totalLength + ")");
        in.clear();
        return;
    }

    in.skipBytes(8);

    final Object frame;

    switch (version) {
        case 0:
            frame = decodeFrame0(in);
            break;
        default:
            throw new IllegalArgumentException("Unsupported protocol version: " + version);
    }

    if (frame != null) {
        out.add(frame);
    }
}
 
Example 7
Source File: RiemannFrameDecoder.java    From ffwd with Apache License 2.0 5 votes vote down vote up
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out)
    throws Exception {
    if (in.readableBytes() < 4) {
        return;
    }

    final long length = in.getUnsignedInt(0);

    if (length > MAX_SIZE) {
        throw new CorruptedFrameException(
            String.format("frame size (%s) larger than max (%d)", length, MAX_SIZE));
    }

    final int intLength = (int) length;

    if (in.readableBytes() < (4 + length)) {
        return;
    }

    in.skipBytes(4);
    final ByteBuf frame = in.readBytes(intLength);

    try {
        out.add(serializer.parse0(frame));
    } finally {
        frame.release();
    }
}
 
Example 8
Source File: BaseHttpAndBlynkUnificationHandler.java    From blynk-server with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
    // Will use the first 5 bytes to detect a protocol.
    if (in.readableBytes() < 5) {
        return;
    }

    //we can't simply read 5 bytes as 1 number, so split on 4 bytes and 1 byte read
    long header4Bytes = in.getUnsignedInt(0);
    short lastByteOfHeader = in.getUnsignedByte(4);

    ChannelPipeline pipeline = ctx.pipeline();
    buildPipeline(pipeline, header4Bytes, lastByteOfHeader).remove(this);
}