package com.saki.encode;

import java.nio.ByteBuffer;

public class UnPack {
    public static boolean BytesEqulsBytes(byte[] in, byte[] in2) {
        if (in.length == in2.length) {
            for (int i = 0; i < in.length; i++) {
                if (in[i] != in2[i])
                    return false;
            }
            return true;
        }
        return false;
    }

    byte[] data;
    ByteBuffer bb;

    public UnPack(byte[] data) {
        this.data = data;
    }

    public UnPack(byte[] data, int i) {
        bb = ByteBuffer.wrap(data);
    }

    public int findBytes(byte... b) {
        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < b.length; j++) {
                if (i + j < data.length && data[i + j] == b[j]) {
                    if (j == b.length - 1)
                        return i;
                } else {
                    break;
                }
            }
        }
        return -1;
    }

    public int findBytes2(byte... b) {
        int pos = bb.position();
        while (bb.position() != bb.limit()) {
            for (int i = 0; i < b.length; i++) {
                if (bb.get() != b[i])
                    break;
                if (i == b.length - 1) {
                    int pos2 = bb.position();
                    bb.position(pos);
                    return pos2 - b.length;
                }
            }
        }
        bb.position(pos);
        return -1;
    }

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

    public byte[] getAll2() {
        int pos = bb.position();
        byte[] dst = new byte[length2()];
        bb.get(dst);
        bb.position(pos);
        return dst;
    }

    public byte getByte() {
        return getBytes(1)[0];
    }

    public byte getByte2() {
        return bb.get();
    }

    public byte[] getBytes(int len) {
        if (len > data.length)
            return new byte[]{};
        byte[] arr = subByteArray(data, 0, len);
        data = subByteArray(data, len, data.length - len);
        return arr;
    }

    public byte[] getBytes2(int len) {
        if (len > 0 && len < length2()) {
            byte[] dst = new byte[len];
            bb.get(dst);
            return dst;
        }
        return new byte[0];
    }

    public int getInt() {
        if (data.length < 4)
            return 0;
        return ByteBuffer.wrap(getBytes(4)).getInt();
    }

    public int getInt2() {
        if (length2() < 4)
            return 0;
        return bb.getInt();
    }

    public short getShort() {
        short value = (short) (((data[0] & 0xFF) << 8) | (data[1] & 0xFF));
        data = subByteArray(data, 2, data.length - 2);
        return value;
    }

    public short getShort2() {
        return bb.getShort();
    }

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

    public int length2() {
        return bb.limit() - bb.position();
    }

    public void setBin(byte[] data) {
        this.data = data;
    }

    public void setBin2(byte[] data) {
        bb = ByteBuffer.wrap(data);
    }

    private byte[] subByteArray(byte[] arr, int pos, int len) {
        if (len <= 0)
            return new byte[0];
        byte[] marr = new byte[len];
        System.arraycopy(arr, pos, marr, 0, len);
        return marr;
    }
}
