< prev index next >

src/java.base/share/classes/java/util/zip/InflaterInputStream.java

Print this page




  34  * This class implements a stream filter for uncompressing data in the
  35  * "deflate" compression format. It is also used as the basis for other
  36  * decompression filters, such as GZIPInputStream.
  37  *
  38  * @see         Inflater
  39  * @author      David Connelly
  40  */
  41 public
  42 class InflaterInputStream extends FilterInputStream {
  43     /**
  44      * Decompressor for this stream.
  45      */
  46     protected Inflater inf;
  47 
  48     /**
  49      * Input buffer for decompression.
  50      */
  51     protected byte[] buf;
  52 
  53     /**





  54      * Length of input buffer.
  55      */
  56     protected int len;
  57 
  58     private boolean closed = false;
  59     // this flag is set to true after EOF has reached
  60     private boolean reachEOF = false;
  61 


  62     /**
  63      * Check to make sure that this stream has not been closed
  64      */
  65     private void ensureOpen() throws IOException {
  66         if (closed) {
  67             throw new IOException("Stream closed");
  68         }
  69     }
  70 
  71 
  72     /**
  73      * Creates a new input stream with the specified decompressor and
  74      * buffer size.
  75      * @param in the input stream
  76      * @param inf the decompressor ("inflater")
  77      * @param size the input buffer size
  78      * @exception IllegalArgumentException if {@code size <= 0}
  79      */
  80     public InflaterInputStream(InputStream in, Inflater inf, int size) {
  81         super(in);
  82         if (in == null || inf == null) {
  83             throw new NullPointerException();
  84         } else if (size <= 0) {
  85             throw new IllegalArgumentException("buffer size <= 0");
  86         }
  87         this.inf = inf;
  88         buf = new byte[size];

  89     }
  90 
  91     /**
  92      * Creates a new input stream with the specified decompressor and a
  93      * default buffer size.
  94      * @param in the input stream
  95      * @param inf the decompressor ("inflater")
  96      */
  97     public InflaterInputStream(InputStream in, Inflater inf) {
  98         this(in, inf, 512);
  99     }
 100 
 101     boolean usesDefaultInflater = false;
 102 
 103     /**
 104      * Creates a new input stream with a default decompressor and buffer size.
 105      * @param in the input stream
 106      */
 107     public InflaterInputStream(InputStream in) {
 108         this(in, new Inflater());


 222      */
 223     public void close() throws IOException {
 224         if (!closed) {
 225             if (usesDefaultInflater)
 226                 inf.end();
 227             in.close();
 228             closed = true;
 229         }
 230     }
 231 
 232     /**
 233      * Fills input buffer with more data to decompress.
 234      * @exception IOException if an I/O error has occurred
 235      */
 236     protected void fill() throws IOException {
 237         ensureOpen();
 238         len = in.read(buf, 0, buf.length);
 239         if (len == -1) {
 240             throw new EOFException("Unexpected end of ZLIB input stream");
 241         }






 242         inf.setInput(buf, 0, len);
 243     }
 244 
 245     /**
 246      * Tests if this input stream supports the <code>mark</code> and
 247      * <code>reset</code> methods. The <code>markSupported</code>
 248      * method of <code>InflaterInputStream</code> returns
 249      * <code>false</code>.
 250      *
 251      * @return  a <code>boolean</code> indicating if this stream type supports
 252      *          the <code>mark</code> and <code>reset</code> methods.
 253      * @see     java.io.InputStream#mark(int)
 254      * @see     java.io.InputStream#reset()
 255      */
 256     public boolean markSupported() {
 257         return false;
 258     }
 259 
 260     /**
 261      * Marks the current position in this input stream.


 267      *                      the mark position becomes invalid.
 268      * @see     java.io.InputStream#reset()
 269      */
 270     public synchronized void mark(int readlimit) {
 271     }
 272 
 273     /**
 274      * Repositions this stream to the position at the time the
 275      * <code>mark</code> method was last called on this input stream.
 276      *
 277      * <p> The method <code>reset</code> for class
 278      * <code>InflaterInputStream</code> does nothing except throw an
 279      * <code>IOException</code>.
 280      *
 281      * @exception  IOException  if this method is invoked.
 282      * @see     java.io.InputStream#mark(int)
 283      * @see     java.io.IOException
 284      */
 285     public synchronized void reset() throws IOException {
 286         throw new IOException("mark/reset not supported");











 287     }
 288 }


  34  * This class implements a stream filter for uncompressing data in the
  35  * "deflate" compression format. It is also used as the basis for other
  36  * decompression filters, such as GZIPInputStream.
  37  *
  38  * @see         Inflater
  39  * @author      David Connelly
  40  */
  41 public
  42 class InflaterInputStream extends FilterInputStream {
  43     /**
  44      * Decompressor for this stream.
  45      */
  46     protected Inflater inf;
  47 
  48     /**
  49      * Input buffer for decompression.
  50      */
  51     protected byte[] buf;
  52 
  53     /**
  54      * Original input buffer.
  55      */
  56     protected byte[] originBuf;
  57 
  58     /**
  59      * Length of input buffer.
  60      */
  61     protected int len;
  62 
  63     private boolean closed = false;
  64     // this flag is set to true after EOF has reached
  65     private boolean reachEOF = false;
  66 
  67     private ZipCryption zipCryption;
  68 
  69     /**
  70      * Check to make sure that this stream has not been closed
  71      */
  72     private void ensureOpen() throws IOException {
  73         if (closed) {
  74             throw new IOException("Stream closed");
  75         }
  76     }
  77 
  78 
  79     /**
  80      * Creates a new input stream with the specified decompressor and
  81      * buffer size.
  82      * @param in the input stream
  83      * @param inf the decompressor ("inflater")
  84      * @param size the input buffer size
  85      * @exception IllegalArgumentException if {@code size <= 0}
  86      */
  87     public InflaterInputStream(InputStream in, Inflater inf, int size) {
  88         super(in);
  89         if (in == null || inf == null) {
  90             throw new NullPointerException();
  91         } else if (size <= 0) {
  92             throw new IllegalArgumentException("buffer size <= 0");
  93         }
  94         this.inf = inf;
  95         buf = new byte[size];
  96         originBuf = new byte[size];
  97     }
  98 
  99     /**
 100      * Creates a new input stream with the specified decompressor and a
 101      * default buffer size.
 102      * @param in the input stream
 103      * @param inf the decompressor ("inflater")
 104      */
 105     public InflaterInputStream(InputStream in, Inflater inf) {
 106         this(in, inf, 512);
 107     }
 108 
 109     boolean usesDefaultInflater = false;
 110 
 111     /**
 112      * Creates a new input stream with a default decompressor and buffer size.
 113      * @param in the input stream
 114      */
 115     public InflaterInputStream(InputStream in) {
 116         this(in, new Inflater());


 230      */
 231     public void close() throws IOException {
 232         if (!closed) {
 233             if (usesDefaultInflater)
 234                 inf.end();
 235             in.close();
 236             closed = true;
 237         }
 238     }
 239 
 240     /**
 241      * Fills input buffer with more data to decompress.
 242      * @exception IOException if an I/O error has occurred
 243      */
 244     protected void fill() throws IOException {
 245         ensureOpen();
 246         len = in.read(buf, 0, buf.length);
 247         if (len == -1) {
 248             throw new EOFException("Unexpected end of ZLIB input stream");
 249         }
 250 
 251         if (zipCryption != null) {
 252             System.arraycopy(buf, 0, originBuf, 0, len);
 253             zipCryption.decryptBytes(buf, 0, len);
 254         }
 255 
 256         inf.setInput(buf, 0, len);
 257     }
 258 
 259     /**
 260      * Tests if this input stream supports the <code>mark</code> and
 261      * <code>reset</code> methods. The <code>markSupported</code>
 262      * method of <code>InflaterInputStream</code> returns
 263      * <code>false</code>.
 264      *
 265      * @return  a <code>boolean</code> indicating if this stream type supports
 266      *          the <code>mark</code> and <code>reset</code> methods.
 267      * @see     java.io.InputStream#mark(int)
 268      * @see     java.io.InputStream#reset()
 269      */
 270     public boolean markSupported() {
 271         return false;
 272     }
 273 
 274     /**
 275      * Marks the current position in this input stream.


 281      *                      the mark position becomes invalid.
 282      * @see     java.io.InputStream#reset()
 283      */
 284     public synchronized void mark(int readlimit) {
 285     }
 286 
 287     /**
 288      * Repositions this stream to the position at the time the
 289      * <code>mark</code> method was last called on this input stream.
 290      *
 291      * <p> The method <code>reset</code> for class
 292      * <code>InflaterInputStream</code> does nothing except throw an
 293      * <code>IOException</code>.
 294      *
 295      * @exception  IOException  if this method is invoked.
 296      * @see     java.io.InputStream#mark(int)
 297      * @see     java.io.IOException
 298      */
 299     public synchronized void reset() throws IOException {
 300         throw new IOException("mark/reset not supported");
 301     }
 302 
 303     /**
 304      * Set ZIP encryption/decryption engine to this inflater.
 305      *
 306      * @param zipCryption ZIP encrypt/decrypt engine. zip decryption will not
 307      * work if this value set to null.
 308      * @since 1.9
 309      */
 310     public void setZipCryption(ZipCryption zipCryption) {
 311         this.zipCryption = zipCryption;
 312     }
 313 }
< prev index next >