API: int java.util.zip.Deflater.deflateBytes(byte[] b, int off, int len)
Usage Scenarios:
1. decrease communication load
2. decrease cache load for receiver when data amount too large
3. comrepssion level depend on data source(text or images..)
Code:
/**
* threshold value for compress
*/
public static final int THRESHOLD = 1200;
/**
* Answer a byte array compressed in the DEFLATER format from bytes.
*
* @param bytes
* a byte array
* @return byte[] compressed bytes
* @throws IOException
*/
public static byte[] compress(byte[] bytes)
{
// Create the compressor with highest level of compression
Deflater compressor = new Deflater();
compressor.setLevel(Deflater.BEST_COMPRESSION);
// Give the compressor the data to compress
compressor.setInput(bytes);
compressor.finish();
// Create an expandable byte array to hold the compressed data.
// You cannot use an array that's the same size as the orginal because
// there is no guarantee that the compressed data will be smaller than
// the uncompressed data.
ByteArrayOutputStream bos = new ByteArrayOutputStream(bytes.length);
// Compress the data
byte[] buf = new byte[1024];
while (!compressor.finished())
{
int count = compressor.deflate(buf);
bos.write(buf, 0, count);
}
try
{
bos.close();
}
catch (IOException e)
{
}
// Get the compressed data
byte[] compressedData = bos.toByteArray();
return compressedData;
}
/**
* Answer a byte array that has been decompressed from the DEFLATER format.
*
* @param bytes
* a byte array
* @return byte[] compressed bytes
* @throws IOException
*/
public static byte[] uncompress(byte[] bytes)
{
// Create the decompressor and give it the data to compress
Inflater decompressor = new Inflater();
decompressor.setInput(bytes);
// Create an expandable byte array to hold the decompressed data
ByteArrayOutputStream bos = new ByteArrayOutputStream(bytes.length);
// Decompress the data
byte[] buf = new byte[1024];
while (!decompressor.finished())
{
try
{
int count = decompressor.inflate(buf);
bos.write(buf, 0, count);
}
catch (DataFormatException e)
{
}
}
try
{
bos.close();
}
catch (IOException e)
{
}
// Get the decompressed data
byte[] decompressedData = bos.toByteArray();
return decompressedData;
}
分享到:
相关推荐
jdk api 1.8_中文文档 jdk api 1.8_中文文档 jdk api 1.8_中文文档
JDK API 1.6 中文版........
jdk api 1.8_google.CHM亲测双击即可打开。之前在网上下载了很多,要么双击打开报错,要么打开后里面是空白,终于找到一款好用的chm jdk api文档,跟大家分享下。
JDK API 1.8 中文 JDK API 1.8 中文 JDK API 1.8 中文 JDK API 1.8 中文 JDK API 1.8 中文 JDK API 1.8 中文 JDK API 1.8 中文 JDK API 1.8 中文 JDK API 1.8 中文 JDK API 1.8 中文 JDK API 1.8 中文 JDK API 1.8 ...
jdk Linux下帮助文件 很好的 .bin
jdk api 1.8_google翻译中文版api
解压之后有两个api版本 1.最新jdk api1.8英文版 2.jdk api1.6中文版
JDK1.8 API 中文 谷歌翻译版 java帮助文档 JDK API java 帮助文档 谷歌翻译 JDK1.8 API 谷歌百度翻译版 java帮助文档 Java最新帮助文档 本帮助文档是使用谷歌翻译,非人工翻译。准确性不能保证,请与英文版配合使用
JDK API中文文档, JDK API中文文档, JDK API中文文档
Java1.8中文API(google翻译版) 文件名:jdk api 1.8_google.CHM 文件大小:40.8M
java jdk api中文开发文档
JDK_API_1.8(中文版)JDK_API_1_8_zh_CN.zip
Java jdk api 1.8_google.CHM JDK API 1.8谷歌翻译中文版在线参考手册,附加使用说明,无需安装,下载直接使用,方便快捷。
包中有JDK帮助文档1.6 和1.7的帮助文档API,中文版,很方便适用
是一款jdk1.8英文版的API帮助文档,可以帮助使用java,jdk的用户们通过api及时查找到对应的类,接口,框架等详细信息的帮助手册,提高英文的阅读能力,学习最真实的java设计思想
Jdk api 1.6中文文档 Jdk api 1.6中文文档 Jdk api 1.6中文文档
JDK_API中文版全套离线 JDK_API中文版全套离线 JDK_API中文版全套离线
jdk api 1.8中文文档 翻译后的jdk api 1.8中文文档 翻译后的jdk api 1.8中文文档 翻译后的jdk api 1.8中文文档 翻译后的
java api文档,手机可用,解压后用手机或者电脑的浏览器打开文件夹中的index.html。JDK版本是1.6.02