public class Inflater
extends Object
| java.lang.Object | |
| ↳ | java.util.zip.Inflater |
该类使用流行的ZLIB压缩库为通用目的解压提供支持。 ZLIB压缩库最初是作为PNG图形标准的一部分开发的,不受专利保护。 它在java.util.zip package description的规格中有详细描述。
以下代码片段演示了使用 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);
// 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
}
也可以看看:
Public constructors |
|
|---|---|
Inflater(boolean nowrap) 创建一个新的解压缩器。 |
|
Inflater() 创建一个新的解压缩器。 |
|
Public methods |
|
|---|---|
void |
end() 关闭解压缩器并丢弃任何未处理的输入。 |
boolean |
finished() 如果已到达压缩数据流的末尾,则返回true。 |
int |
getAdler() 返回未压缩数据的ADLER-32值。 |
long |
getBytesRead() 返回迄今为止输入的压缩字节总数。 |
long |
getBytesWritten() 返回到目前为止输出的未压缩字节的总数。 |
int |
getRemaining() 返回输入缓冲区中剩余的字节总数。 |
int |
getTotalIn() 返回迄今为止输入的压缩字节总数。 |
int |
getTotalOut() 返回到目前为止输出的未压缩字节的总数。 |
int |
inflate(byte[] b) 将字节解压缩到指定的缓冲区中。 |
int |
inflate(byte[] b, int off, int len) 将字节解压缩到指定的缓冲区中。 |
boolean |
needsDictionary() 如果解压缩需要预设字典,则返回true。 |
boolean |
needsInput() 如果输入缓冲区中没有数据,则返回true。 |
void |
reset() 重置充气器,以便可以处理一组新的输入数据。 |
void |
setDictionary(byte[] b, int off, int len) 将预设字典设置为给定的字节数组。 |
void |
setDictionary(byte[] b) 将预设字典设置为给定的字节数组。 |
void |
setInput(byte[] b, int off, int len) 设置解压缩的输入数据。 |
void |
setInput(byte[] b) 设置解压缩的输入数据。 |
Protected methods |
|
|---|---|
void |
finalize() 收集垃圾时关闭解压缩程序。 |
Inherited methods |
|
|---|---|
java.lang.Object
|
|
Inflater (boolean nowrap)
创建一个新的解压缩器。 如果参数'nowrap'为true,则不会使用ZLIB头和校验和字段。 这提供了与GZIP和PKZIP使用的压缩格式的兼容性。
注意:使用'nowrap'选项时,还需要提供一个额外的“虚拟”字节作为输入。 这是ZLIB本地库所必需的,以支持某些优化。
| Parameters | |
|---|---|
nowrap |
boolean: if true then support GZIP compatible compression |
void end ()
关闭解压缩器并丢弃任何未处理的输入。 当解压缩器不再被使用时应该调用此方法,但也会由finalize()方法自动调用此方法。 一旦调用这个方法,Inflater对象的行为就是未定义的。
boolean finished ()
如果已到达压缩数据流的末尾,则返回true。
| Returns | |
|---|---|
boolean |
true if the end of the compressed data stream has been reached |
int getAdler ()
返回未压缩数据的ADLER-32值。
| Returns | |
|---|---|
int |
the ADLER-32 value of the uncompressed data |
long getBytesRead ()
返回迄今为止输入的压缩字节总数。
| Returns | |
|---|---|
long |
the total (non-negative) number of compressed bytes input so far |
long getBytesWritten ()
返回到目前为止输出的未压缩字节的总数。
| Returns | |
|---|---|
long |
the total (non-negative) number of uncompressed bytes output so far |
int getRemaining ()
返回输入缓冲区中剩余的字节总数。 这可以用于找出解压缩完成后输入缓冲区中还有哪些字节。
| Returns | |
|---|---|
int |
the total number of bytes remaining in the input buffer |
int getTotalIn ()
返回迄今为止输入的压缩字节总数。
由于字节数可能大于Integer.MAX_VALUE,因此 getBytesRead()方法是获取此信息的首选方法。
| Returns | |
|---|---|
int |
the total number of compressed bytes input so far |
int getTotalOut ()
返回到目前为止输出的未压缩字节的总数。
Since the number of bytes may be greater than Integer.MAX_VALUE, the getBytesWritten() method is now the preferred means of obtaining this information.
| Returns | |
|---|---|
int |
the total number of uncompressed bytes output so far |
int inflate (byte[] b)
将字节解压缩到指定的缓冲区中。 返回未压缩的实际字节数。 返回值0表示应该调用needsInput()或needsDictionary()以确定是否需要更多输入数据或预设字典。 在后一种情况下,可以使用getAdler()来获取所需字典的Adler-32值。
| Parameters | |
|---|---|
b |
byte: the buffer for the uncompressed data |
| Returns | |
|---|---|
int |
the actual number of uncompressed bytes |
| Throws | |
|---|---|
DataFormatException |
if the compressed data format is invalid |
也可以看看:
int inflate (byte[] b,
int off,
int len)
将字节解压缩到指定的缓冲区中。 返回未压缩的实际字节数。 返回值0表示应该调用needsInput()或needsDictionary()以确定是否需要更多输入数据或预设字典。 在后一种情况下,可以使用getAdler()来获取所需字典的Adler-32值。
| Parameters | |
|---|---|
b |
byte: the buffer for the uncompressed data |
off |
int: the start offset of the data |
len |
int: the maximum number of uncompressed bytes |
| Returns | |
|---|---|
int |
the actual number of uncompressed bytes |
| Throws | |
|---|---|
DataFormatException |
if the compressed data format is invalid |
也可以看看:
boolean needsDictionary ()
如果解压缩需要预设字典,则返回true。
| Returns | |
|---|---|
boolean |
true if a preset dictionary is needed for decompression |
也可以看看:
boolean needsInput ()
如果输入缓冲区中没有数据,则返回true。 这可以用来确定是否应该调用#setInput以提供更多输入。
| Returns | |
|---|---|
boolean |
true if no data remains in the input buffer |
void setDictionary (byte[] b,
int off,
int len)
将预设字典设置为给定的字节数组。 当inflate()返回0时应该被调用,needsDictionary()返回true表示需要预置字典。 方法getAdler()可用于获取所需字典的Adler-32值。
| Parameters | |
|---|---|
b |
byte: the dictionary data bytes |
off |
int: the start offset of the data |
len |
int: the length of the data |
也可以看看:
void setDictionary (byte[] b)
将预设字典设置为给定的字节数组。 当inflate()返回0时应该被调用,needsDictionary()返回true表示需要预置字典。 方法getAdler()可用于获取所需字典的Adler-32值。
| Parameters | |
|---|---|
b |
byte: the dictionary data bytes |
也可以看看:
void setInput (byte[] b,
int off,
int len)
设置解压缩的输入数据。 每当needsInput()返回true时应该调用,表示需要更多输入数据。
| Parameters | |
|---|---|
b |
byte: the input data bytes |
off |
int: the start offset of the input data |
len |
int: the length of the input data |
也可以看看:
void setInput (byte[] b)
设置解压缩的输入数据。 每当needsInput()返回true时应该调用,表示需要更多输入数据。
| Parameters | |
|---|---|
b |
byte: the input data bytes |
也可以看看: