Example usage for io.netty.buffer ByteBuf writerIndex

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

Introduction

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

Prototype

public abstract int writerIndex();

Source Link

Document

Returns the writerIndex of this buffer.

Usage

From source file:org.opendaylight.capwap.msgelements.WTPRebootStatistics.java

License:Open Source License

@Override
public int encode(ByteBuf buf) {

    int start = buf.writerIndex();
    buf.writeBytes(ByteManager.unsignedShortToArray(this.rebootCount));
    buf.writeBytes(ByteManager.unsignedShortToArray(this.acInitiated));
    buf.writeBytes(ByteManager.unsignedShortToArray(this.linkFailure));
    buf.writeBytes(ByteManager.unsignedShortToArray(this.softwareFailure));
    buf.writeBytes(ByteManager.unsignedShortToArray(this.hwFailure));
    buf.writeBytes(ByteManager.unsignedShortToArray(this.otherFailure));
    buf.writeBytes(ByteManager.unsignedShortToArray(this.unKnownFailure));
    buf.writeByte(ByteManager.shortToUnsingedByte(this.lastFailureType));
    return buf.writerIndex() - start;
}

From source file:org.opendaylight.capwap.msgelements.WtpStaticIPAddressInfo.java

License:Open Source License

@Override
public int encode(ByteBuf buf) {
    int start = buf.writerIndex();
    this.address.encode(buf);
    this.netmask.encode(buf);
    this.gateway.encode(buf);
    buf.writeByte(ByteManager.shortToUnsingedByte(this._static));
    return 0;/*from   ww w .  j  a v  a 2  s.  c om*/
}

From source file:org.opendaylight.groupbasedpolicy.jsonrpc.JsonRpcDecoder.java

License:Open Source License

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

    logger.trace("readable bytes {}, records read {}, incomplete record bytes {}", buf.readableBytes(),
            recordsRead, lastRecordBytes);

    if (lastRecordBytes == 0) {
        if (buf.readableBytes() < 4) {
            return; //wait for more data
        }/*from   w w  w  .  java 2s  .com*/

        skipSpaces(buf);

        byte[] buff = new byte[4];
        buf.getBytes(buf.readerIndex(), buff);
        ByteSourceJsonBootstrapper strapper = new ByteSourceJsonBootstrapper(jacksonIOContext, buff, 0, 4);
        JsonEncoding jsonEncoding = strapper.detectEncoding();
        if (!JsonEncoding.UTF8.equals(jsonEncoding)) {
            throw new InvalidEncodingException(jsonEncoding.getJavaName(), "currently only UTF-8 is supported");
        }
    }

    int i = lastRecordBytes + buf.readerIndex();

    for (; i < buf.writerIndex(); i++) {
        switch (buf.getByte(i)) {
        case '{':
            if (!inS)
                leftCurlies++;
            break;
        case '}':
            if (!inS)
                rightCurlies++;
            break;
        case '"': {
            if (buf.getByte(i - 1) != '\\')
                inS = !inS;
            break;
        }
        default:
            break;
        }

        if (leftCurlies != 0 && leftCurlies == rightCurlies && !inS) {
            ByteBuf slice = buf.readSlice(1 + i - buf.readerIndex());
            JsonParser jp = jacksonJsonFactory.createParser(new ByteBufInputStream(slice));
            JsonNode root = jp.readValueAsTree();
            out.add(root);
            leftCurlies = rightCurlies = lastRecordBytes = 0;
            recordsRead++;
            break;
        }

        if (i - buf.readerIndex() >= maxFrameLength) {
            fail(ctx, i - buf.readerIndex());
        }
    }

    // end of stream, save the incomplete record index to avoid reexamining the whole on next run
    if (i >= buf.writerIndex()) {
        lastRecordBytes = buf.readableBytes();
        return;
    }
}

From source file:org.opendaylight.netide.openflowjava.protocol.impl.serialization.factories.MultipartReplyMessageFactory.java

License:Open Source License

