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:org.onosproject.lisp.msg.protocols.DefaultLispInfo.java

License:Apache License

public static LispInfo deserialize(ByteBuf byteBuf) throws LispParseError, LispReaderException {

    if (byteBuf.readerIndex() != 0) {
        return null;
    }/*w  w  w  .  j  a va2s.c  o m*/

    // infoReply -> 1 bit
    boolean infoReplyFlag = ByteOperator.getBit(byteBuf.readByte(), INFO_REPLY_INDEX);

    // let's skip the reserved field
    byteBuf.skipBytes(RESERVED_SKIP_LENGTH_1);

    // nonce -> 64 bits
    long nonce = byteBuf.readLong();

    // keyId -> 16 bits
    short keyId = byteBuf.readShort();

    // authenticationDataLength -> 16 bits
    short authLength = byteBuf.readShort();

    // authData -> depends on the authenticationDataLength
    byte[] authData = new byte[authLength];
    byteBuf.readBytes(authData);

    // ttl -> 32 bits
    int ttl = byteBuf.readInt();

    // let's skip the reserved field
    byteBuf.skipBytes(RESERVED_SKIP_LENGTH_2);

    // mask length -> 8 bits
    short maskLength = byteBuf.readUnsignedByte();

    LispAfiAddress prefix = new LispAfiAddress.AfiAddressReader().readFrom(byteBuf);

    return new DefaultLispInfo(infoReplyFlag, nonce, keyId, authLength, authData, ttl, (byte) maskLength,
            prefix);
}

From source file:org.onosproject.lisp.msg.types.lcaf.LispLcafAddress.java

License:Apache License

/**
 * Deserializes common fields from byte buffer.
 *
 * @param byteBuf byte buffer/*from  w ww  .  jav  a 2 s  .c om*/
 * @return LispLcafAddress with filled common data fields
 */
public static LispLcafAddress deserializeCommon(ByteBuf byteBuf) {

    // let's skip first and second two bytes,
    // because it represents LCAF AFI code
    byteBuf.skipBytes(LCAF_AFI_CODE_BYTE_LENGTH);

    // reserved1 -> 8 bits
    byte reserved1 = (byte) byteBuf.readUnsignedByte();

    // flags -> 8 bits
    byte flag = (byte) byteBuf.readUnsignedByte();

    // LCAF type -> 8 bits
    byte lcafType = (byte) byteBuf.readUnsignedByte();

    // reserved2 -> 8bits
    byte reserved2 = (byte) byteBuf.readUnsignedByte();

    // length -> 16 bits
    short length = (short) byteBuf.readUnsignedShort();

    return new LispLcafAddress(LispCanonicalAddressFormatEnum.valueOf(lcafType), reserved1, reserved2, flag,
            length);
}

From source file:org.onosproject.xmpp.core.ctl.handlers.XmlStreamDecoder.java

License:Apache License

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

    AsyncByteArrayFeeder streamFeeder = streamReader.getInputFeeder();
    logger.info("Decoding XMPP data.. ");

    byte[] buffer = new byte[in.readableBytes()];
    in.readBytes(buffer);/*  ww  w  .java 2 s.  co m*/
    logger.debug("Buffer length: " + buffer.length);
    try {
        streamFeeder.feedInput(buffer, 0, buffer.length);
    } catch (XMLStreamException exception) {
        logger.info(exception.getMessage());
        in.skipBytes(in.readableBytes());
        logger.info("Bytes skipped");
        throw exception;
    }

    while (streamReader.hasNext() && streamReader.next() != AsyncXMLStreamReader.EVENT_INCOMPLETE) {
        out.add(allocator.allocate(streamReader));
    }

}

From source file:org.opendaylight.capwap.binding_802_11.MsgElem802_11Factory.java

License:Open Source License

static public WTP_Radio_Information decodeWtpRadioInfoElm(ByteBuf buf, int length) {
    if (buf == null) {
        LOG.error("ByteBuf null WtpRadioInfoElm  ");
        return null;
    }/*from  ww  w  .  ja v a  2s .  c  om*/
    if (!buf.isReadable()) {
        LOG.error("ByteBuf not readable WtpRadioInfoElm");
        return null;
    }

    WTP_Radio_Information radioInfo = new WTP_Radio_Information();
    radioInfo.setRadioId(buf.readByte());
    buf.skipBytes(3);
    radioInfo.setRadioType(buf.readByte());
    return radioInfo;
}

From source file:org.opendaylight.ext.impl.FooActionDeserializer.java

