概述
Snappy是Google开源的压缩/解压缩库。和其他压缩库相比,snappy的压缩率并不是最高的,兼容性也并非最好的。相反,它的诞生旨在以极高的压缩/解压缩速率提供合理的压缩率。Snappy官指出:在64位单核core-i7处理器下,snappy的压缩率能够达到250MB/S,而解压缩速度则能达到500MB/S。目前很多软件使用(或支持)snappy作为压缩库,如MongoDB,Cassandra,Hadoop,Lucene…本文简单介绍snappy Java Api的使用。
Snappy提供了很多高级API以方便对byte[],Sting,double[]等进行压缩和解压缩,示例代码如下:
import org.xerial.snappy.Snappy;
String input = "Hello snappy-java! Snappy-java is a JNI-based wrapper of Snappy, a fast compresser/decompresser.";
{
byte[] compressed = Snappy.compress(input.getBytes("UTF-8"));
byte[] uncompressed = Snappy.uncompress(compressed);
String result = new String(uncompressed, "UTF-8");
System.out.println(result);
}
{
byte[] compressed = Snappy.compress(input);
System.out.println(Snappy.uncompressString(compressed));
}
{
double [] arr = new double[]{123.456,234.567,345.678};
byte[] compressed = Snappy.compress(arr);
double [] unarr = Snappy.uncompressDoubleArray(compressed);
System.out.println(Arrays.toString(unarr));
}
同时Snappy仍然保留着低级API,用于最小化内存的拷贝,例如
/*
*inputAddr:待压缩数据的内存地址
*inputSize:待压缩数据的byte size
*destAddr:压缩结果的保存地址
*return:返回压缩后数据的大小
*/
public static long rawCompress(long inputAddr,long inputSize, long destAddr)throws java.io.IOException;
/*
*inputAddr:待解压缩数据的内存地址
*inputSize:待解压缩数据的byte size
*destAddr:解压缩结果的保存地址
*return:返回解压缩后数据的大小
*/
public static long rawUncompress(long inputAddr,long inputSize,long destAddr)throws java.io.IOException;
基于输入/输出流的Snappy解压缩
SnappyOutputStream和SnappyInputStream分别用于流数据的压缩/解压缩。此外,从snappy v1.1.0开始提供了Framing-format(帧格式)输入输出流的压缩/解压缩的方法:SnappyFramedOutputStream和SnappyFramedInputStream。需要注意的是以SnappyOutputStream压缩的数据不能以SnappyFramedInputStream方法解开,反之亦然。下面以SnappyOutputStream和SnappyInputStream为例介绍。
SnappyOutputStream继承自java.io OutputStream,重写了常用的的write,close,flush:
File file = new File("..."); //待压缩文件
File out = new File("./", file.getName() + ".snappy"); //压缩结果文件
byte[] buffer = new byte[1024 * 1024 * 8];
FileInputStream fi = null;
FileOutputStream fo = null;
SnappyOutputStream sout = null;
try
{
fi = new FileInputStream(file);
fo = new FileOutputStream(out);
sout = new SnappyOutputStream(fo);
while(true)
{
int count = fi.read(buffer, 0, buffer.length);
if(count == -1) { break; }
sout.write(buffer, 0, count);
}
sout.flush();
}
catch(Throwable ex)
{
ex.printStackTrace();
}
finally
{
if(sout != null) {try { sout.close();} catch (Exception e) {}}
if(fi != null) { try { fi.close(); } catch(Exception x) {} }
if(fo != null) { try { fo.close(); } catch(Exception x) {} }
}
SnappyInputStream 继承自java.io InputStream,重写了常用的read,close方法等
File file = new File("xxx"); //待解压文件
File out = new File("xxx"); //解压后文件
byte[] buffer = new byte[1024 * 1024 * 8];
FileInputStream fi = null;
FileOutputStream fo = null;
SnappyInputStream sin = null;
try
{
fo = new FileOutputStream(out);
fi = new FileInputStream(file.getPath());
sin = new SnappyInputStream(fi);
while(true)
{
int count = sin.read(buffer, 0, buffer.length);
if(count == -1) { break; }
fo.write(buffer, 0, count);
}
fo.flush();
}
catch(Throwable ex)
{
ex.printStackTrace();
}
finally
{
if(sin != null) { try { sin.close(); } catch(Exception x) {} }
if(fi != null) { try { fi.close(); } catch(Exception x) {} }
if(fo != null) { try { fo.close(); } catch(Exception x) {} }
}
最后
以上就是老实牛排为你收集整理的Snappy Java API简介的全部内容,希望文章能够帮你解决Snappy Java API简介所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复