private void writeOxmRelatedTableProperty(final ByteBuf output, final TableFeatureProperties property,
        final byte code) {
    int startIndex = output.writerIndex();
    output.writeShort(code);//w w  w . j  a v  a2 s.c o m
    int lengthIndex = output.writerIndex();
    output.writeShort(EncodeConstants.EMPTY_LENGTH);
    List<MatchEntry> entries = property.getAugmentation(OxmRelatedTableFeatureProperty.class).getMatchEntry();
    if (entries != null) {
        TypeKeyMaker<MatchEntry> keyMaker = TypeKeyMakerFactory
                .createMatchEntriesKeyMaker(EncodeConstants.OF13_VERSION_ID);
        ListSerializer.serializeHeaderList(entries, keyMaker, registry, output);
    }
    int length = output.writerIndex() - startIndex;
    output.setShort(lengthIndex, length);
    output.writeZero(paddingNeeded(length));
}

From source file:org.opendaylight.netide.openflowjava.protocol.impl.serialization.factories.MultipartReplyMessageFactory.java

License:Open Source License

private void writeActionsRelatedTableProperty(final ByteBuf output, final TableFeatureProperties property,
        final byte code) {
    int startIndex = output.writerIndex();
    output.writeShort(code);//  w w w .  ja  v a  2  s  .  com
    int lengthIndex = output.writerIndex();
    output.writeShort(EncodeConstants.EMPTY_LENGTH);
    List<Action> actions = property.getAugmentation(ActionRelatedTableFeatureProperty.class).getAction();
    if (actions != null) {
        TypeKeyMaker<Action> keyMaker = TypeKeyMakerFactory
                .createActionKeyMaker(EncodeConstants.OF13_VERSION_ID);
        ListSerializer.serializeHeaderList(actions, keyMaker, registry, output);
    }
    int length = output.writerIndex() - startIndex;
    output.setShort(lengthIndex, length);
    output.writeZero(paddingNeeded(length));
}

From source file:org.opendaylight.netide.openflowjava.protocol.impl.serialization.factories.MultipartReplyMessageFactory.java

License:Open Source License

private static void writeNextTableRelatedTableProperty(final ByteBuf output,
        final TableFeatureProperties property, final byte code) {
    int startIndex = output.writerIndex();
    output.writeShort(code);//from w  w  w  .  j a  v  a  2 s .  co  m
    int lengthIndex = output.writerIndex();
    output.writeShort(EncodeConstants.EMPTY_LENGTH);
    List<NextTableIds> nextTableIds = property.getAugmentation(NextTableRelatedTableFeatureProperty.class)
            .getNextTableIds();
    if (nextTableIds != null) {
        for (NextTableIds next : nextTableIds) {
            output.writeByte(next.getTableId());
        }
    }
    int length = output.writerIndex() - startIndex;
    output.setShort(lengthIndex, length);
    output.writeZero(paddingNeeded(length));
}

From source file:org.opendaylight.netide.openflowjava.protocol.impl.serialization.factories.MultipartReplyMessageFactory.java

License:Open Source License

private void writeInstructionRelatedTableProperty(final ByteBuf output, final TableFeatureProperties property,
        final byte code) {
    int startIndex = output.writerIndex();
    output.writeShort(code);//from  www.  j  ava2s . c  om
    int lengthIndex = output.writerIndex();
    output.writeShort(EncodeConstants.EMPTY_LENGTH);
    List<Instruction> instructions = property.getAugmentation(InstructionRelatedTableFeatureProperty.class)
            .getInstruction();
    if (instructions != null) {
        TypeKeyMaker<Instruction> keyMaker = TypeKeyMakerFactory
                .createInstructionKeyMaker(EncodeConstants.OF13_VERSION_ID);
        ListSerializer.serializeHeaderList(instructions, keyMaker, registry, output);
    }
    int length = output.writerIndex() - startIndex;
    output.setShort(lengthIndex, length);
    output.writeZero(paddingNeeded(length));
}

