NetSIDPkg.java

package builder.netsiddev.commands;

import static server.netsiddev.NetworkSIDProtocol.*;

public interface NetSIDPkg {
    byte[] toByteArray();

    /**
     * Fill in packet data length so that the server knows when an entire packet has been
     * read.
     *
     * @return byte array containing the command with length information
     */
    default byte[] toByteArrayWithLength() {
        byte[] arr = toByteArray();
        if (arr.length - HEADER_SIZE > MAX_PAYLOAD_LENGTH) {
            throw new IllegalArgumentException("Network SID payload exceeds 65535 bytes");
        }
        arr[2] = (byte) (arr.length - HEADER_SIZE >> 8 & 0xff);
        arr[3] = (byte) (arr.length - HEADER_SIZE & 0xff);
        return arr;
    }
}