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.github.sparkfy.network.protocol.MessageWithHeader.java

License:Apache License

private int copyByteBuf(ByteBuf buf, WritableByteChannel target) throws IOException {
    int written = target.write(buf.nioBuffer());
    buf.skipBytes(written);
    return written;
}

From source file:com.github.sylvek.wsmqttfwd.decoder.Utils.java

License:Open Source License

public static boolean checkHeaderAvailability(ByteBuf in) {
    if (in.readableBytes() < 1) {
        return false;
    }//from w w w  .j  av  a2s  .c  o m
    //byte h1 = in.get();
    //byte messageType = (byte) ((h1 & 0x00F0) >> 4);
    in.skipBytes(1); //skip the messageType byte

    int remainingLength = Utils.decodeRemainingLength(in);
    if (remainingLength == -1) {
        return false;
    }

    //check remaining length
    if (in.readableBytes() < remainingLength) {
        return false;
    }

    //return messageType == type ? MessageDecoderResult.OK : MessageDecoderResult.NOT_OK;
    return true;
}

From source file:com.growcontrol.common.netty.JsonObjectDecoder.java

License:Apache License

@Override
protected void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out)
        throws Exception {
    if (this.state == ST_CORRUPTED) {
        in.skipBytes(in.readableBytes());
        return;//from   ww  w. j a v  a 2  s  .  co  m
    }
    // index of next byte to process.
    int idx = this.idx;
    final int wrtIdx = in.writerIndex();
    if (wrtIdx > this.maxObjectLength) {
        // buffer size exceeded maxObjectLength; discarding the complete buffer.
        in.skipBytes(in.readableBytes());
        reset();
        throw new TooLongFrameException(
                "object length exceeds " + this.maxObjectLength + ": " + wrtIdx + " bytes discarded");
    }
    for (/* use current idx */; idx < wrtIdx; idx++) {
        final byte c = in.getByte(idx);
        if (this.state == ST_DECODING_NORMAL) {
            decodeByte(c, in, idx);
            // All opening braces/brackets have been closed. That's enough to conclude
            // that the JSON object/array is complete.
            if (this.openBraces == 0) {
                ByteBuf json = extractObject(ctx, in, in.readerIndex(), idx + 1 - in.readerIndex());
                if (json != null) {
                    out.add(json);
                }
                // The JSON object/array was extracted => discard the bytes from
                // the input buffer.
                in.readerIndex(idx + 1);
                // Reset the object state to get ready for the next JSON object/text
                // coming along the byte stream.
                reset();
            }
        } else if (this.state == ST_DECODING_ARRAY_STREAM) {
            decodeByte(c, in, idx);
            if (!this.insideString && (this.openBraces == 1 && c == ',' || this.openBraces == 0 && c == ']')) {
                // skip leading spaces. No range check is needed and the loop will terminate
                // because the byte at position idx is not a whitespace.
                for (int i = in.readerIndex(); Character.isWhitespace(in.getByte(i)); i++) {
                    in.skipBytes(1);
                }
                // skip trailing spaces.
                int idxNoSpaces = idx - 1;
                while (idxNoSpaces >= in.readerIndex() && Character.isWhitespace(in.getByte(idxNoSpaces))) {
                    idxNoSpaces--;
                }
                ByteBuf json = extractObject(ctx, in, in.readerIndex(), idxNoSpaces + 1 - in.readerIndex());
                if (json != null) {
                    out.add(json);
                }
                in.readerIndex(idx + 1);
                if (c == ']') {
                    reset();
                }
            }
            // JSON object/array detected. Accumulate bytes until all braces/brackets are closed.
        } else if (c == '{' || c == '[') {
            initDecoding(c);
            // Discard the array bracket
            if (this.state == ST_DECODING_ARRAY_STREAM)
                in.skipBytes(1);
            // Discard leading spaces in front of a JSON object/array.
        } else if (Character.isWhitespace(c)) {
            in.skipBytes(1);
        } else {
            this.state = ST_CORRUPTED;
            throw new CorruptedFrameException(
                    "invalid JSON received at byte position " + idx + ": " + ByteBufUtil.hexDump(in));
        }
    }
    if (in.readableBytes() == 0)
        this.idx = 0;
    else
        this.idx = idx;
}

From source file:com.ibasco.agql.protocols.valve.source.query.handlers.SourceRconPacketDecoder.java

