Example usage for io.netty.buffer ByteBuf skipBytes

List of usage examples for io.netty.buffer ByteBuf skipBytes

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf skipBytes.

Prototype

public abstract ByteBuf skipBytes(int length);

Source Link

Document

Increases the current readerIndex by the specified length in this buffer.

Usage

From source file:com.navercorp.nbasearc.gcp.RedisDecoder.java

License:Apache License

/**
 * Process error./*from w  w w  .  ja v  a 2 s .  co  m*/
 *
 * @param is the is
 */
private boolean processError(ByteBuf in) {
    int length = lineLength(in);
    if (length == -1) {
        return false;
    }

    in.skipBytes(length);
    return true;
}

From source file:com.navercorp.nbasearc.gcp.RedisDecoder.java

License:Apache License

/**
 * Process multi bulk reply./*  w  ww  . j  a  va  2  s .  c  o m*/
 *
 * @param is the is
 * @return the list
 */
private boolean processMultiBulkReply(ByteBuf in) {
    final int length = lineLength();
    if (length == 0) {
        return false;
    }
    in.skipBytes(length);

    int num = readInt(length - 2);
    if (num == -1) {
        return true;
    }

    for (int i = 0; i < num; i++) {
        if (hasFrame(in) == false) {
            return false;
        }
    }
    return true;
}

From source file:com.navercorp.nbasearc.gcp.RedisDecoder.java

License:Apache License

/**
 * Process integer./* w  w w.j  a  v a 2  s  .com*/
 *
 * @param is the is
 * @return the long
 */
private boolean processInteger(ByteBuf in) {
    int length = lineLength();
    if (length == 0) {
        return false;
    }

    in.skipBytes(length);
    return true;
}

From source file:com.navercorp.nbasearc.gcp.RedisDecoder.java

License:Apache License

/**
 * Process bulk reply./*from w  w w .j a  va  2 s.  c o  m*/
 *
 * @param is the is
 * @return the byte[]
 */
private boolean processBulkReply(ByteBuf in) {
    int length = lineLength();
    if (length == 0) {
        return false;
    }
    in.skipBytes(length);

    int numBytes = readInt(length - 2); // 2 == \r\n
    if (numBytes == -1) {
        return true;
    }

    if (in.readableBytes() < numBytes + 2) {
        return false;
    }

    in.skipBytes(numBytes + 2);

    return true;
}

From source file:com.navercorp.nbasearc.gcp.RedisDecoder.java

License:Apache License

/**
 * Process status code reply./*from  w  w  w  .j  a  v  a  2s .  com*/
 *
 * @param is the is
 * @return the byte[]
 */
private boolean processStatusCodeReply(ByteBuf in) {
    int length = lineLength();
    if (length == 0) {
        return false;
    }

    in.skipBytes(length);
    return true;
}

