public class Inflater 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);
// 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
}
Deflater
| Constructor and Description |
|---|
Inflater()
创建一个新的解压缩程序。
|
Inflater(boolean nowrap)
创建一个新的解压缩程序。
|
| Modifier and Type | Method and Description |
|---|---|
void |
end()
关闭解压缩程序并丢弃任何未处理的输入。
|
protected void |
finalize()
收集垃圾时关闭解压缩程序。
|
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)
将预设字典设置为给定的字节数组。
|
void |
setDictionary(byte[] b, int off, int len)
将预设字典设置为给定的字节数组。
|
void |
setInput(byte[] b)
设置解压缩的输入数据。
|
void |
setInput(byte[] b, int off, int len)
设置解压缩的输入数据。
|
public Inflater(boolean nowrap)
注意:当使用'nowrap'选项时,还需要提供一个额外的“dummy”字节作为输入。 为了支持某些优化,这是ZLIB本机库所必需的。
nowrap - 如果为true,则支持GZIP兼容压缩
public Inflater()
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 - 数据的长度
needsDictionary() ,
getAdler()
public void setDictionary(byte[] b)
b - 字典数据字节
needsDictionary() ,
getAdler()
public int getRemaining()
public boolean needsInput()
public boolean needsDictionary()
setDictionary(byte[], int, int)
public boolean finished()
public int inflate(byte[] b,
int off,
int len)
throws DataFormatException
b - 未压缩数据的缓冲区
off - 数据的起始偏移量
len - 未压缩字节的最大数量
DataFormatException - 如果压缩数据格式无效
needsInput() ,
needsDictionary()
public int inflate(byte[] b)
throws DataFormatException
b - 未压缩数据的缓冲区
DataFormatException - 如果压缩数据格式无效
needsInput() ,
needsDictionary()
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.