package me.proxylink.common;

import java.util.Arrays;
import java.util.Objects;

public final class Frame {
    private static final byte[] EMPTY = new byte[0];

    private final FrameType type;
    private final long streamId;
    private final byte[] payload;

    public Frame(FrameType type, long streamId, byte[] payload) {
        this.type = Objects.requireNonNull(type, "type");
        this.streamId = streamId;
        this.payload = payload == null || payload.length == 0 ? EMPTY : Arrays.copyOf(payload, payload.length);
    }

    public static Frame empty(FrameType type, long streamId) {
        return new Frame(type, streamId, EMPTY);
    }

    public FrameType type() {
        return type;
    }

    public long streamId() {
        return streamId;
    }

    public byte[] payload() {
        return payload.length == 0 ? EMPTY : Arrays.copyOf(payload, payload.length);
    }

    public int payloadLength() {
        return payload.length;
    }
}