License:Open Source License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {

    final String separator = "=================================================================================================";

    //TODO: Move all code logic below to SourceRconPacketBuilder

    log.debug(separator);/*from  w w  w.  j  av a  2  s  .  c o  m*/
    log.debug(" ({}) DECODING INCOMING DATA : Bytes Received = {} {}", index.incrementAndGet(),
            in.readableBytes(), index.get() > 1 ? "[Continuation]" : "");
    log.debug(separator);

    String desc = StringUtils.rightPad("Minimum allowable size?", PAD_SIZE);
    //Verify we have the minimum allowable size
    if (in.readableBytes() < 14) {
        log.debug(" [ ] {} = NO (Actual Readable Bytes: {})", desc, in.readableBytes());
        return;
    }
    log.debug(" [x] {} = YES (Actual Readable Bytes: {})", desc, in.readableBytes());

    //Reset if this happens to be not a valid source rcon packet
    in.markReaderIndex();

    //Read and Verify size
    desc = StringUtils.rightPad("Bytes received at least => than the \"declared\" size?", PAD_SIZE);
    int size = in.readIntLE();
    int readableBytes = in.readableBytes();
    if (readableBytes < size) {
        log.debug(" [ ] {} = NO (Declared Size: {}, Actual Bytes Read: {})", desc, readableBytes, size);
        in.resetReaderIndex();
        return;
    }
    log.debug(" [x] {} = YES (Declared Size: {}, Actual Bytes Read: {})", desc, readableBytes, size);

    //Read and verify request id
    desc = StringUtils.rightPad("Request Id within the valid range?", PAD_SIZE);
    int id = in.readIntLE();
    if (!(id == -1 || id == SourceRconUtil.RCON_TERMINATOR_RID || SourceRconUtil.isValidRequestId(id))) {
        log.debug(" [ ] {} = NO (Actual: {})", desc, id);
        in.resetReaderIndex();
        return;
    }
    log.debug(" [x] {} = YES (Actual: {})", desc, id);

    //Read and verify request type
    desc = StringUtils.rightPad("Valid response type?", PAD_SIZE);
    int type = in.readIntLE();
    if (get(type) == null) {
        log.debug(" [ ] {} = NO (Actual: {})", desc, type);
        in.resetReaderIndex();
        return;
    }
    log.debug(" [x] {} = YES (Actual: {} = {})", desc, type, SourceRconResponseType.get(type));

    //Read and verify body
    desc = StringUtils.rightPad("Contains Body?", PAD_SIZE);
    int bodyLength = in.bytesBefore((byte) 0);
    String body = StringUtils.EMPTY;
    if (bodyLength <= 0)
        log.debug(" [ ] {} = NO", desc);
    else {
        body = in.readCharSequence(bodyLength, StandardCharsets.UTF_8).toString();
        log.debug(" [x] {} = YES (Length: {}, Body: {})", desc, bodyLength,
                StringUtils.replaceAll(StringUtils.truncate(body, 30), "\n", "\\\\n"));
    }

    //Peek at the last two bytes and verify that they are null-bytes
    byte bodyTerminator = in.getByte(in.readerIndex());
    byte packetTerminator = in.getByte(in.readerIndex() + 1);

    desc = StringUtils.rightPad("Contains TWO null-terminating bytes at the end?", PAD_SIZE);

    //Make sure the last two bytes are NULL bytes (request id: 999 is reserved for split packet responses)
    if ((bodyTerminator != 0 || packetTerminator != 0) && (id == SourceRconUtil.RCON_TERMINATOR_RID)) {
        log.debug("Skipping {} bytes", in.readableBytes());
        in.skipBytes(in.readableBytes());
        return;
    } else if (bodyTerminator != 0 || packetTerminator != 0) {
        log.debug(" [ ] {} = NO (Actual: Body Terminator = {}, Packet Terminator = {})", desc, bodyTerminator,
                packetTerminator);
        in.resetReaderIndex();
        return;
    } else {
        log.debug(" [x] {} = YES (Actual: Body Terminator = {}, Packet Terminator = {})", desc, bodyTerminator,
                packetTerminator);
        //All is good, skip the last two bytes
        if (in.readableBytes() >= 2)
            in.skipBytes(2);
    }

    //At this point, we can now construct a packet
    log.debug(" [x] Status: PASS (Size = {}, Id = {}, Type = {}, Remaining Bytes = {}, Body Size = {})", size,
            id, type, in.readableBytes(), bodyLength);
    log.debug(separator);

    //Reset the index
    index.set(0);

    //Construct the response packet and send to the next handlers
    SourceRconResponsePacket responsePacket;

    //Did we receive a terminator packet?
    if (this.terminatingPacketsEnabled && id == SourceRconUtil.RCON_TERMINATOR_RID
            && StringUtils.isBlank(body)) {
        responsePacket = new SourceRconTermResponsePacket();
    } else {
        responsePacket = SourceRconPacketBuilder.getResponsePacket(type);
    }

    if (responsePacket != null) {
        responsePacket.setSize(size);
        responsePacket.setId(id);
        responsePacket.setType(type);
        responsePacket.setBody(body);
        log.debug(
                "Decode Complete. Passing response for request id : '{}' to the next handler. Remaining bytes ({})",
                id, in.readableBytes());
        out.add(responsePacket);
    }
}

