package com.gyee.edge.protocol.iec60870.infoelement;

import java.io.DataInputStream;
import java.io.IOException;

/**
 * Represents a length of file or section (LOF) information element.
 */
public class IeLengthOfFileOrSection extends InformationElement {

    private final int value;

    public IeLengthOfFileOrSection(int value) {
        this.value = value;
    }

    static IeLengthOfFileOrSection decode(DataInputStream is) throws IOException {
        int value = 0;
        for (int i = 0; i < 3; ++i) {
            value |= is.readUnsignedByte() << (i * 8);
        }
        return new IeLengthOfFileOrSection(value);
    }

    @Override
    int encode(byte[] buffer, int i) {

        buffer[i++] = (byte) value;
        buffer[i++] = (byte) (value >> 8);
        buffer[i] = (byte) (value >> 16);

        return 3;

    }

    public int getValue() {
        return value;
    }

    @Override
    public String toString() {
        return "Length of file or section: " + value;
    }
}