License:Open Source License

@Override
public Action deserialize(ByteBuf input) {

    input.skipBytes(EncodeConstants.SIZE_OF_SHORT_IN_BYTES); // we know the type of action
    input.skipBytes(EncodeConstants.SIZE_OF_SHORT_IN_BYTES); // we don't need length

    ActionBuilder actionBuilder = new ActionBuilder();
    actionBuilder.setExperimenterId(new ExperimenterId(input.readUnsignedInt()));
    ActionFooBuilder actionFooBuilder = new ActionFooBuilder();
    ExtActionFooBuilder extActionFooBuilder = new ExtActionFooBuilder();
    extActionFooBuilder.setFirst(input.readUnsignedShort());
    extActionFooBuilder.setSecond(input.readUnsignedShort());
    input.skipBytes(4); // padding
    actionFooBuilder.setExtActionFoo(extActionFooBuilder.build());
    actionBuilder.setActionChoice(actionFooBuilder.build());
    return actionBuilder.build();
}

From source file:org.opendaylight.netide.openflowjava.protocol.impl.deserialization.factories.FlowModInputMessageFactory.java

License:Open Source License

@Override
public FlowModInput deserialize(ByteBuf rawMessage) {
    FlowModInputBuilder builder = new FlowModInputBuilder();
    builder.setVersion((short) EncodeConstants.OF13_VERSION_ID);
    builder.setXid(rawMessage.readUnsignedInt());
    byte[] cookie = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
    rawMessage.readBytes(cookie);//ww  w.j a v  a 2s  . c om
    builder.setCookie(new BigInteger(1, cookie));
    byte[] cookie_mask = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
    rawMessage.readBytes(cookie_mask);
    builder.setCookieMask(new BigInteger(1, cookie_mask));
    builder.setTableId(new TableId((long) rawMessage.readUnsignedByte()));
    builder.setCommand(FlowModCommand.forValue(rawMessage.readUnsignedByte()));
    builder.setIdleTimeout(rawMessage.readUnsignedShort());
    builder.setHardTimeout(rawMessage.readUnsignedShort());
    builder.setPriority(rawMessage.readUnsignedShort());
    builder.setBufferId(rawMessage.readUnsignedInt());
    builder.setOutPort(new PortNumber(rawMessage.readUnsignedInt()));
    builder.setOutGroup(rawMessage.readUnsignedInt());
    builder.setFlags(createFlowModFlagsFromBitmap(rawMessage.readUnsignedShort()));
    rawMessage.skipBytes(PADDING);
    OFDeserializer<Match> matchDeserializer = registry.getDeserializer(
            new MessageCodeKey(EncodeConstants.OF13_VERSION_ID, EncodeConstants.EMPTY_VALUE, Match.class));
    builder.setMatch(matchDeserializer.deserialize(rawMessage));
    CodeKeyMaker keyMaker = CodeKeyMakerFactory.createInstructionsKeyMaker(EncodeConstants.OF13_VERSION_ID);
    List<Instruction> instructions = ListDeserializer.deserializeList(EncodeConstants.OF13_VERSION_ID,
            rawMessage.readableBytes(), rawMessage, keyMaker, registry);
    builder.setInstruction(instructions);
    return builder.build();
}

From source file:org.opendaylight.netide.openflowjava.protocol.impl.deserialization.factories.FlowModInputMessageFactoryTest.java

License:Open Source License

