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

com.digitalpetri.enip.cip.epath.DataSegment Maven / Gradle / Ivy

There is a newer version: 1.5.0-RC1
Show newest version
package com.digitalpetri.enip.cip.epath;

import io.netty.buffer.ByteBuf;

import java.nio.charset.Charset;

public abstract class DataSegment extends EPathSegment {

    public static final int SEGMENT_TYPE = 0x80;

    protected abstract ByteBuf encode(ByteBuf buffer);

    public static ByteBuf encode(DataSegment segment, boolean padded, ByteBuf buffer) {
        return segment.encode(buffer);
    }

    public static final class AnsiDataSegment extends DataSegment {

        public static final int SUBTYPE = 0x11;

        private static final Charset ASCII = Charset.forName("US-ASCII");

        private final String data;

        public AnsiDataSegment(String data) {
            this.data = data;
        }

        @Override
        protected ByteBuf encode(ByteBuf buffer) {
            String data = this.data.length() <= 255 ?
                    this.data : this.data.substring(0, 255);

            buffer.writeByte(SEGMENT_TYPE | SUBTYPE);
            buffer.writeByte(data.length());
            buffer.writeBytes(data.getBytes(ASCII));
            if (data.length() % 2 != 0) buffer.writeByte(0);

            return buffer;
        }

    }

    public static final class SimpleDataSegment extends DataSegment {

        private final short[] data;

        public SimpleDataSegment(short[] data) {
            this.data = data;
        }

        @Override
        protected ByteBuf encode(ByteBuf buffer) {
            buffer.writeByte(SEGMENT_TYPE);
            buffer.writeByte(data.length);

            for (short d : data) {
                buffer.writeShort(d);
            }

            return buffer;
        }

    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy