public class Deflater extends Object
下面的代码片段展示了一个平凡的压缩和解压缩使用Deflater和Inflater字符串。
。try { // Encode a String into bytes String inputString = "blahblahblah"; byte[] input = inputString.getBytes("UTF-8"); // Compress the bytes byte[] output = new byte[100]; Deflater compresser = new Deflater(); compresser.setInput(input); compresser.finish(); int compressedDataLength = compresser.deflate(output); compresser.end(); // Decompress the bytes Inflater decompresser = new Inflater(); decompresser.setInput(output, 0, compressedDataLength); byte[] result = new byte[100]; int resultLength = decompresser.inflate(result); decompresser.end(); // Decode the bytes into a String String outputString = new String(result, 0, resultLength, "UTF-8"); } catch(java.io.UnsupportedEncodingException ex) { // handle } catch (java.util.zip.DataFormatException ex) { // handle }
Inflater
Modifier and Type | Field and Description |
---|---|
static int |
BEST_COMPRESSION
最佳压缩压缩级。
|
static int |
BEST_SPEED
压缩级别最快的压缩。
|
static int |
DEFAULT_COMPRESSION
默认压缩级别。
|
static int |
DEFAULT_STRATEGY
默认压缩策略。
|
static int |
DEFLATED
对于压缩算法的压缩方法(目前唯一一个支持)。
|
static int |
FILTERED
压缩策略最适用的数据,主要是由一个有点随机分布的小值。
|
static int |
FULL_FLUSH
加压冲洗模式用来冲洗出所有未决的输出和复位deflater。
|
static int |
HUFFMAN_ONLY
赫夫曼编码的压缩策略。
|
static int |
NO_COMPRESSION
无压缩的压缩级别。
|
static int |
NO_FLUSH
压缩刷新模式,以达到最佳的压缩效果。
|
static int |
SYNC_FLUSH
用于清除所有挂起的输出的压缩刷新模式,可能会降低一些压缩算法的压缩。
|
Constructor and Description |
---|
Deflater()
用默认的压缩级别创建一个新的压缩机。
|
Deflater(int level)
使用指定的压缩级别创建一个新的压缩机。
|
Deflater(int level, boolean nowrap)
使用指定的压缩级别创建一个新的压缩机。
|
Modifier and Type | Method and Description |
---|---|
int |
deflate(byte[] b)
压缩输入数据和压缩数据填充指定的缓冲区。
|
int |
deflate(byte[] b, int off, int len)
压缩输入数据和压缩数据填充指定的缓冲区。
|
int |
deflate(byte[] b, int off, int len, int flush)
压缩输入数据和压缩数据填充指定的缓冲区。
|
void |
end()
关闭压缩机并丢弃任何未处理的输入。
|
protected void |
finalize()
当收集到垃圾时关闭压缩机。
|
void |
finish()
当调用时,表示压缩应该以输入缓冲区的当前内容结束。
|
boolean |
finished()
如果压缩数据输出流的结束已达到,则返回真。
|
int |
getAdler()
返回未压缩的数据adler-32价值。
|
long |
getBytesRead()
返回未压缩的字节输入总数量为止。
|
long |
getBytesWritten()
返回到目前为止的压缩字节数的总数。
|
int |
getTotalIn()
返回未压缩的字节输入总数量为止。
|
int |
getTotalOut()
返回到目前为止的压缩字节数的总数。
|
boolean |
needsInput()
返回true如果输入数据缓冲区是空的,setinput()应该是为了提供更多的输入要求。
|
void |
reset()
重置deflater这样一套新的输入数据处理。
|
void |
setDictionary(byte[] b)
设置预置的压缩字典。
|
void |
setDictionary(byte[] b, int off, int len)
设置预置的压缩字典。
|
void |
setInput(byte[] b)
设置压缩的输入数据。
|
void |
setInput(byte[] b, int off, int len)
设置压缩的输入数据。
|
void |
setLevel(int level)
将压缩级别设置为指定的值。
|
void |
setStrategy(int strategy)
将压缩策略设置为指定的值。
|
public static final int DEFLATED
public static final int NO_COMPRESSION
public static final int BEST_SPEED
public static final int BEST_COMPRESSION
public static final int DEFAULT_COMPRESSION
public static final int FILTERED
public static final int HUFFMAN_ONLY
public static final int DEFAULT_STRATEGY
public static final int NO_FLUSH
deflate(byte[], int, int, int)
,
Constant Field Values
public static final int SYNC_FLUSH
deflate(byte[], int, int, int)
,
Constant Field Values
public static final int FULL_FLUSH
deflate(byte[], int, int, int)
,
Constant Field Values
public Deflater(int level, boolean nowrap)
level
-压缩级别(0-9)
nowrap
使用gzip压缩兼容
public Deflater(int level)
level
-压缩级别(0-9)
public Deflater()
public void setInput(byte[] b, int off, int len)
b
-输入数据字节
off
的起始偏移量的数据
len
-数据的长度
needsInput()
public void setInput(byte[] b)
b
-输入数据字节
needsInput()
public void setDictionary(byte[] b, int off, int len)
b
-字典数据字节
off
的起始偏移量的数据
len
-数据的长度
Inflater.inflate(byte[], int, int)
,
Inflater.getAdler()
public void setDictionary(byte[] b)
b
-字典数据字节
Inflater.inflate(byte[], int, int)
,
Inflater.getAdler()
public void setStrategy(int strategy)
如果压缩策略的改变,deflate
下次调用将压缩到目前为止与旧策略的输入(可刷新);新的策略只会在调用生效。
strategy
-新的压缩策略
IllegalArgumentException
如果压缩策略是无效的
public void setLevel(int level)
如果压缩水平的改变,deflate
下次调用将压缩到目前为止与旧水平输入(可刷新);新的水平只会在调用生效。
level
-新的压缩级别(0-9)
IllegalArgumentException
-如果压缩级别无效
public boolean needsInput()
public void finish()
public boolean finished()
public int deflate(byte[] b, int off, int len)
needsInput
应以确定是否需要更多的输入数据。
该方法采用NO_FLUSH
为压缩冲洗模式。这种形式的deflater.deflate(b, off, len)
方法调用产生相同的结果作为deflater.deflate(b, off, len, Deflater.NO_FLUSH)
调用。
b
-压缩数据的缓冲区
off
的起始偏移量的数据
len
-压缩数据的最大字节数
public int deflate(byte[] b)
needsInput
应以确定是否需要更多的输入数据。
该方法采用NO_FLUSH
为压缩冲洗模式。这种形式的deflater.deflate(b)
方法调用产生相同的结果作为deflater.deflate(b, 0, b.length, Deflater.NO_FLUSH)
调用。
b
-压缩数据的缓冲区
public int deflate(byte[] b, int off, int len, int flush)
压缩刷新模式是以下三种模式之一:
NO_FLUSH
:允许deflater的多少来决定数据的积累,在生产前的输出,以达到最佳的压缩(应该用在正常使用的情况下)。在这个冲洗模式0返回值表明needsInput()
应以确定是否需要更多的输入数据。SYNC_FLUSH
:所有未决的输出在deflater刷新,输出到指定的缓冲区,这样一个压缩数据上的作品打气筒可以得到所有的输入数据到目前为止(特别是needsInput()
返回true
之后调用如果提供足够的输出空间)。Flushing SYNC_FLUSH
可能降低一些压缩算法压缩,所以它只应在必要时使用。FULL_FLUSH
:所有未决的输出是冲出来与SYNC_FLUSH
。压缩状态复位,在压缩数据输出作品的打气筒可以重新从这一点如果以前的压缩数据已损坏或者随机访问所需的。使用FULL_FLUSH
往往会严重降低压缩。在FULL_FLUSH
或SYNC_FLUSH
的情况下,如果返回值是len
,输出缓冲b
可用空间,这种方法应该被调用,再以同样的flush
参数和输出空间。
b
-压缩数据的缓冲区
off
的起始偏移量的数据
len
-压缩数据的最大字节数
flush
-加压冲洗方式
IllegalArgumentException
-如果冲水模式无效
public int getAdler()
public int getTotalIn()
由于字节数可能大于integer.max_value,的getBytesRead()
方法已获得这一信息的首选手段。
public long getBytesRead()
public int getTotalOut()
由于字节数可能大于integer.max_value,的getBytesWritten()
方法已获得这一信息的首选手段。
public long getBytesWritten()
public void reset()
public void end()
protected void finalize()
finalize
方法重写,继承类
Object
WeakReference
,
PhantomReference
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.