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:uk.ac.lancs.stopcock.netty.OpenFlowDecoder.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> objects)
        throws Exception {
    /* Sanity check. */
    if (byteBuf.readableBytes() < 8) {
        throw new IllegalStateException();
    }//from  w w w.ja v  a  2 s. co  m

    /* Read OpenFlow Header. */
    short version = byteBuf.readUnsignedByte();
    short typeId = byteBuf.readUnsignedByte();
    int length = byteBuf.readUnsignedShort();
    long transactionId = byteBuf.readUnsignedInt();

    /* Sanity check that we have enough data. */
    if (byteBuf.readableBytes() < (length - 8)) {
        throw new IllegalStateException();
    }

    /* Copy the data portion of the OpenFlow packet and store it. */
    byteBuf.resetReaderIndex();
    byte[] originalData = byteBuf.readBytes(length).array();

    /* Construct the OpenFlow header and container for both it and data. */
    Header header = new Header(version, typeId, length, transactionId);
    Type type = Type.getById(typeId);

    /* Get full openflow packet for processing with openflowj */
    byteBuf.resetReaderIndex();

    /* Call openflowj using our Netty 3.9.X -> Netty 4.0.0 proxy object. */
    OFMessage message = OFFactories.getGenericReader().readFrom(new NettyCompatibilityChannelBuffer(byteBuf));

    /* Container object for header, raw data and openflowj message. */
    Container container = new Container(header, originalData, type, message);

    /* Add to the Netty pipeline. */
    objects.add(container);
}

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();/* w  ww .  j  av a 2  s  . c o  m*/

    int val = 0;
    int bytes = 0;
    while (true) {
        if (!in.isReadable()) {
            in.resetReaderIndex();
            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   w  w w . jav  a  2 s .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));
}