From source file:com.netflix.iep.http.NetflixJsonObjectDecoder.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    if (state == ST_CORRUPTED) {
        in.skipBytes(in.readableBytes());
        return;/*from w w w . ja  va  2 s.c o m*/
    }

    if (LOGGER.isTraceEnabled()) {
        byte[] bytes = new byte[in.readableBytes()];
        in.getBytes(in.readerIndex(), bytes, 0, in.readableBytes());
        LOGGER.trace("starting [" + in.readerIndex() + ":" + in.readableBytes() + "]:" + new String(bytes));
    }

    // index of next byte to process.
    int len = this.len;
    int wrtIdx = in.writerIndex();

    for (; in.readerIndex() + len < wrtIdx; len++) {
        if (len > maxObjectLength) {
            // buffer size exceeded maxObjectLength; discarding the complete buffer.
            in.skipBytes(in.readableBytes());
            reset();
            throw new TooLongFrameException(
                    "object length exceeds " + maxObjectLength + ": " + len + " bytes discarded");
        }
        byte c = in.getByte(in.readerIndex() + len);
        if (state == ST_DECODING_NORMAL) {
            decodeByte(c, in, in.readerIndex() + len);

            // All opening braces/brackets have been closed. That's enough to conclude
            // that the JSON object/array is complete.
            if (openBraces == 0) {
                ByteBuf json = extractObject(ctx, in, in.readerIndex(), len + 1);
                if (json != null) {
                    out.add(json);
                }

                // The JSON object/array was extracted => discard the bytes from
                // the input buffer.
                in.readerIndex(in.readerIndex() + len + 1);
                len = 0;
                // Reset the object state to get ready for the next JSON object/text
                // coming along the byte stream.
                reset();
                break;
            }
        } else if (state == ST_DECODING_ARRAY_STREAM) {
            if (len == 0 && Character.isWhitespace(c)) {
                in.skipBytes(1);
                len--;
            }
            decodeByte(c, in, in.readerIndex() + len);
            if (!insideString && (openBraces == 1 && c == ',' || openBraces == 0 && c == ']')) {
                ByteBuf json = extractObject(ctx, in, in.readerIndex(), len);
                if (json != null) {
                    out.add(json);
                }

                in.readerIndex(in.readerIndex() + len + 1);
                len = 0;

                if (c == ']') {
                    reset();
                }
                break;
            }
            // JSON object/array detected. Accumulate bytes until all braces/brackets are closed.
        } else if (c == '{' || c == '[') {
            initDecoding(c);

            if (state == ST_DECODING_ARRAY_STREAM) {
                // Discard the array bracket
                in.skipBytes(1);
                len--;
            }
            // Discard leading spaces in front of a JSON object/array.
        } else if (Character.isWhitespace(c)) {
            in.skipBytes(1);
            len--;
        } else {
            state = ST_CORRUPTED;
            throw new CorruptedFrameException("invalid JSON received at byte position "
                    + (in.readerIndex() + len) + ": " + ByteBufUtil.hexDump(in));
        }
    }

    this.len = len;

    if (LOGGER.isTraceEnabled()) {
        byte[] bytes = new byte[in.readableBytes()];
        in.getBytes(in.readerIndex(), bytes, 0, in.readableBytes());
        LOGGER.trace("remainder [" + in.readerIndex() + ":" + in.readableBytes() + "]:" + new String(bytes));
    }
}

From source file:com.netty.grpc.proxy.demo.handler.GrpcProxyFrontendHandler.java

License:Apache License

private boolean readClientPrefaceString(ByteBuf in) throws Http2Exception {
    ByteBuf clientPrefaceString = Http2CodecUtil.connectionPrefaceBuf();
    int prefaceRemaining = clientPrefaceString.readableBytes();
    int bytesRead = min(in.readableBytes(), prefaceRemaining);

    // If the input so far doesn't match the preface, break the connection.
    if (bytesRead == 0 || !ByteBufUtil.equals(in, in.readerIndex(), clientPrefaceString,
            clientPrefaceString.readerIndex(), bytesRead)) {
        String receivedBytes = hexDump(in, in.readerIndex(),
                min(in.readableBytes(), clientPrefaceString.readableBytes()));
        throw connectionError(PROTOCOL_ERROR,
                "HTTP/2 client preface string missing or corrupt. " + "Hex dump for received bytes: %s",
                receivedBytes);/*from   w w w. ja  v a  2  s.com*/
    }
    in.skipBytes(bytesRead);
    clientPrefaceString.skipBytes(bytesRead);

    if (!clientPrefaceString.isReadable()) {
        // Entire preface has been read.
        clientPrefaceString.release();
        return true;
    }
    return false;
}

From source file:com.netty.test.LengthFieldBasedExFrameDecoder.java

License:Apache License

/**
 * Create a frame out of the {@link ByteBuf} and return it.
 *
 * @param   ctx             the {@link ChannelHandlerContext} which this {@link ByteToMessageDecoder} belongs to
 * @param   in              the {@link ByteBuf} from which to read data
 * @return  frame           the {@link ByteBuf} which represent the frame or {@code null} if no frame could
 *                          be created./*from   w w w .j a  va2  s  .  c  om*/
 */
protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
    if (discardingTooLongFrame) {
        long bytesToDiscard = this.bytesToDiscard;
        int localBytesToDiscard = (int) Math.min(bytesToDiscard, in.readableBytes());
        in.skipBytes(localBytesToDiscard);
        bytesToDiscard -= localBytesToDiscard;
        this.bytesToDiscard = bytesToDiscard;
        failIfNecessary(false);
        return null;
    }

    if (in.readableBytes() < lengthFieldEndOffset) {
        return null;
    }

    int actualLengthFieldOffset = in.readerIndex() + lengthFieldOffset;
    long frameLength = getFrameLength(in, actualLengthFieldOffset);

    if (frameLength < 0) {
        in.skipBytes(lengthFieldEndOffset);
        throw new CorruptedFrameException("negative pre-adjustment length field: " + frameLength);
    }

    frameLength += lengthAdjustment + lengthFieldEndOffset;

    if (frameLength < lengthFieldEndOffset) {
        in.skipBytes(lengthFieldEndOffset);
        throw new CorruptedFrameException("Adjusted frame length (" + frameLength + ") is less "
                + "than lengthFieldEndOffset: " + lengthFieldEndOffset);
    }

    if (frameLength > maxFrameLength) {
        // Enter the discard mode and discard everything received so far.
        discardingTooLongFrame = true;
        tooLongFrameLength = frameLength;
        bytesToDiscard = frameLength - in.readableBytes();
        in.skipBytes(in.readableBytes());
        failIfNecessary(true);
        return null;
    }

    // never overflows because it's less than maxFrameLength
    int frameLengthInt = (int) frameLength;
    if (in.readableBytes() < frameLengthInt) {
        return null;
    }

    if (initialBytesToStrip > frameLengthInt) {
        in.skipBytes(frameLengthInt);
        throw new CorruptedFrameException("Adjusted frame length (" + frameLength + ") is less "
                + "than initialBytesToStrip: " + initialBytesToStrip);
    }
    in.skipBytes(initialBytesToStrip);

    // extract frame
    int readerIndex = in.readerIndex();
    int actualFrameLength = frameLengthInt - initialBytesToStrip;
    ByteBuf frame = extractFrame(ctx, in, readerIndex, actualFrameLength);
    in.readerIndex(readerIndex + actualFrameLength);
    return frame;
}

From source file:com.quavo.osrs.network.protocol.codec.login.world.WorldLoginDecoder.java

License:Open Source License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    if (!in.isReadable()) {
        return;//from w w  w.  j  ava 2  s . co  m
    }

    ByteBuf buffer = ByteBufUtils.encipherRSA(in, EXPONENT, MODULUS);
    int id = buffer.readByte();
    if (id != 1) {
        return;
    }
    int clearanceId = buffer.readByte();
    int[] clientKeys = new int[4];
    for (int index = 0; index < clientKeys.length; index++) {
        clientKeys[index] = buffer.readInt();
    }
    LoginClearance clearance = LoginClearance.getType(clearanceId).get().read(buffer);
    String password = ByteBufUtils.readString(buffer);
    buffer = ByteBufUtils.decipherXTEA(in, clientKeys);
    String username = ByteBufUtils.readString(buffer);
    DisplayMode mode = DisplayMode.getDisplayMode(buffer.readByte()).get();
    int width = buffer.readShort();
    int height = buffer.readShort();
    DisplayInformation display = new DisplayInformation(mode, width, height);
    buffer.skipBytes(24);
    String token = ByteBufUtils.readString(buffer);
    buffer.readInt();
    MachineInformation machineInformation = MachineInformation.decode(buffer);
    buffer.readInt();
    buffer.readInt();
    buffer.readInt();
    buffer.readInt();
    buffer.readByte();
    int[] crc = new int[16];
    for (int index = 0; index < crc.length; index++) {
        crc[index] = buffer.readInt();
    }
    int[] serverKeys = new int[4];
    for (int index = 0; index < serverKeys.length; index++) {
        serverKeys[index] = clientKeys[index] + 50;
    }
    IsaacRandomPair isaacPair = new IsaacRandomPair(new IsaacRandom(serverKeys), new IsaacRandom(clientKeys));

    out.add(new WorldLoginRequest(this, type, username, password, clearance, display, machineInformation, crc,
            token, isaacPair));
}

