zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    Eclipse常用快捷键
    Kali Linux安装Google中文输入法(只需5步)
    Kali Linux 更新源 操作完整版教程
    Oracle存储过程的异常处理
    Eclipse调试DEBUG时快速查看某个变量的值的快捷键、快速跳转到某行的快捷键
    oracle listagg和wm_concat函数
    ORACLE分页查询SQL语法——最高效的分页
    Mock拦截请求URL返回模板数据
    前端安全之XSS攻击及防御
    Sublime Text3注册码,亲测可用
  • 原文地址:https://www.cnblogs.com/yeyang/p/11844742.html
Copyright © 2011-2022 走看看