java应用org.xerial.snappy.Snappy实现数据压缩、解压缩,并对比三种压缩方式

一、导包

坐标:

 
 	org.xerial.snappy
 	snappy-java
 	1.1.2.4
 

也可以去仓库中心下载

二、代码

压缩、解压缩实现

import info.compress.InputGenerator;
import org.xerial.snappy.Snappy;
import org.xerial.snappy.SnappyFramedOutputStream;
import org.xerial.snappy.SnappyInputStream;
import org.xerial.snappy.SnappyOutputStream;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class SnappyImp {
    private final int BLOCK = 1024 * 1024 * 8;
    
    /**
     * 使用Snappy.compress进行压缩
     * @param data
     * @return byte[]
     * @create 2020/3/11 16:38
     */
    public byte[] snappyCompress(byte[] data) throws Exception {
        return Snappy.compress(data);
    }

    /**
     * 使用Snappy.uncompress进行解压缩
     * @param data
     * @return byte[]
     * @create 2020/3/11 16:38
     */
    public byte[] snappyDecompress(byte[] data) throws Exception {
        return Snappy.uncompress(data);
    }


    /**
     * 使用SnappyOutputStream进行压缩
     * @param data 
     * @return byte[] 
     * @create 2020/3/11 21:27
     */
    public byte[] snappyStreamCompress(byte[] data) throws IOException{

        ByteArrayInputStream is = new ByteArrayInputStream(data);
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        SnappyOutputStream sos = new SnappyOutputStream(os);
        int count;
        byte temp[] = new byte[BLOCK];
        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();
        }
    }

    /**
     * S使用nappyFramedOutputStream进行压缩
     * @param data 
     * @return byte[] 
     * @create 2020/3/11 21:27
     */
    public byte[] snappyFramedStreamCompress(byte[] data) throws IOException{
        ByteArrayInputStream is = new ByteArrayInputStream(data);
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        SnappyFramedOutputStream sfos = new SnappyFramedOutputStream(os);
        int count;
        byte temp[] = new byte[BLOCK];
        try {
            while ((count = is.read(temp)) != -1) {
                sfos.write(temp, 0, count);
            }
            sfos.flush();
            byte[] output = os.toByteArray();
            return output;
        } finally {
            sfos.close();
            is.close();
        }
    }

    /**
     * SnappyInputStream解压缩
     * @param data 
     * @return byte[] 
     * @create 2020/3/11 21:28
     */
    public byte[] snappyStreamDecompress(byte[] data) throws IOException{
        byte[] buffer = new byte[BLOCK];
        ByteArrayInputStream is = new ByteArrayInputStream(data);
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        SnappyInputStream sis = new SnappyInputStream(is);
       try {
           while(true){
               int count =sis.read(buffer, 0, buffer.length);
               if(count == -1) { break; }
               os.write(buffer,0,count);
           }
           os.flush();
       }finally {
           if(sis != null) { try { sis.close(); } catch(Exception x) {} }
           if(os != null) { try { os.close(); } catch(Exception x) {} }
           if(is != null) { try { is.close(); } catch(Exception x) {} }
       }
        return os.toByteArray();
    }
}

使用JMH进行基准测试,来对比一下三种压缩方式

import info.compress.InputGenerator;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.concurrent.TimeUnit;


@OutputTimeUnit(TimeUnit.MILLISECONDS)
@State(Scope.Thread)
@Warmup(iterations = 3)
@Measurement(iterations = 3)
@BenchmarkMode(Mode.Throughput)
public class newTest {
    @Benchmark
    public void snappyCompress() throws Exception{
        Path inputFile = InputGenerator.FILE_PATH.toPath();
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        try {
            Files.copy(inputFile, os);
        } catch (IOException e) {
            e.printStackTrace();
        }
        SnappyImp snappyImp = new SnappyImp();
        byte[] bytes = snappyImp.snappyCompress(os.toByteArray());
    }

    @Benchmark
    public void snappyStreamCompress() throws IOException {
        Path inputFile = InputGenerator.FILE_PATH.toPath();
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        try {
            Files.copy(inputFile, os);
        } catch (IOException e) {
            e.printStackTrace();
        }
        SnappyImp snappyImp = new SnappyImp();
        byte[] bytes = snappyImp.snappyStreamCompress(os.toByteArray());
    }

    @Benchmark
    public void snappyFramedStreamCompress() throws IOException {
        Path inputFile = InputGenerator.FILE_PATH.toPath();
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        try {
            Files.copy(inputFile, os);
        } catch (IOException e) {
            e.printStackTrace();
        }
        SnappyImp snappyImp = new SnappyImp();
        byte[] bytes = snappyImp.snappyFramedStreamCompress(os.toByteArray());
    }


    public static void main(String[] args) throws Exception {
        Options options = new OptionsBuilder().include(newTest.class.getSimpleName()).forks(3).build();
        new Runner(options).run();
    }
}

从网上找的数据合成的方法

import java.io.*;
import java.nio.file.Files;

public class InputGenerator {
    private static final String JAVADOC_PATH = "D:/Java/docs";
    public static final File FILE_PATH = new File( "./compress_src/docs.txt" );

    static
    {
        try {
            if ( !FILE_PATH.exists())
                makeJavadocFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void makeJavadocFile() throws IOException {
        try( OutputStream os = new BufferedOutputStream( new FileOutputStream( FILE_PATH ), 65536 ) )
        {
            appendDir(os, new File( JAVADOC_PATH ));
        }
        System.out.println( "Javadoc file created");
    }

    private static void appendDir( final OutputStream os, final File root ) throws IOException {
        for ( File f : root.listFiles() )
        {
            if ( f.isDirectory() ){
                appendDir( os, f );
            }
            else{
                Files.copy(f.toPath(), os);
            }
        }
    }
}

结果:

java应用org.xerial.snappy.Snappy实现数据压缩、解压缩,并对比三种压缩方式_第1张图片
直接看Score,可以看出snappyCompress的得分最好,也就是说性能最好了

你可能感兴趣的:(javaDemo)