📜  Java Java类

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

Java Java类

此类实现了一个流过滤器,用于以“deflate”压缩格式解压缩数据。它也被用作其他解压过滤器的基础,例如 GZIPInputStream。
构造函数

  • InflaterInputStream(InputStream in) :创建一个具有默认解压缩器和缓冲区大小的新输入流。
  • InflaterInputStream(InputStream in, Inflater inf) :使用指定的解压缩器和默认缓冲区大小创建一个新的输入流。
  • InflaterInputStream(InputStream in, Inflater inf, int size) :使用指定的解压器和缓冲区大小创建一个新的输入流。

方法:

  • int available() :到达 EOF 后返回 0,否则总是返回 1。
    Syntax : public int available()
                  throws IOException
    Returns:
    1 before EOF and 0 after EOF.
    Throws:
    IOException
    
  • void close() :关闭此输入流并释放与该流关联的所有系统资源。
    Syntax : public void close()
               throws IOException
    Throws:
    IOException 
    
  • protected void fill() :用更多要解压缩的数据填充输入缓冲区。
    Syntax : protected void fill()
                 throws IOException
    Throws:
    IOException
    
  • void mark(int readlimit) :标记此输入流中的当前位置。
    Syntax : public void mark(int readlimit)
    Parameters:
    readlimit - the maximum limit of bytes that can be read
    before the mark position becomes invalid.
  • boolean markSupported() :测试此输入流是否支持 mark 和 reset 方法。
    Syntax : public boolean markSupported()
    Returns:
    a boolean indicating if this stream type supports the mark and reset methods.
    
  • int read() :读取一个字节的未压缩数据。
    Syntax : public int read()
             throws IOException
    Returns:
    the byte read, or -1 if end of compressed input 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 - the buffer into which the data is read
    off - the start offset in the destination array b
    len - the maximum number of bytes read
    Returns:
    the actual number of bytes read, or -1 if the end of the compressed input is reached.
    Throws:
    NullPointerException 
    IndexOutOfBoundsException 
    ZipException
    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 - the number of bytes to skip
    Returns:
    the actual number of bytes skipped.
    Throws:
    IOException 
    IllegalArgumentException 

程序:

//Java program to demonstrate InflaterInputStream
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.InflaterInputStream;
  
  
class InflaterInputStreamDemo
{
    public static void main(String[] args) throws IOException
    {
  
        FileOutputStream fos = new FileOutputStream("file.txt");
  
        //Assign FileOutputStream to DeflaterOutputStream
        DeflaterOutputStream dos = new DeflaterOutputStream(fos);
  
        //write it into DeflaterOutputStream
        for (int i = 0; i < 10; i++)
        {
            dos.write(i);
        }
        dos.flush();
        dos.close();
        FileInputStream fis = new FileInputStream("file.txt");
        InflaterInputStream iis = new InflaterInputStream(fis);
  
        //illustrating available() method
        System.out.println(iis.available());
  
        //illustrating mark and markSupported()
        if (iis.markSupported())
            iis.mark(15);
        else
            System.out.println(false);
          
        //illustrating skip() method
        iis.skip(3);
  
        //illustrating read()
        for (int i = 0; i <3 ; i++) 
        {
            System.out.print(iis.read());
        }
  
        //illustrating read(byte[] b,int off,int len)
        byte b[]=new byte[4];
          
        for (int i = 0; i <4 ; i++) 
        {
            iis.read(b,0,4);
        }
          
        for (int i = 0; i < 4; i++) 
        {
            System.out.print(b[i]);
        }
    }
}

输出:

1
false
3456789


下一篇:
Java Java类