From source file:com.ibasco.agql.protocols.valve.source.query.logger.SourceLogListenHandler.java

License:Open Source License

@Override
protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception {
    ByteBuf data = msg.content();
    if (data.readableBytes() > 6 && data.readIntLE() == -1) {
        byte[] raw = new byte[data.readableBytes() - 2];
        data.readBytes(raw);//from  www .  j a v a2s  . c o  m
        data.skipBytes(2);
        //Pass to the callback
        if (logEventCallback != null)
            logEventCallback.accept(new SourceLogEntry(new String(raw, Charsets.UTF_8), msg.sender()));
    }
}

From source file:com.jamierf.jsonrpc.util.JsonObjectDecoder.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  ww w . j ava  2s .  co m*/
    }

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

    if (wrtIdx > maxObjectLength) {
        // buffer size exceeded maxObjectLength; discarding the complete buffer.
        in.skipBytes(in.readableBytes());
        reset();
        throw new TooLongFrameException(
                "object length exceeds " + maxObjectLength + ": " + wrtIdx + " bytes discarded");
    }

    for (/* use current idx */; idx < wrtIdx; idx++) {
        byte c = in.getByte(idx);
        if (state == ST_DECODING_NORMAL) {
            decodeByte(c, in, idx);

            // 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(), idx + 1 - in.readerIndex());
                if (json != null) {
                    out.add(json);
                }

                // The JSON object/array was extracted => discard the bytes from
                // the input buffer.
                in.readerIndex(idx + 1);
                // Reset the object state to get ready for the next JSON object/text
                // coming along the byte stream.
                reset();
            }
        } else if (state == ST_DECODING_ARRAY_STREAM) {
            decodeByte(c, in, idx);

            if (!insideString && (openBraces == 1 && c == ',' || openBraces == 0 && c == ']')) {
                // skip leading spaces. No range check is needed and the loop will terminate
                // because the byte at position idx is not a whitespace.
                for (int i = in.readerIndex(); Character.isWhitespace(in.getByte(i)); i++) {
                    in.skipBytes(1);
                }

                // skip trailing spaces.
                int idxNoSpaces = idx - 1;
                while (idxNoSpaces >= in.readerIndex() && Character.isWhitespace(in.getByte(idxNoSpaces))) {
                    idxNoSpaces--;
                }

                ByteBuf json = extractObject(ctx, in, in.readerIndex(), idxNoSpaces + 1 - in.readerIndex());
                if (json != null) {
                    out.add(json);
                }

                in.readerIndex(idx + 1);

                if (c == ']') {
                    reset();
                }
            }
            // 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);
            }
            // Discard leading spaces in front of a JSON object/array.
        } else if (Character.isWhitespace(c)) {
            in.skipBytes(1);
        } else {
            state = ST_CORRUPTED;
            throw new CorruptedFrameException(
                    "invalid JSON received at byte position " + idx + ": " + ByteBufUtil.hexDump(in));
        }
    }

    if (in.readableBytes() == 0) {
        this.idx = 0;
    } else {
        this.idx = idx;
    }
}

From source file:com.kael.surf.net.codec.LengthFieldBasedFrameDecoder.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  www .  ja  v  a2  s  .  co m*/
 */
protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
    Integer packetNum = ctx.attr(countKey).get();
    if (packetNum == null || packetNum <= 100) {
        ctx.attr(countKey).set(packetNum == null ? 1 : packetNum + 1);

        int readableBytes = in.readableBytes();
        if (skipPolicy && readableBytes >= lengthFieldEndOffset) {
            if (readableBytes >= POLICY_STR_BYTES.length) {
                byte[] dst = new byte[POLICY_STR_BYTES.length];
                in.getBytes(in.readerIndex(), dst);
                if (isEqual(dst, POLICY_STR_BYTES)) {
                    in.skipBytes(POLICY_STR_BYTES.length);
                    return new String(dst);
                }
            } else {
                byte[] dst = new byte[readableBytes];
                in.getBytes(in.readerIndex(), dst);
                if (isEqual(dst, POLICY_STR_BYTES)) {
                    return null;
                }
            }
        }

        if (skipTencentGTWHead && readableBytes >= lengthFieldEndOffset) {
            if (readableBytes >= GET_BYTES.length) {
                byte[] dst = new byte[readableBytes];
                in.getBytes(in.readerIndex(), dst);
                if (isBeginWith(dst, GET_BYTES)) {
                    int endIndex = findEndIndex(dst, CRLF_BYTES);
                    if (endIndex == -1) {
                        return null;
                    }
                    in.skipBytes(endIndex);
                }
            } else {
                byte[] dst = new byte[readableBytes];
                in.getBytes(in.readerIndex(), dst);
                if (isEqual(dst, GET_BYTES)) {
                    return null;
                }
            }
        }
    }

    if (discardingTooLongFrame) {
        long bytesToDiscard = this.bytesToDiscard;
        int localBytesToDiscard = (int) Math.min(bytesToDiscard, in.readableBytes());
        in.skipBytes(localBytesToDiscard);
        bytesToDiscard -= localBytesToDiscard;
        this.bytesToDiscard = bytesToDiscard;

        failIfNecessary(false);
    }

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

    int actualLengthFieldOffset = in.readerIndex() + lengthFieldOffset;
    long frameLength = getUnadjustedFrameLength(in, actualLengthFieldOffset, lengthFieldLength, byteOrder);

    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) {
        long discard = frameLength - in.readableBytes();
        tooLongFrameLength = frameLength;

        if (discard < 0) {
            // buffer contains more bytes then the frameLength so we can discard all now
            in.skipBytes((int) frameLength);
        } else {
            // Enter the discard mode and discard everything received so far.
            discardingTooLongFrame = true;
            bytesToDiscard = discard;
            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.kazzla.asterisk.netty.MessageDecoder.java

License:Apache License

public void decode(ChannelHandlerContext ctx, ByteBuf b, List<Object> out) throws CodecException {
    ByteBuffer buffer = b.nioBuffer();
    Option<Message> msg = codec.decode(buffer);
    if (msg instanceof Some) {
        b.skipBytes(buffer.position());
        out.add(msg);/* www. ja  va  2s  .  com*/
    }
}

From source file:com.linecorp.armeria.client.endpoint.dns.DnsServiceEndpointGroup.java

License:Apache License

@Override
ImmutableSortedSet<Endpoint> onDnsRecords(List<DnsRecord> records, int ttl) throws Exception {
    final ImmutableSortedSet.Builder<Endpoint> builder = ImmutableSortedSet.naturalOrder();
    for (DnsRecord r : records) {
        if (!(r instanceof DnsRawRecord) || r.type() != DnsRecordType.SRV) {
            continue;
        }/*from ww  w.j a v a 2s.c o m*/

        final ByteBuf content = ((ByteBufHolder) r).content();
        if (content.readableBytes() <= 6) { // Too few bytes
            warnInvalidRecord(DnsRecordType.SRV, content);
            continue;
        }

        content.markReaderIndex();
        content.skipBytes(2); // priority unused
        final int weight = content.readUnsignedShort();
        final int port = content.readUnsignedShort();

        final Endpoint endpoint;
        try {
            final String target = stripTrailingDot(DefaultDnsRecordDecoder.decodeName(content));
            endpoint = port > 0 ? Endpoint.of(target, port) : Endpoint.of(target);
        } catch (Exception e) {
            content.resetReaderIndex();
            warnInvalidRecord(DnsRecordType.SRV, content);
            continue;
        }

        builder.add(endpoint.withWeight(weight));
    }

    final ImmutableSortedSet<Endpoint> endpoints = builder.build();
    if (logger().isDebugEnabled()) {
        logger().debug("{} Resolved: {} (TTL: {})", logPrefix(),
                endpoints.stream().map(e -> e.authority() + '/' + e.weight()).collect(Collectors.joining(", ")),
                ttl);
    }

    return endpoints;
}

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

License:Apache License

private boolean hasFrame(ByteBuf in) {
    if (in.isReadable() == false) {
        return false;
    }//from  w w  w  .ja  va  2  s  . co m

    lookasideBufferLength = Math.min(in.readableBytes(), lookasideBuffer.length);
    in.readBytes(lookasideBuffer, 0, lookasideBufferLength);
    lookasideBufferReaderIndex = 0;
    in.readerIndex(in.readerIndex() - lookasideBufferLength);

    byte b = lookasideBuffer[lookasideBufferReaderIndex++];
    in.skipBytes(1);
    if (b == MINUS_BYTE) {
        return processError(in);
    } else if (b == ASTERISK_BYTE) {
        return processMultiBulkReply(in);
    } else if (b == COLON_BYTE) {
        return processInteger(in);
    } else if (b == DOLLAR_BYTE) {
        return processBulkReply(in);
    } else if (b == PLUS_BYTE) {
        return processStatusCodeReply(in);
    } else {
        throw new JedisConnectionException("Unknown reply: " + (char) b);
    }
}