snappy-java两种压缩方式的区别

1.Snappy-java项目地址

https://github.com/xerial/snappy-java

2.Snappy-java两种压缩方式

使用Snappy.compress进行压缩

String dataString = "The quick brown fox jumps over the lazy dog";
byte[] compressed = Snappy.compress(dataString.getBytes("UTF-8"));
byte[] uncompressed = Snappy.uncompress(compressed);
String result = new String(uncompressed, "UTF-8");
System.out.println(result);

使用SnappyInputStream进行压缩

    public static byte[] compressSnappy(byte[] data) throws IOException {
        ByteArrayInputStream is = new ByteArrayInputStream(data);
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        SnappyOutputStream sos = new SnappyOutputStream(os);
        int count;
        byte temp[] = new byte[BUFFER];
        try {
            while ((count = is.read(temp)) != -1) {
                sos.write(temp, 0, count);
            }
            sos.flush();
            byte[] output = os.toByteArray();
            return output;
        } finally {
            sos.close();
            is.close();
        }
    } 

3.两种压缩方式的区别

    /**
     * 输出如下:
     * Snappy.compress  压缩结果:2b a8 54 68 65 20 71 75 69 63 6b 20 62 72 6f 77 6e 20 66 6f 78 20 6a 75 6d 70 73 20 6f 76 65 72 20 74 68 65 20 6c 61 7a 79 20 64 6f 67
     * SnappyInputStream压缩结果:82 53 4e 41 50 50 59 00 00 00 00 01 00 00 00 01 00 00 00 2d 2b a8 54 68 65 20 71 75 69 63 6b 20 62 72 6f 77 6e 20 66 6f 78 20 6a 75 6d 70 73 20 6f 76 65 72 20 74 68 65 20 6c 61 7a 79 20 64 6f 67
     *                         |---------------------magic header(16bytes)-----|size(4bytes)|----compressed data-----
     */
    @Test
    public void testSnappyCompress() throws Exception {
        String dataString = "The quick brown fox jumps over the lazy dog";
​
        byte[] compressedData = Snappy.compress(dataString.getBytes());
        System.out.println("Snappy.compress  压缩结果:" + bytes2hex(compressedData));
​
        byte[] compressedData2 = compressSnappy(dataString.getBytes());
        System.out.println("SnappyInputStream压缩结果:" + bytes2hex(compressedData2));
    }
    /**
     * 将byte数组按16进制的方式输出
     */
    public static String bytes2hex(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        String tmp = null;
        for (byte b : bytes) {
            // 将每个字节与0xFF进行与运算,然后转化为10进制,然后借助于Integer再转化为16进制
            tmp = Integer.toHexString(0xFF & b);
            if (tmp.length() == 1) {
                tmp = "0" + tmp;
            }
            sb.append(tmp).append(" ");
        }
        return sb.toString();
    }

区别如下:

通过Snappy.compress()进行压缩,压缩后的数据没有magic header

通过SnappyInputStream进行压缩,压缩后的数据有固定的header

posted @ 2019-11-12 19:55  Ye_yang  阅读(4872)  评论(0编辑  收藏  举报