From source file:org.opendaylight.netide.openflowjava.protocol.impl.serialization.factories.NetIdePacketOutInputMessageFactory.java

License:Open Source License

@Override
public void serialize(PacketOutInput message, ByteBuf outBuffer) {
    ByteBufUtils.writeOFHeader(MESSAGE_TYPE, message, outBuffer, EncodeConstants.EMPTY_LENGTH);
    outBuffer.writeInt(message.getBufferId().intValue());
    outBuffer.writeInt(message.getInPort().getValue().intValue());
    int actionsLengthIndex = outBuffer.writerIndex();
    outBuffer.writeShort(message.getAction().size());
    outBuffer.writeZero(PADDING_IN_PACKET_OUT_MESSAGE);
    int actionsStartIndex = outBuffer.writerIndex();
    ListSerializer.serializeList(message.getAction(),
            TypeKeyMakerFactory.createActionKeyMaker(message.getVersion()), registry, outBuffer);
    outBuffer.setShort(actionsLengthIndex, outBuffer.writerIndex() - actionsStartIndex);
    byte[] data = message.getData();
    if (data != null) {
        outBuffer.writeBytes(data);/*from  www .  ja v a  2  s  . co  m*/
    }
    ByteBufUtils.updateOFHeaderLength(outBuffer);
}

From source file:org.opendaylight.openflowjava.protocol.impl.serialization.action.OF13SetFieldActionSerializer.java

License:Open Source License

@Override
public void serialize(Action action, ByteBuf outBuffer) {
    int startIndex = outBuffer.writerIndex();
    outBuffer.writeShort(ActionConstants.SET_FIELD_CODE);
    int lengthIndex = outBuffer.writerIndex();
    outBuffer.writeShort(EncodeConstants.EMPTY_LENGTH);
    MatchEntry entry = ((SetFieldCase) action.getActionChoice()).getSetFieldAction().getMatchEntry().get(0);
    MatchEntrySerializerKey<?, ?> key = new MatchEntrySerializerKey<>(EncodeConstants.OF13_VERSION_ID,
            entry.getOxmClass(), entry.getOxmMatchField());
    if (entry.getOxmClass().equals(ExperimenterClass.class)) {
        ExperimenterIdCase experimenterIdCase = (ExperimenterIdCase) entry.getMatchEntryValue();
        key.setExperimenterId(experimenterIdCase.getExperimenter().getExperimenter().getValue());
    } else {// w ww  . ja v  a2s  . co m
        key.setExperimenterId(null);
    }
    OFSerializer<MatchEntry> serializer = registry.getSerializer(key);
    serializer.serialize(entry, outBuffer);
    int paddingRemainder = (outBuffer.writerIndex() - startIndex) % EncodeConstants.PADDING;
    if (paddingRemainder != 0) {
        outBuffer.writeZero(EncodeConstants.PADDING - paddingRemainder);
    }
    outBuffer.setShort(lengthIndex, outBuffer.writerIndex() - startIndex);
}

From source file:org.opendaylight.openflowjava.protocol.impl.serialization.experimenter.AbstractBundleMessageFactory.java

License:Open Source License

protected void writeBundleProperties(final List<BundleProperty> properties, final ByteBuf outBuffer) {
    for (BundleProperty property : properties) {
        BundlePropertyType type = property.getType();
        if (type != null && type.equals(BundlePropertyType.ONFETBPTEXPERIMENTER)) {
            int startIndex = outBuffer.writerIndex();
            outBuffer.writeShort(type.getIntValue());
            int lengthIndex = outBuffer.writerIndex();
            outBuffer.writeShort(EncodeConstants.EMPTY_LENGTH);
            writeBundleExperimenterProperty(property, outBuffer);
            outBuffer.setShort(lengthIndex, outBuffer.writerIndex() - startIndex);
        } else {/*w  ww.  j  a v  a  2 s. co  m*/
            LOG.warn("lTrying to serialize unknown bundle property (type: {}), skipping", type.getIntValue());
        }
    }
}