📜  Java Java类

📅  最后修改于: 2022-05-13 01:55:32.477000             🧑  作者: Mango

Java Java类

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

构造函数和描述

  • DeflaterInputStream(InputStream in) :创建一个具有默认压缩器和缓冲区大小的新输入流。
  • 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
    Returns:
    the actual number of bytes skipped
    Throws:
    IOException
  • void close() :关闭此输入流及其底层输入流,丢弃任何未决的未压缩数据。
    Syntax :public void close()
               throws IOException
    Overrides:
    close in class FilterInputStream
    Throws:
    IOException
  • void mark(int limit) :不支持此操作。
    Syntax :public void mark(int limit)
    Parameters:
    limit - maximum bytes that can be read before invalidating the position marker
    
  • boolean markSupported() :始终返回 false,因为此输入流不支持 mark() 和 reset() 方法。
    Syntax :public boolean markSupported()
    Returns:
    false, always
  • int read() :从输入流中读取单个字节的压缩数据。
    Syntax :public int read()
             throws IOException
    Returns:
    a single byte of compressed data, or -1 if the end of the uncompressed
    input stream is reached
    Throws:
    IOException
  • int read(byte[] b, int off, 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
    Returns:
    the actual number of bytes read, or -1 if the end of the
    uncompressed input stream is reached
    Throws:
    IndexOutOfBoundsException 
    IOException 
  • void reset() :不支持此操作。
    Syntax :public void reset()
               throws IOException
    Throws:
    IOException
  • long skip(long n) :跳过并丢弃输入流中的数据。
    Syntax :public long skip(long n)
              throws IOException
    Parameters:
    n - number of bytes to be skipped
    Returns:
    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类