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

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

/**
 * Represents a short floating point number (R32-IEEE STD 754) information element.
 */
public class IeShortFloat extends InformationElement {

    private final float value;

    public IeShortFloat(float value) {
        this.value = value;
    }

    IeShortFloat(DataInputStream is) throws IOException {
        value = Float.intBitsToFloat((is.readByte() & 0xff) | ((is.readByte() & 0xff) << 8)
                | ((is.readByte() & 0xff) << 16) | ((is.readByte() & 0xff) << 24));
    }

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

        int tempVal = Float.floatToIntBits(value);
        buffer[i++] = (byte) tempVal;
        buffer[i++] = (byte) (tempVal >> 8);
        buffer[i++] = (byte) (tempVal >> 16);
        buffer[i] = (byte) (tempVal >> 24);

        return 4;
    }

    public float getValue() {
        return value;
    }

    @Override
    public String toString() {
        return "Short float value: " + value;
    }
}