From source file:com.shelf.messagepack.MessagePackFrameDecoder.java

License:Apache License

public long decodeLength(ByteBuf in, int offset) throws Exception {
    if (discardingTooLongFrame) {
        long bytesToDiscard = this.bytesToDiscard;
        int localBytesToDiscard = (int) Math.min(bytesToDiscard, in.readableBytes());
        in.skipBytes(localBytesToDiscard);
        bytesToDiscard -= localBytesToDiscard;
        this.bytesToDiscard = bytesToDiscard;

        failIfNecessary(false);//from  ww  w .j  av a 2 s  . co  m
    }

    int readerIndex = in.readerIndex() + offset;
    short b = in.getUnsignedByte(readerIndex);
    int ubyte = b & 0xff;

    LOGGER.trace("message: " + toHex(ubyte));

    switch (ubyte) {
    case NIL:
        return 1L;
    case FALSE:
        return 1L;
    case TRUE:
        return 1L;
    case BIN8: {
        short length = in.getUnsignedByte(readerIndex + 1);
        return 2L + length;
    }
    case BIN16: {
        int length = in.getUnsignedShort(readerIndex + 1);
        return 3L + length;
    }
    case BIN32: {
        long length = in.getUnsignedInt(readerIndex + 1);
        return 5L + length;
    }
    case EXT8: {
        short length = in.getUnsignedByte(readerIndex + 1);
        return 3L + length;
    }
    case EXT16: {
        int length = in.getUnsignedShort(readerIndex + 1);
        return 4L + length;
    }
    case EXT32: {
        long length = in.getUnsignedInt(readerIndex + 1);
        return 6L + length;
    }

    case FLOAT32:
        return 5L;
    case FLOAT64:
        return 9L;
    case UINT8:
        return 2L;
    case UINT16:
        return 3L;
    case UINT32:
        return 5L;
    case UINT64:
        return 9L;
    case INT8:
        return 2L;
    case INT16:
        return 3L;
    case INT32:
        return 5L;
    case INT64:
        return 9L;
    case FIXEXT1:
        return 3L;
    case FIXEXT2:
        return 4L;
    case FIXEXT4:
        return 6L;
    case FIXEXT8:
        return 10L;
    case FIXEXT16:
        return 18L;
    case STR8: {
        short length = in.getUnsignedByte(readerIndex + 1);
        return 2L + length;
    }
    case STR16: {
        int length = in.getUnsignedShort(readerIndex + 1);
        return 3L + length;
    }
    case STR32: {
        long length = in.getUnsignedInt(readerIndex + 1);
        return 5L + length;
    }
    case ARRAY16: {
        int elemCount = in.getUnsignedShort(readerIndex + 1);
        return getArraySize(in, 3, offset, elemCount);
    }
    case ARRAY32: {
        long elemCount = in.getUnsignedInt(readerIndex + 1);
        return getArraySize(in, 5, offset, elemCount);
    }
    case MAP16: {
        int elemCount = in.getUnsignedShort(readerIndex + 1);
        return getArraySize(in, 3, offset, elemCount * 2);
    }
    case MAP32: {
        long elemCount = in.getUnsignedInt(readerIndex + 1);
        return getArraySize(in, 5, offset, elemCount * 2);
    }
    default:
        if ((ubyte >> 7) == 0) { //positive fixint
            return 1L;
        } else if ((ubyte >> 4) == 0b1000) { //fixmap
            int elemCount = ubyte & 0b00001111;
            return getArraySize(in, 1, offset, elemCount * 2);
        } else if ((ubyte >> 4) == 0b1001) { //fixarray
            int elemCount = ubyte & 0b00001111;
            return getArraySize(in, 1, offset, elemCount);
        } else if ((ubyte >> 5) == 0b101) { //fixstr
            int length = ubyte & 0b00011111;
            return 1L + length;
        } else if ((ubyte >> 5) == 0b111) { //negative fixint
            return 1L;
        } else {
            throw new CorruptedFrameException("Unknown header byte of message: " + toHex(ubyte));
        }
    }
}