📜  Java Java类

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

Java Java类

此类实现了一个流过滤器,用于读取 GZIP 文件格式的压缩数据。

构造函数

  • GZIPInputStream(InputStream in) :创建一个具有默认缓冲区大小的新输入流。
  • GZIPInputStream(InputStream in, int size) :创建一个具有指定缓冲区大小的新输入流。

方法 :

  • void close() :关闭此输入流并释放与该流关联的所有系统资源。
    Syntax :public void close()
               throws IOException
    Specified by:
    close in interface Closeable
    Specified by:
    close in interface AutoCloseable
    Overrides:
    close in class InflaterInputStream
    Throws:
    IOException 
  • int read(byte[] buf, int off, int len) :将未压缩的数据读入字节数组。如果 len 不为零,则该方法将阻塞,直到可以解压缩某些输入;否则,不读取任何字节并返回 0。
    Syntax :public int read(byte[] buf,
           int off,
           int len)
             throws IOException
    Overrides:
    read in class InflaterInputStream
    Parameters:
    buf - 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 stream is reached
    Throws:
    NullPointerException
    IndexOutOfBoundsException
    ZipException
    IOException 

从类Java.util.zip.InflaterInputStream 继承的方法
可用、填充、标记、标记支持、读取、重置、跳过
从类Java.io.FilterInputStream 继承的方法

从类Java.lang.Object 继承的方法
克隆,等于,完成,getClass,hashCode,通知,notifyAll,toString,等待,等待,等待

程序 :

//Java program demonstrating GZipInputStream methods 
  
import java.io.FileInputStream;              
import java.io.FileOutputStream;     
import java.io.IOException;              
import java.util.Arrays;
import java.util.zip.GZIPInputStream; 
  
class GZipInputStreamDemo        
{                                                                            
    public static void main(String[] args) throws IOException 
    {                                                                                            
        FileInputStream fis = new FileInputStream("file.txt"); 
        GZIPInputStream gzis = new GZIPInputStream(fis);    
          
        //Uncompressed FileContents      
        //01234567890 
        byte b[]=new byte[10];
                                                      
        //skipping 1 byte    
        gzis.skip(1);
          
        //illustrating available() and 
        //read(byte b[],int off,int len) 
        if( gzis.available()!=-1)    
            gzis.read(b);                    
        System.out.println(Arrays.toString(b));
                                      
        //closing the stream                                                 
        gzis.close();                                                        
    }                                                                        
} 

输出 :

[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]