package com.qmth.boot.test.tools.zip;

import com.qmth.boot.tools.models.ByteArray;
import org.junit.Test;

public class StringCompactTest {

    @Test
    public void test() throws Exception {
        String text = "1234567890,WEB";

        byte[] ba = ByteArray.fromString(text).value();
        for (int i = 0; i < ba.length; i++) {
            ba[i] = reverseByte(ba[i]);
        }
        System.out.println(ByteArray.fromArray(ba).toBase64());
        System.out.println(ByteArray.fromArray(ba).toHexString());
        //        System.out.println(ByteArray.fromString(text).toBase64());
        //        byte[] ba = ByteArray.fromString(text).value();
        //        ArrayUtils.reverse(ba);
        //        String result = ByteArray.fromArray(ba).toBase64();
        //        System.out.println(result);

        //        long start = System.currentTimeMillis();
        //        for (int i = 0; i < 100000; i++) {
        //            byte[] ba = ByteArray.fromString(RandomStringUtils.random(16, true, true)).value();
        //            ArrayUtils.reverse(ba);
        //            String result = ByteArray.fromArray(ba).toBase64();
        //            //System.out.println(result);
        //        }
        //        System.out.println(System.currentTimeMillis() - start);

        //        Deflater deflater = new Deflater(Deflater.BEST_SPEED);
        //        deflater.setInput(ByteArray.fromString(text).value());
        //        deflater.finish();
        //        final byte[] bytes = new byte[256];
        //        ByteArrayOutputStream ous = new ByteArrayOutputStream(256);
        //        while (!deflater.finished()) {
        //            int length = deflater.deflate(bytes);
        //            ous.write(bytes, 0, length);
        //        }
        //        deflater.end();
        //        System.out.println(ByteArray.fromArray(ous.toByteArray()).toHexString());

        //        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        //        GZIPOutputStream ous = new GZIPOutputStream(bout);
        //        ByteArray.fromString(text).toOutputStream(ous);
        //        ous.close();
        //        String result = ByteArray.fromArray(bout.toByteArray()).toBase64();
        //        System.out.println(result);
        //        GZIPInputStream zins = new GZIPInputStream(new ByteArrayInputStream(ByteArray.fromBase64(result).value()));
        //        System.out.println(ByteArray.fromInputStream(zins).toString());

        //System.out.println(ByteArray.fromString(text).toBase64());

        //        String md5 = ByteArray.md5("key").toHexString();
        //        String key = md5.substring(0, 16);
        //        //String vector = md5.substring(16);
        //        System.out.println(AES.encrypt(ByteArray.fromString(text).value(), key).toHexString());

        //        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        //        ObjectOutputStream ous = new ObjectOutputStream(bout);
        //        ByteArray.fromString(text).toOutputStream(ous);
        //        ous.close();
        //        System.out.println(ByteArray.fromArray(bout.toByteArray()).toHexString());

    }

    public static byte reverseByte(byte i) {
        // HD, Figure 7-1
        i = (byte) ((i & 0x55) << 1 | (i >>> 1) & 0x55);
        i = (byte) ((i & 0x33) << 2 | (i >>> 2) & 0x33);
        i = (byte) ((i & 0x0f) << 4 | (i >>> 4) & 0x0f);
        return i;
    }
}
