Example usage for io.netty.buffer ByteBuf resetReaderIndex

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

Introduction

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

Prototype

public abstract ByteBuf resetReaderIndex();

Source Link

Document

Repositions the current readerIndex to the marked readerIndex in this buffer.

Usage

From source file:com.dempe.chat.common.mqtt.codec.PublishDecoder.java

License:Open Source License

@Override
void decode(AttributeMap ctx, ByteBuf in, List<Object> out) throws Exception {
    LOG.debug("decode invoked with buffer {}", in);
    in.resetReaderIndex();
    int startPos = in.readerIndex();

    //Common decoding part
    PublishMessage message = new PublishMessage();
    if (!decodeCommonHeader(message, in)) {
        LOG.debug("decode ask for more data after {}", in);
        in.resetReaderIndex();/*from   w  w w  . j  av a  2s.c om*/
        return;
    }

    if (Utils.isMQTT3_1_1(ctx)) {
        if (message.getQos() == AbstractMessage.QOSType.MOST_ONE && message.isDupFlag()) {
            //bad protocol, if QoS=0 => DUP = 0
            throw new CorruptedFrameException("Received a PUBLISH with QoS=0 & DUP = 1, MQTT 3.1.1 violation");
        }

        if (message.getQos() == AbstractMessage.QOSType.RESERVED) {
            throw new CorruptedFrameException(
                    "Received a PUBLISH with QoS flags setted 10 b11, MQTT 3.1.1 violation");
        }
    }

    int remainingLength = message.getRemainingLength();

    //Topic name
    String topic = Utils.decodeString(in);
    if (topic == null) {
        in.resetReaderIndex();
        return;
    }
    //[MQTT-3.3.2-2] The Topic Name in the PUBLISH Packet MUST NOT contain wildcard characters.
    if (topic.contains("+") || topic.contains("#")) {
        throw new CorruptedFrameException(
                "Received a PUBLISH with topic containing wild card chars, topic: " + topic);
    }
    //check topic is at least one char [MQTT-4.7.3-1]
    if (topic.length() == 0) {
        throw new CorruptedFrameException("Received a PUBLISH with topic without any character");
    }

    message.setTopicName(topic);

    if (message.getQos() == AbstractMessage.QOSType.LEAST_ONE
            || message.getQos() == AbstractMessage.QOSType.EXACTLY_ONCE) {
        message.setMessageID(in.readUnsignedShort());
    }
    int stopPos = in.readerIndex();

    //read the payload
    int payloadSize = remainingLength - (stopPos - startPos - 2)
            + (Utils.numBytesToEncode(remainingLength) - 1);
    if (in.readableBytes() < payloadSize) {
        in.resetReaderIndex();
        return;
    }
    ByteBuf bb = Unpooled.buffer(payloadSize);
    in.readBytes(bb);
    message.setPayload(bb.nioBuffer());

    out.add(message);
}

From source file:com.dempe.chat.common.mqtt.codec.PubRelDecoder.java

License:Open Source License

@Override
void decode(AttributeMap ctx, ByteBuf in, List<Object> out) throws UnsupportedEncodingException {
    in.resetReaderIndex();
    //Common decoding part
    MessageIDMessage message = new PubRelMessage();
    if (!decodeCommonHeader(message, 0x02, in)) {
        in.resetReaderIndex();//from   w ww . j  a v  a  2  s  .com
        return;
    }

    //read  messageIDs
    message.setMessageID(in.readUnsignedShort());
    out.add(message);
}

From source file:com.dempe.chat.common.mqtt.codec.SubAckDecoder.java

License:Open Source License

@Override
void decode(AttributeMap ctx, ByteBuf in, List<Object> out) throws Exception {
    //Common decoding part
    in.resetReaderIndex();
    SubAckMessage message = new SubAckMessage();
    if (!decodeCommonHeader(message, 0x00, in)) {
        in.resetReaderIndex();//from   w  w  w.  ja v a  2 s  . co  m
        return;
    }
    int remainingLength = message.getRemainingLength();

    //MessageID
    message.setMessageID(in.readUnsignedShort());
    remainingLength -= 2;

    //Qos array
    if (in.readableBytes() < remainingLength) {
        in.resetReaderIndex();
        return;
    }
    for (int i = 0; i < remainingLength; i++) {
        byte qos = in.readByte();
        message.addType(AbstractMessage.QOSType.valueOf(qos));
    }

    out.add(message);
}

From source file:com.dempe.chat.common.mqtt.codec.SubscribeDecoder.java

License:Open Source License

@Override
void decode(AttributeMap ctx, ByteBuf in, List<Object> out) throws Exception {
    //Common decoding part
    SubscribeMessage message = new SubscribeMessage();
    in.resetReaderIndex();
    if (!decodeCommonHeader(message, 0x02, in)) {
        in.resetReaderIndex();/* w w  w.  ja v a2 s .  co m*/
        return;
    }

    //check qos level
    if (message.getQos() != AbstractMessage.QOSType.LEAST_ONE) {
        throw new CorruptedFrameException(
                "Received SUBSCRIBE message with QoS other than LEAST_ONE, was: " + message.getQos());
    }

    int start = in.readerIndex();
    //read  messageIDs
    message.setMessageID(in.readUnsignedShort());
    int read = in.readerIndex() - start;
    while (read < message.getRemainingLength()) {
        decodeSubscription(in, message);
        read = in.readerIndex() - start;
    }

    if (message.subscriptions().isEmpty()) {
        throw new CorruptedFrameException("subscribe MUST have got at least 1 couple topic/QoS");
    }

    out.add(message);
}

