Example usage for io.netty.buffer ByteBuf markReaderIndex

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

Introduction

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

Prototype

public abstract ByteBuf markReaderIndex();

Source Link

Document

Marks the current readerIndex in this buffer.

Usage

From source file:pub.vrtech.protocol.RedisCommandCodec.java

License:Apache License

@Override
protected void doDecode(Channel channel, ByteBuf buffer, List<Object> out) throws IOException {
    if (bytes != null) {
        int numArgs = bytes.length;
        for (int i = arguments; i < numArgs; i++) {//?
            buffer.markReaderIndex();//
            if (buffer.readByte() == RespType.BULK_STRING.getFlag()) {
                final long l = readLength(buffer);//?
                if (l < 0) {
                    buffer.resetReaderIndex();
                    return;
                }/*ww  w  . j  a  v a2 s  .  com*/
                int size = (int) l;
                if (buffer.readableBytes() < (size + 2)) {
                    buffer.resetReaderIndex();
                    return;
                }
                bytes[i] = new byte[size];
                buffer.readBytes(bytes[i]);//?
                buffer.skipBytes(2);///r/n
                arguments++;
            } else {
                throw new IOException("Unexpected character");
            }
        }
        try {
            out.add(new RedisCommand(bytes));
        } finally {
            //?bytes
            bytes = null;
            arguments = 0;
        }
    } else if (buffer.readByte() == RespType.ARRAY.getFlag()) {// ?
        buffer.markReaderIndex();// 
        long length = readLength(buffer);
        if (length > Integer.MAX_VALUE) {
            throw new IllegalArgumentException(
                    "Java only supports arrays up to " + Integer.MAX_VALUE + " in size");
        }
        if (length < 0) {// ??
            buffer.resetReaderIndex();
            return;
        }
        final int numArgs = (int) length;
        bytes = new byte[numArgs][];//SET KEY  VALUE numArgs3
        doDecode(channel, buffer, out);
    }
}

From source file:qunar.tc.qmq.delay.receiver.ReceivedDelayMessageProcessor.java

License:Apache License

private RawMessageExtend doDeserializeRawMessagesExtend(ByteBuf body) {
    body.markReaderIndex();
    int headerStart = body.readerIndex();
    long bodyCrc = body.readLong();
    MessageHeader header = deserializeMessageHeader(body);
    header.setBodyCrc(bodyCrc);//from   w ww .j a  v  a2s.c  om
    int bodyLen = body.readInt();
    int headerLen = body.readerIndex() - headerStart;
    int totalLen = headerLen + bodyLen;

    body.resetReaderIndex();
    ByteBuf messageBuf = body.readSlice(totalLen);
    // client config error,prefer to send after ten second
    long scheduleTime = System.currentTimeMillis() + 10000;
    if (Flags.isDelay(header.getFlag())) {
        scheduleTime = header.getExpireTime();
    }

    return new RawMessageExtend(header, messageBuf, messageBuf.readableBytes(), scheduleTime);
}

From source file:qunar.tc.qmq.netty.DecodeHandler.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf in, List<Object> list)
        throws Exception {
    if (in.readableBytes() < RemotingHeader.MIN_HEADER_SIZE + RemotingHeader.LENGTH_FIELD)
        return;// w ww .  java2  s.  com

    int magicCode = in.getInt(in.readerIndex() + RemotingHeader.LENGTH_FIELD);
    if (DEFAULT_MAGIC_CODE != magicCode) {
        throw new IOException("Illegal Data, MagicCode=" + Integer.toHexString(magicCode));
    }

    in.markReaderIndex();
    int total = in.readInt();
    if (in.readableBytes() < total) {
        in.resetReaderIndex();
        return;
    }

    short headerSize = in.readShort();
    RemotingHeader remotingHeader = decodeHeader(in);

    int bodyLength = total - headerSize - RemotingHeader.HEADER_SIZE_LEN;

    RemotingCommand remotingCommand = new RemotingCommand();
    //because netty(lower version) has memory leak when ByteBuf cross thread
    //We can ensure server use high version netty, bu we can't ensure client
    if (isServer) {
        ByteBuf bodyData = in.readSlice(bodyLength);
        bodyData.retain();
        remotingCommand.setBody(bodyData);
    } else {
        ByteBuf bodyData = Unpooled.buffer(bodyLength, bodyLength);
        in.readBytes(bodyData, bodyLength);
        remotingCommand.setBody(bodyData);
    }
    remotingCommand.setHeader(remotingHeader);
    list.add(remotingCommand);
}

From source file:qunar.tc.qmq.processor.SendMessageProcessor.java

License:Apache License

private RawMessage deserializeRawMessageWithCrc(ByteBuf body) {
    long bodyCrc = body.readLong();

    int headerStart = body.readerIndex();
    body.markReaderIndex();
    MessageHeader header = deserializeMessageHeader(body);
    header.setBodyCrc(bodyCrc);/*from  w w w  .java  2 s. co  m*/
    int bodyLen = body.readInt();
    int headerLen = body.readerIndex() - headerStart;

    int totalLen = headerLen + bodyLen;
    body.resetReaderIndex();
    ByteBuf messageBuf = body.readSlice(totalLen);
    return new RawMessage(header, messageBuf, totalLen);
}

From source file:qunar.tc.qmq.protocol.QMQSerializer.java

License:Apache License

public static RawMessage deserializeRawMessage(ByteBuf body) {
    int headerStart = body.readerIndex();
    body.markReaderIndex();
    MessageHeader header = deserializeMessageHeader(body);
    int bodyLen = body.readInt();
    int headerLen = body.readerIndex() - headerStart;

    int totalLen = headerLen + bodyLen;
    body.resetReaderIndex();// w ww  . j  a va  2 s.  com
    byte[] data = new byte[totalLen];
    body.readBytes(data);
    header.setBodyCrc(Crc32.crc32(data));
    return new RawMessage(header, Unpooled.wrappedBuffer(data), totalLen);
}

From source file:uk.co.thinkofdeath.prismarine.network.VarIntFrameCodec.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    // Return back to this point if the varint isn't complete
    in.markReaderIndex();

    int val = 0;
    int bytes = 0;
    while (true) {
        if (!in.isReadable()) {
            in.resetReaderIndex();/*from ww w.j  a v a2s.c  o m*/
            return;
        }

        int b = in.readByte();
        val |= (b & 0b01111111) << (bytes++ * 7);
        if (bytes >= 3) { // Smaller limit for packets
            throw new DecoderException("VarInt too big");
        }

        // If the 8th bit is set then the varint continues
        if ((b & 0x80) == 0) {
            break;
        }
    }

    if (!in.isReadable(val)) {
        // Packet isn't complete yet
        in.resetReaderIndex();
        return;
    }
    out.add(in.readBytes(val));
}

From source file:waazdoh.cp2p.messaging.MessageDecoder.java

License:Open Source License

@Override
protected void decode(ChannelHandlerContext arg0, ByteBuf cb, List<Object> msgs) throws Exception {
    if (cb.readableBytes() < 8)
        return;//from ww  w . ja  v a 2s .co m
    cb.markReaderIndex();
    int length = cb.readInt();

    if (cb.readableBytes() < length) {
        log.info("missing readablebytes " + cb.readableBytes() + " vs " + length);
        cb.resetReaderIndex();
        return;
    }

    byte[] bs = new byte[length];
    cb.readBytes(bs);

    DataInputStream dis = new DataInputStream(new ByteArrayInputStream(bs));
    msgs.add(parse(dis));
}