@Test
public void test() throws Exception {
    FlowModInput expectedMessage = createMessage();
    SerializerRegistry registry = new SerializerRegistryImpl();
    registry.init();/*w ww  . j av  a2  s. c om*/
    OFSerializer<FlowModInput> serializer = registry
            .getSerializer(new MessageTypeKey<>(EncodeConstants.OF13_VERSION_ID, FlowModInput.class));
    ByteBuf originalBuffer = UnpooledByteBufAllocator.DEFAULT.buffer();
    serializer.serialize(expectedMessage, originalBuffer);

    // TODO: Skipping first 4 bytes due to the way deserializer is
    // implemented
    // Skipping version, type and length from OF header
    originalBuffer.skipBytes(4);
    FlowModInput deserializedMessage = BufferHelper.deserialize(flowFactory, originalBuffer);
    Assert.assertEquals("Wrong version", expectedMessage.getVersion(), deserializedMessage.getVersion());
    Assert.assertEquals("Wrong XId", expectedMessage.getXid(), deserializedMessage.getXid());
    Assert.assertEquals("Wrong cookie", expectedMessage.getCookie(), deserializedMessage.getCookie());
    Assert.assertEquals("Wrong cookie mask", expectedMessage.getCookieMask(),
            deserializedMessage.getCookieMask());
    Assert.assertEquals("Wrong table id", expectedMessage.getTableId().getValue(),
            deserializedMessage.getTableId().getValue());
    Assert.assertEquals("Wrong command", expectedMessage.getCommand().getIntValue(),
            deserializedMessage.getCommand().getIntValue());
    Assert.assertEquals("Wrong idle timeout", expectedMessage.getIdleTimeout(),
            deserializedMessage.getIdleTimeout());
    Assert.assertEquals("Wrong hard timeout", expectedMessage.getHardTimeout(),
            deserializedMessage.getHardTimeout());
    Assert.assertEquals("Wrong priority", expectedMessage.getPriority(), deserializedMessage.getPriority());
    Assert.assertEquals("Wrong buffer id ", expectedMessage.getBufferId(), deserializedMessage.getBufferId());
    Assert.assertEquals("Wrong out port", expectedMessage.getOutPort().getValue(),
            deserializedMessage.getOutPort().getValue());
    Assert.assertEquals("Wrong out group", expectedMessage.getOutGroup(), deserializedMessage.getOutGroup());
    Assert.assertEquals("Wrong number of flags", expectedMessage.getFlags().getValue().length,
            deserializedMessage.getFlags().getValue().length);
    for (int i = 0; i < expectedMessage.getFlags().getValue().length; i++) {
        Assert.assertEquals("Wrong flag", expectedMessage.getFlags().getValue()[i],
                deserializedMessage.getFlags().getValue()[i]);
    }
    Assert.assertEquals("Wrong match", expectedMessage.getMatch(), deserializedMessage.getMatch());
    Assert.assertEquals("Wrong number of instructions", expectedMessage.getInstruction().size(),
            deserializedMessage.getInstruction().size());
    int i = 0;
    for (Instruction ins : expectedMessage.getInstruction()) {
        Assert.assertEquals("Wrong instruction", ins, deserializedMessage.getInstruction().get(i));
        i++;
    }
}

From source file:org.opendaylight.netide.openflowjava.protocol.impl.deserialization.factories.GetFeaturesInputFactoryTest.java

License:Open Source License

@Test
public void test() throws Exception {
    GetFeaturesInput expectedMessage = createMessage();
    SerializerRegistry registry = new SerializerRegistryImpl();
    registry.init();//from   w ww.j  av  a2s  .  c o m
    OFSerializer<GetFeaturesInput> serializer = registry
            .getSerializer(new MessageTypeKey<>(EncodeConstants.OF13_VERSION_ID, GetFeaturesInput.class));
    ByteBuf originalBuffer = UnpooledByteBufAllocator.DEFAULT.buffer();
    serializer.serialize(expectedMessage, originalBuffer);

    // TODO: Skipping first 4 bytes due to the way deserializer is
    // implemented
    // Skipping version, type and length from OF header
    originalBuffer.skipBytes(4);
    GetFeaturesInput deserializedMessage = BufferHelper.deserialize(factory, originalBuffer);
    Assert.assertEquals("Wrong version", expectedMessage.getVersion(), deserializedMessage.getVersion());
    Assert.assertEquals("Wrong XId", expectedMessage.getXid(), deserializedMessage.getXid());
}

From source file:org.opendaylight.netide.openflowjava.protocol.impl.deserialization.factories.GroupModInputMessageFactory.java

License:Open Source License

@Override
public GroupModInput deserialize(ByteBuf rawMessage) {
    GroupModInputBuilder builder = new GroupModInputBuilder();
    builder.setVersion((short) EncodeConstants.OF13_VERSION_ID);
    builder.setXid(rawMessage.readUnsignedInt());
    builder.setCommand(GroupModCommand.forValue(rawMessage.readUnsignedShort()));
    builder.setType(GroupType.forValue(rawMessage.readUnsignedByte()));
    rawMessage.skipBytes(PADDING);
    builder.setGroupId(new GroupId(rawMessage.readUnsignedInt()));
    List<BucketsList> bucketsList = new ArrayList<>();
    while (rawMessage.readableBytes() > 0) {
        BucketsListBuilder bucketsBuilder = new BucketsListBuilder();
        int bucketsLength = rawMessage.readUnsignedShort();
        bucketsBuilder.setWeight(rawMessage.readUnsignedShort());
        bucketsBuilder.setWatchPort(new PortNumber(rawMessage.readUnsignedInt()));
        bucketsBuilder.setWatchGroup(rawMessage.readUnsignedInt());
        rawMessage.skipBytes(PADDING_IN_BUCKETS_HEADER);
        CodeKeyMaker keyMaker = CodeKeyMakerFactory.createActionsKeyMaker(EncodeConstants.OF13_VERSION_ID);
        List<Action> actions = ListDeserializer.deserializeList(EncodeConstants.OF13_VERSION_ID,
                bucketsLength - BUCKETS_HEADER_LENGTH, rawMessage, keyMaker, registry);
        bucketsBuilder.setAction(actions);
        bucketsList.add(bucketsBuilder.build());
    }/*from  w w w .j av  a  2s . c om*/
    builder.setBucketsList(bucketsList);
    return builder.build();
}

From source file:org.opendaylight.netide.openflowjava.protocol.impl.deserialization.factories.MeterModInputMessageFactory.java

License:Open Source License

@Override
public MeterModInput deserialize(ByteBuf rawMessage) {
    MeterModInputBuilder builder = new MeterModInputBuilder();
    builder.setVersion((short) EncodeConstants.OF13_VERSION_ID);
    builder.setXid(rawMessage.readUnsignedInt());
    builder.setCommand(MeterModCommand.forValue(rawMessage.readUnsignedShort()));
    builder.setFlags(createMeterFlags(rawMessage.readUnsignedShort()));
    builder.setMeterId(new MeterId(rawMessage.readUnsignedInt()));
    List<Bands> bandsList = new ArrayList<>();
    while (rawMessage.readableBytes() > 0) {
        BandsBuilder bandsBuilder = new BandsBuilder();
        int bandStartIndex = rawMessage.readerIndex();
        int bandType = rawMessage.readUnsignedShort();
        switch (bandType) {
        case 1:/*from   w  w  w. j  a v a 2s.  c o m*/
            MeterBandDropCaseBuilder bandDropCaseBuilder = new MeterBandDropCaseBuilder();
            MeterBandDropBuilder bandDropBuilder = new MeterBandDropBuilder();
            bandDropBuilder.setType(MeterBandType.forValue(bandType));
            rawMessage.readUnsignedShort();
            bandDropBuilder.setRate(rawMessage.readUnsignedInt());
            bandDropBuilder.setBurstSize(rawMessage.readUnsignedInt());
            rawMessage.skipBytes(PADDING_IN_METER_BAND_DROP_HEADER);
            bandDropCaseBuilder.setMeterBandDrop(bandDropBuilder.build());
            bandsBuilder.setMeterBand(bandDropCaseBuilder.build());
            break;
        case 2:
            MeterBandDscpRemarkCaseBuilder bandDscpRemarkCaseBuilder = new MeterBandDscpRemarkCaseBuilder();
            MeterBandDscpRemarkBuilder bandDscpRemarkBuilder = new MeterBandDscpRemarkBuilder();
            bandDscpRemarkBuilder.setType(MeterBandType.forValue(bandType));
            rawMessage.readUnsignedShort();
            bandDscpRemarkBuilder.setRate(rawMessage.readUnsignedInt());
            bandDscpRemarkBuilder.setBurstSize(rawMessage.readUnsignedInt());
            bandDscpRemarkBuilder.setPrecLevel(rawMessage.readUnsignedByte());
            rawMessage.skipBytes(PADDING_IN_METER_BAND_DSCP_HEADER);
            bandDscpRemarkCaseBuilder.setMeterBandDscpRemark(bandDscpRemarkBuilder.build());
            bandsBuilder.setMeterBand(bandDscpRemarkCaseBuilder.build());
            break;
        case 0xFFFF:
            long expId = rawMessage
                    .getUnsignedInt(rawMessage.readerIndex() + 2 * EncodeConstants.SIZE_OF_INT_IN_BYTES);
            rawMessage.readerIndex(bandStartIndex);
            OFDeserializer<MeterBandExperimenterCase> deserializer = registry
                    .getDeserializer(ExperimenterDeserializerKeyFactory
                            .createMeterBandDeserializerKey(EncodeConstants.OF13_VERSION_ID, expId));
            bandsBuilder.setMeterBand(deserializer.deserialize(rawMessage));
            break;
        }
        bandsList.add(bandsBuilder.build());
    }
    builder.setBands(bandsList);
    return builder.build();
}