package com.saki.qq.wtlogin.pack;

import com.saki.encode.Canvart;

public class Pack {
    private byte[] data = new byte[0];

    public void addInHead(byte... b) {
        byte[] bin = new byte[b.length + data.length];
        System.arraycopy(b, 0, bin, 0, b.length);
        System.arraycopy(data, 0, bin, b.length, data.length);
        data = bin;

    }

    public void appendByte(int i) {
        concat((byte) i);
    }

    public void appendBytes(byte... b) {
        concat(b);
    }

    public void appendInt(int i) {
        concat(Canvart.Int2Bytes(i));
    }

    public void appendLong(long l) {
        concat(Canvart.Long2Bytes(l, 4));
    }

    public void appendShort(int s) {
        concat(Canvart.Short2Bytes((short) s));
    }

    public void appendString(String str) {
        concat(str.getBytes());
    }

    public void clear() {
        data = new byte[0];
    }

    private void concat(byte... bin) {
        byte[] mbin = new byte[data.length + bin.length];
        System.arraycopy(data, 0, mbin, 0, data.length);
        System.arraycopy(bin, 0, mbin, data.length, bin.length);
        data = mbin;
    }

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

    public boolean isEmpty() {
        return data.length == 0;
    }

    public int length() {
        return data.length;
    }

    public void setByte(byte... b) {
        data = b;
    }
}
