当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Java.util.zip.DeflaterInputStream用法及代码示例


实现输入流过滤器,用于以 “deflate” 压缩格式压缩数据。

构造函数和说明

  • DeflaterInputStream(输入流中):使用默认压缩器和缓冲区大小创建新的输入流。
  • DeflaterInputStream(InputStream in, Deflater defl):使用指定的压缩器和默认缓冲区大小创建新的输入流。
  • DeflaterInputStream(InputStream in, Deflater defl, int bufLen) :使用指定的压缩器和缓冲区大小创建新的输入流。

方法:

  • int available():到达 EOF 后返回 0,否则始终返回 1。
    Syntax :public int available()
                  throws IOException
    Parameters:
    n - number of bytes to be skipped
    返回:
    the actual number of bytes skipped
    Throws:
    IOException
  • 无效close():关闭此输入流及其底层输入流,丢弃任何挂起的未压缩数据。
    Syntax :public void close()
               throws IOException
    Overrides:
    close in class FilterInputStream
    Throws:
    IOException
  • 无效标记(int limit):不支持该操作。
    Syntax :public void mark(int limit)
    参数:
    limit - maximum bytes that can be read before invalidating the position marker
    
  • 布尔值markSupported():始终返回 false,因为此输入流不支持 mark() 和 reset() 方法。
    Syntax :public boolean markSupported()
    返回:
    false, always
  • int read():从输入流读取单个字节的压缩数据。
    Syntax :public int read()
             throws IOException
    返回:
    a single byte of compressed data, or -1 if the end of the uncompressed
    input stream is reached
    Throws:
    IOException
  • int 读取(字节[] b,int 关闭,int len):将压缩数据读入字节数组。
    Syntax :public int read(byte[] b,
           int off,
           int len)
             throws IOException
    Parameters:
    b - buffer into which the data is read
    off - starting offset of the data within b
    len - maximum number of compressed bytes to read into b
    返回:
    the actual number of bytes read, or -1 if the end of the
    uncompressed input stream is reached
    Throws:
    IndexOutOfBoundsException 
    IOException 
  • 无效reset():不支持该操作。
    Syntax :public void reset()
               throws IOException
    Throws:
    IOException
  • 长跳过(长n):跳过并丢弃输入流中的数据。
    Syntax :public long skip(long n)
              throws IOException
    参数:
    n - number of bytes to be skipped
    返回:
    the actual number of bytes skipped
    Throws:
    IOException

程序:


//Java program to illustrate DeflaterInputStream class 
import java.io.ByteArrayInputStream; 
import java.io.IOException; 
import java.util.zip.DeflaterInputStream; 
  
class DeflaterInputStreamDemo 
{ 
    public static void main(String[] args) throws IOException 
    { 
        byte b[] = new byte[10]; 
        for (byte i = 0; i <10 ; i++) 
        { 
            b[i] = i; 
        } 
        ByteArrayInputStream bin = new ByteArrayInputStream(b); 
        DeflaterInputStream din = new DeflaterInputStream(bin); 
  
        //illustrating markSupported() method 
        System.out.println(din.markSupported()); 
          
        //illustrating skip() method 
        din.skip(1); 
  
        //illustrating available() method 
        System.out.println(din.available()); 
  
        //illustrating read(byte[] b,int off,int len) 
        byte c[] = new byte[10]; 
          
        din.read(c,0,9); 
                for (int i = 0; i < 9; i++) 
        { 
            System.out.print(c[i]); 
        } 
        while(din.available() == 1) 
        { 
            //Reads a single byte of compressed data 
            System.out.print(din.read()); 
        } 
          
        System.out.println(); 
        System.out.println(din.available()); 
  
        // illustrating close() method 
        din.close(); 
    } 
} 

输出:

false
1
-1009996100981029710199231224400175046-1
0

上面的输出表示压缩数据。

下一篇:Java 中的 Java.util.zip.DeflaterOutputStream 类



相关用法


注:本文由纯净天空筛选整理自佚名大神的英文原创作品 Java.util.zip.DeflaterInputStream class in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。