From source file:com.dempe.chat.common.mqtt.codec.UnsubscribeDecoder.java

License:Open Source License

@Override
void decode(AttributeMap ctx, ByteBuf in, List<Object> out) throws Exception {
    //Common decoding part
    in.resetReaderIndex();
    UnsubscribeMessage message = new UnsubscribeMessage();
    if (!decodeCommonHeader(message, 0x02, in)) {
        in.resetReaderIndex();/*ww w .  j  ava2 s .  c  o m*/
        return;
    }

    //check qos level
    if (message.getQos() != AbstractMessage.QOSType.LEAST_ONE) {
        throw new CorruptedFrameException(
                "Found an Unsubscribe message with qos other than LEAST_ONE, was: " + message.getQos());
    }

    int start = in.readerIndex();
    //read  messageIDs
    message.setMessageID(in.readUnsignedShort());
    int read = in.readerIndex() - start;
    while (read < message.getRemainingLength()) {
        String topicFilter = Utils.decodeString(in);
        //check topic is at least one char [MQTT-4.7.3-1]
        if (topicFilter.length() == 0) {
            throw new CorruptedFrameException("Received an UNSUBSCRIBE with empty topic filter");
        }
        message.addTopicFilter(topicFilter);
        read = in.readerIndex() - start;
    }
    if (message.topicFilters().isEmpty()) {
        throw new CorruptedFrameException("unsubscribe MUST have got at least 1 topic");
    }
    out.add(message);
}

From source file:com.dianping.cat.message.CodecHandler.java

License:Open Source License

public static MessageTree decode(ByteBuf buf) {
    byte[] data = new byte[3];
    MessageTree tree;/*  w ww  .  j  a  v  a 2 s  . co m*/

    buf.getBytes(4, data);
    String hint = new String(data);

    buf.resetReaderIndex();

    if ("PT1".equals(hint)) {
        tree = m_plainTextCodec.decode(buf);
    } else if ("NT1".equals(hint)) {
        tree = m_nativeCodec.decode(buf);
    } else {
        throw new RuntimeException("Error message type : " + hint);
    }

    MessageTreeFormat.format(tree);
    return tree;
}

From source file:com.dingwang.rpc.decode.RpcDecoder.java

License:Open Source License

@Override
public final void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    if (in.readableBytes() < 4) {
        return;/*from  w  w w  . ja  va 2 s .c  o m*/
    }
    in.markReaderIndex();
    int dataLength = in.readInt();
    if (dataLength < 0) {
        ctx.close();
    }
    if (in.readableBytes() < dataLength) {
        in.resetReaderIndex();
    }
    byte[] data = new byte[dataLength];
    in.readBytes(data);

    Object obj = SerializationUtil.deserialize(data, genericClass);
    out.add(obj);
}

From source file:com.dinstone.jrpc.transport.netty4.TransportProtocolDecoder.java

License:Apache License

private byte[] readFrame(ByteBuf in) {
    if (in.readableBytes() > 4) {
        in.markReaderIndex();/*from   ww w .  j  a  v  a 2s . c  o m*/
        int len = in.readInt();
        if (len > maxObjectSize) {
            throw new IllegalStateException(
                    "The encoded object is too big: " + len + " (> " + maxObjectSize + ")");
        } else if (len < 1) {
            throw new IllegalStateException("The encoded object is too small: " + len + " (< 1)");
        }

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

        byte[] rpcBytes = new byte[len];
        in.readBytes(rpcBytes);
        return rpcBytes;
    }

    return null;
}

From source file:com.doctor.netty5.example.factorial_algorithm.BigIntegerDecoder.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    // ? ?F +  +  -> 1 + 4 + ??
    // ??//*from w w  w .  j  a v  a  2 s .c om*/
    if (in.readableBytes() < 5) {
        return; // F + 
    }

    in.markReaderIndex();
    short magicNumber = in.readUnsignedByte();// ?F
    if (magicNumber != 'F') {
        in.resetReaderIndex();
        throw new RuntimeException("magicNumber must be 'F' ");
    }

    // ???
    int dataLength = in.readInt();

    if (in.readableBytes() < dataLength) {
        in.resetReaderIndex();
        return;
    }

    byte[] data = new byte[dataLength];
    in.readBytes(data);
    out.add(new BigInteger(data));
}

From source file:com.dwarf.netty.guide.factorial.BigIntegerDecoder.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
    // Wait until the length prefix is available.
    if (in.readableBytes() < 5) {
        return;/*w w  w. java  2s  .  c o  m*/
    }

    in.markReaderIndex();

    // Check the magic number.
    int magicNumber = in.readUnsignedByte();
    if (magicNumber != 'F') {
        in.resetReaderIndex();
        throw new CorruptedFrameException("Invalid magic number: " + magicNumber);
    }

    // Wait until the whole data is available.
    int dataLength = in.readInt();
    if (in.readableBytes() < dataLength) {
        in.resetReaderIndex();
        return;
    }

    // Convert the received data into a new BigInteger.
    byte[] decoded = new byte[dataLength];
    in.readBytes(decoded);

    out.add(new BigInteger(decoded));
}