package net.northking.cctp.upperComputer.deviceManager.entity;

/**
 * 收发DevicePort数据
 */
public class DevicePortData {
    /**
     * 单个数据最大长度
     * <br>
     * 默认400KiB，适配传图
     * <br>
     * 需结合WDA传输数据的特征决定
     */
    public static final int MAX_LENGTH = 4096 * 100;
    private final int sessionId;
    private byte[] data;
    private int offset = 0;
    private int length = -1;

    public DevicePortData(int sessionId, byte[] data) {
        this.sessionId = sessionId;
        this.data = data;
        if (data.length > MAX_LENGTH) throw new IllegalArgumentException("每次发送长度不要大于" + MAX_LENGTH);
    }

    public int getSessionId() {
        return sessionId;
    }

    public byte[] getData() {
        return data;
    }

    public void setData(byte[] data) {
        this.data = data;
        this.offset = 0;
        this.length = data.length;
    }

    public int getOffset() {
        return offset;
    }

    public void setOffset(int offset) {
        if (offset > MAX_LENGTH) throw new IllegalArgumentException("offset不能大于" + MAX_LENGTH + "，offset:" + offset);

        this.offset = offset;
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        if (length > MAX_LENGTH) throw new IllegalArgumentException("length不能大于" + MAX_LENGTH + "，length:" + length);

        this.length = length;
    }

    public void setData(byte[] data, int offset, int length) {
        if (length > MAX_LENGTH || offset > MAX_LENGTH) throw new IllegalArgumentException("offset和length不能大于" + MAX_LENGTH + "，offset:" + offset + " length:" + length);
        this.data = data;
        this.offset = offset;
        this.length = length;
    }
}
