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

Print this page




   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.util.zip;
  27 
  28 import java.util.function.LongConsumer;
  29 import java.util.function.LongSupplier;
  30 
  31 /**
  32  * This class provides support for general purpose decompression using the
  33  * popular ZLIB compression library. The ZLIB compression library was
  34  * initially developed as part of the PNG graphics standard and is not
  35  * protected by patents. It is fully described in the specifications at
  36  * the <a href="package-summary.html#package.description">java.util.zip
  37  * package description</a>.
  38  *
  39  * <p>The following code fragment demonstrates a trivial compression
  40  * and decompression of a string using {@code Deflater} and
  41  * {@code Inflater}.
  42  *
  43  * <blockquote><pre>
  44  * try {
  45  *     // Encode a String into bytes
  46  *     String inputString = "blahblahblah\u20AC\u20AC";
  47  *     byte[] input = inputString.getBytes("UTF-8");
  48  *
  49  *     // Compress the bytes
  50  *     byte[] output = new byte[100];


 101 
 102     private static final byte[] defaultBuf = new byte[0];
 103 
 104     static {
 105         ZipUtils.loadLibrary();
 106         initIDs();
 107     }
 108 
 109     /**
 110      * Creates a new decompressor. If the parameter 'nowrap' is true then
 111      * the ZLIB header and checksum fields will not be used. This provides
 112      * compatibility with the compression format used by both GZIP and PKZIP.
 113      * <p>
 114      * Note: When using the 'nowrap' option it is also necessary to provide
 115      * an extra "dummy" byte as input. This is required by the ZLIB native
 116      * library in order to support certain optimizations.
 117      *
 118      * @param nowrap if true then support GZIP compatible compression
 119      */
 120     public Inflater(boolean nowrap) {
 121         this.zsRef = ZStreamRef.get(this,
 122                 // Desugared for startup purposes.
 123                 new LongSupplier() {
 124                     @Override
 125                     public long getAsLong() {
 126                         return init(nowrap);
 127                     }
 128                 },
 129                 new LongConsumer() {
 130                     @Override
 131                     public void accept(long value) {
 132                         end();
 133                     }
 134                 });
 135     }
 136 
 137     /**
 138      * Creates a new decompressor.
 139      */
 140     public Inflater() {
 141         this(false);
 142     }
 143 
 144     /**
 145      * Sets input data for decompression. Should be called whenever
 146      * needsInput() returns true indicating that more input data is
 147      * required.
 148      * @param b the input data bytes
 149      * @param off the start offset of the input data
 150      * @param len the length of the input data
 151      * @see Inflater#needsInput
 152      */
 153     public void setInput(byte[] b, int off, int len) {
 154         if (b == null) {




   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.util.zip;
  27 



  28 /**
  29  * This class provides support for general purpose decompression using the
  30  * popular ZLIB compression library. The ZLIB compression library was
  31  * initially developed as part of the PNG graphics standard and is not
  32  * protected by patents. It is fully described in the specifications at
  33  * the <a href="package-summary.html#package.description">java.util.zip
  34  * package description</a>.
  35  *
  36  * <p>The following code fragment demonstrates a trivial compression
  37  * and decompression of a string using {@code Deflater} and
  38  * {@code Inflater}.
  39  *
  40  * <blockquote><pre>
  41  * try {
  42  *     // Encode a String into bytes
  43  *     String inputString = "blahblahblah\u20AC\u20AC";
  44  *     byte[] input = inputString.getBytes("UTF-8");
  45  *
  46  *     // Compress the bytes
  47  *     byte[] output = new byte[100];


  98 
  99     private static final byte[] defaultBuf = new byte[0];
 100 
 101     static {
 102         ZipUtils.loadLibrary();
 103         initIDs();
 104     }
 105 
 106     /**
 107      * Creates a new decompressor. If the parameter 'nowrap' is true then
 108      * the ZLIB header and checksum fields will not be used. This provides
 109      * compatibility with the compression format used by both GZIP and PKZIP.
 110      * <p>
 111      * Note: When using the 'nowrap' option it is also necessary to provide
 112      * an extra "dummy" byte as input. This is required by the ZLIB native
 113      * library in order to support certain optimizations.
 114      *
 115      * @param nowrap if true then support GZIP compatible compression
 116      */
 117     public Inflater(boolean nowrap) {
 118         this.zsRef = ZStreamRef.get(this, () -> init(nowrap), Inflater::end);













 119     }
 120 
 121     /**
 122      * Creates a new decompressor.
 123      */
 124     public Inflater() {
 125         this(false);
 126     }
 127 
 128     /**
 129      * Sets input data for decompression. Should be called whenever
 130      * needsInput() returns true indicating that more input data is
 131      * required.
 132      * @param b the input data bytes
 133      * @param off the start offset of the input data
 134      * @param len the length of the input data
 135      * @see Inflater#needsInput
 136      */
 137     public void setInput(byte[] b, int off, int len) {
 138         if (b == null) {