📜  Java Java类

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

Java Java类

此类实现了一个输出流过滤器,用于解压缩以“deflate”压缩格式存储的数据。
构造函数

  • InflaterOutputStream(OutputStream out) :创建一个具有默认解压缩器和缓冲区大小的新输出流。
  • InflaterOutputStream(OutputStream out, Inflater infl) :使用指定的解压缩器和默认缓冲区大小创建一个新的输出流。
  • InflaterOutputStream(OutputStream out, Inflater infl, int bufLen) :使用指定的解压器和缓冲区大小创建一个新的输出流。

方法:

  • void close() :将任何剩余的未压缩数据写入输出流并关闭底层输出流。
    Syntax :
    public void close()
               throws IOException
    Overrides:
    close in class FilterOutputStream
    Throws:
    IOException
  • void finish() :在不关闭底层流的情况下完成将未压缩数据写入输出流。
    Syntax :public void finish()
                throws IOException
    Throws:
    IOException
  • void flush() :刷新此输出流,强制写入任何待处理的缓冲输出字节。
    Syntax :public void flush()
               throws IOException
    Overrides:
    flush in class FilterOutputStream
    Throws:
    IOException
  • void write(byte[] b, int off, int len) :将字节数组写入未压缩的输出流。
    Syntax :public void write(byte[] b,
             int off,
             int len)
               throws IOException
    Overrides:
    write in class FilterOutputStream
    Parameters:
    b - buffer containing compressed data to decompress and write to the output stream
    off - starting offset of the compressed data within b
    len - number of bytes to decompress from b
    Throws:
    IndexOutOfBoundsException 
    IOException
    NullPointerException 
    ZipException
  • void write(int b) :将一个字节写入未压缩的输出流。
    Syntax :public void write(int b)
               throws IOException
    Parameters:
    b - a single byte of compressed data to decompress and write to the output stream
    Throws:
    IOException
    ZipException 
    

程序 1

//Java program to illustrate InflaterInputStream class
import java.io.*;
import java.util.Arrays;
import java.util.zip.DeflaterInputStream;
import java.util.zip.InflaterOutputStream;
  
class InflaterOutputStreamDemo 
{
    public static void main(String[] args) throws IOException 
    {
        byte b[] = {1, 2, 3, 4, 5, 6};
        ByteArrayInputStream bin = new ByteArrayInputStream(b);
        DeflaterInputStream din = new DeflaterInputStream(bin);
        byte c[] = new byte[10];
        din.read(c);
        din.close();
  
  
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        InflaterOutputStream ios = new InflaterOutputStream(bos);
        //illustrating write(byte b[],int off,int len)
        ios.write(c);
  
        //flushing
        ios.flush();
          
        //Finishes writing uncompressed data to the output stream
        // without closing the underlying stream.
        ios.finish();
        System.out.println(Arrays.toString(bos.toByteArray()));
        bos.close();
          
        //illustrating close()
        ios.close();
    }
}

输出 :

[1, 2, 3, 4, 5, 6]

方案二:

//Java program to illustrate InflaterInputStream class
import java.io.*;
import java.util.Arrays;
import java.util.zip.DeflaterInputStream;
import java.util.zip.InflaterOutputStream;
  
class InflaterOutputStreamDemo 
{
    public static void main(String[] args) throws IOException 
    {
        byte b[] = {1, 2, 3, 4, 5, 6};
        ByteArrayInputStream bin = new ByteArrayInputStream(b);
        DeflaterInputStream din = new DeflaterInputStream(bin);
        FileOutputStream fos=new FileOutputStream("file.txt");
        byte c[] = new byte[10];
        din.read(c);
        fos.write(c);
        din.close();
        fos.close();
  
        //reading the compressed data
        FileInputStream fis = new FileInputStream("file.txt");
  
        ByteArrayOutputStream bos1=new ByteArrayOutputStream();
        InflaterOutputStream ios = new InflaterOutputStream(bos1);
        int ch;
          
        //illustrating write() method
        while ( (ch=fis.read() ) != -1) 
        {
            ios.write(ch);
        }
        System.out.print(Arrays.toString(bos1.toByteArray()));
    }
  
}

输出 :

[1, 2, 3, 4, 5, 6]

下一篇: Java Java类