// Path: BiliLiveLib/src/main/java/charlie/bililivelib/danmaku/DanmakuReceiver.java
// public static final Charset UTF8 = Charset.forName("UTF-8");


import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import static charlie.bililivelib.danmaku.DanmakuReceiver.UTF8;
package charlie.bililivelib.danmaku;




/**
 * 用于封装发送到弹幕服务器的数据包。<br>
 * 数据包的结构如下:<br>
 * {@code +-----+-------+-----+--------+-------+----------+}<br>
 * {@code | LEN | H Len | VER | OPCODE | PARAM | --BODY-- |}<br>
 * {@code | -4- |  -2-  | -2- | --4--- | --4-- | Variable |}<br>
 * {@code +-----+-------+-----+--------+-------+----------+}<br>
 * - LEN:    数据包长度，包含自身长度，int类型;<br>
 * - H Len:  包头部长度，默认包类型长度为16 Bytes, short类型;<br>
 * - VER:    协议版本，short类型，目前仅支持0x01;<br>
 * - OPCODE: 操作码，int类型。具体见{@link DanmakuPacket.Action};<br>
 * - PARAM:  操作参数，int类型，默认0x01;<br>
 * - BODY:   数据包体，长度可变。
 *
 * @author Charlie Jiang
 * @since rv1
 */
@Data
@AllArgsConstructor
public final class DanmakuPacket {
    private static final short DEFAULT_HEADER_LENGTH = 0x10;
    private static final short DEFAULT_PROTOCOL_VERSION = 1;
    private static final int DEFAULT_PARAM = 1;
    private static final int DEFAULT_LENGTH = -1;

    private static final int PACKET_HEADER_SIZE = 16;

    private short headerLength = DEFAULT_HEADER_LENGTH;
    private short protocolVersion = DEFAULT_PROTOCOL_VERSION;
    private int length;
    private Action action;
    private int param = DEFAULT_PARAM;
    private String body = "";

    public DanmakuPacket(Action action) {
        this(action, "");
    }

    public DanmakuPacket(Action action, String body) {
        this(DEFAULT_HEADER_LENGTH,
                DEFAULT_PROTOCOL_VERSION,
                DEFAULT_LENGTH,
                action,
                DEFAULT_PARAM,
                body);
    }

    /**
     * 由本类生成byte[]数组
     * @return byte[]数组
     */
    public byte[] generate() {
// Path: BiliLiveLib/src/main/java/charlie/bililivelib/danmaku/DanmakuReceiver.java
// public static final Charset UTF8 = Charset.forName("UTF-8");


// Path: BiliLiveLib/src/main/java/charlie/bililivelib/danmaku/DanmakuPacket.java
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import static charlie.bililivelib.danmaku.DanmakuReceiver.UTF8;

package charlie.bililivelib.danmaku;




/**
 * 用于封装发送到弹幕服务器的数据包。<br>
 * 数据包的结构如下:<br>
 * {@code +-----+-------+-----+--------+-------+----------+}<br>
 * {@code | LEN | H Len | VER | OPCODE | PARAM | --BODY-- |}<br>
 * {@code | -4- |  -2-  | -2- | --4--- | --4-- | Variable |}<br>
 * {@code +-----+-------+-----+--------+-------+----------+}<br>
 * - LEN:    数据包长度，包含自身长度，int类型;<br>
 * - H Len:  包头部长度，默认包类型长度为16 Bytes, short类型;<br>
 * - VER:    协议版本，short类型，目前仅支持0x01;<br>
 * - OPCODE: 操作码，int类型。具体见{@link DanmakuPacket.Action};<br>
 * - PARAM:  操作参数，int类型，默认0x01;<br>
 * - BODY:   数据包体，长度可变。
 *
 * @author Charlie Jiang
 * @since rv1
 */
@Data
@AllArgsConstructor
public final class DanmakuPacket {
    private static final short DEFAULT_HEADER_LENGTH = 0x10;
    private static final short DEFAULT_PROTOCOL_VERSION = 1;
    private static final int DEFAULT_PARAM = 1;
    private static final int DEFAULT_LENGTH = -1;

    private static final int PACKET_HEADER_SIZE = 16;

    private short headerLength = DEFAULT_HEADER_LENGTH;
    private short protocolVersion = DEFAULT_PROTOCOL_VERSION;
    private int length;
    private Action action;
    private int param = DEFAULT_PARAM;
    private String body = "";

    public DanmakuPacket(Action action) {
        this(action, "");
    }

    public DanmakuPacket(Action action, String body) {
        this(DEFAULT_HEADER_LENGTH,
                DEFAULT_PROTOCOL_VERSION,
                DEFAULT_LENGTH,
                action,
                DEFAULT_PARAM,
                body);
    }

    /**
     * 由本类生成byte[]数组
     * @return byte[]数组
     */
    public byte[] generate() {
        byte[] bodyBytes = body.getBytes(UTF8);