All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.digitalpetri.enip.cpf.UnconnectedDataItemRequest Maven / Gradle / Ivy

package com.digitalpetri.enip.cpf;

import java.util.function.Consumer;

import io.netty.buffer.ByteBuf;

public class UnconnectedDataItemRequest extends CpfItem {

    public static final int TYPE_ID = 0xB2;

    private final Consumer encoder;

    public UnconnectedDataItemRequest(Consumer encoder) {
        super(TYPE_ID);

        this.encoder = encoder;
    }

    public Consumer getEncoder() {
        return encoder;
    }

    public static ByteBuf encode(UnconnectedDataItemRequest item, ByteBuf buffer) {
        buffer.writeShort(item.getTypeId());

        // Length placeholder...
        int lengthStartIndex = buffer.writerIndex();
        buffer.writeShort(0);

        // Encode the encapsulated data...
        int dataStartIndex = buffer.writerIndex();
        item.getEncoder().accept(buffer);

        // Go back and update the length.
        int bytesWritten = buffer.writerIndex() - dataStartIndex;
        buffer.markWriterIndex();
        buffer.writerIndex(lengthStartIndex);
        buffer.writeShort(bytesWritten);
        buffer.resetWriterIndex();

        return buffer;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy