src/share/classes/java/util/zip/Deflater.java

Print this page




 105      */
 106     public static final int DEFAULT_COMPRESSION = -1;
 107 
 108     /**
 109      * Compression strategy best used for data consisting mostly of small
 110      * values with a somewhat random distribution. Forces more Huffman coding
 111      * and less string matching.
 112      */
 113     public static final int FILTERED = 1;
 114 
 115     /**
 116      * Compression strategy for Huffman coding only.
 117      */
 118     public static final int HUFFMAN_ONLY = 2;
 119 
 120     /**
 121      * Default compression strategy.
 122      */
 123     public static final int DEFAULT_STRATEGY = 0;
 124 



























 125     static {
 126         /* Zip library is loaded from System.initializeSystemClass */
 127         initIDs();
 128     }
 129 
 130     /**
 131      * Creates a new compressor using the specified compression level.
 132      * If 'nowrap' is true then the ZLIB header and checksum fields will
 133      * not be used in order to support the compression format used in
 134      * both GZIP and PKZIP.
 135      * @param level the compression level (0-9)
 136      * @param nowrap if true then use GZIP compatible compression
 137      */
 138     public Deflater(int level, boolean nowrap) {
 139         this.level = level;
 140         this.strategy = DEFAULT_STRATEGY;
 141         strm = init(level, DEFAULT_STRATEGY, nowrap);
 142     }
 143 
 144     /**


 272 
 273     /**
 274      * When called, indicates that compression should end with the current
 275      * contents of the input buffer.
 276      */
 277     public synchronized void finish() {
 278         finish = true;
 279     }
 280 
 281     /**
 282      * Returns true if the end of the compressed data output stream has
 283      * been reached.
 284      * @return true if the end of the compressed data output stream has
 285      * been reached
 286      */
 287     public synchronized boolean finished() {
 288         return finished;
 289     }
 290 
 291     /**
 292      * Fills specified buffer with compressed data. Returns actual number
 293      * of bytes of compressed data. A return value of 0 indicates that
 294      * needsInput() should be called in order to determine if more input
 295      * data is required.






 296      * @param b the buffer for the compressed data
 297      * @param off the start offset of the data
 298      * @param len the maximum number of bytes of compressed data
 299      * @return the actual number of bytes of compressed data

 300      */
 301     public synchronized int deflate(byte[] b, int off, int len) {
 302         if (b == null) {
 303             throw new NullPointerException();
 304         }
 305         if (off < 0 || len < 0 || off > b.length - len) {
 306             throw new ArrayIndexOutOfBoundsException();
 307         }
 308         return deflateBytes(b, off, len);
 309     }
 310 
 311     /**
 312      * Fills specified buffer with compressed data. Returns actual number
 313      * of bytes of compressed data. A return value of 0 indicates that
 314      * needsInput() should be called in order to determine if more input
 315      * data is required.






 316      * @param b the buffer for the compressed data
 317      * @return the actual number of bytes of compressed data

 318      */
 319     public int deflate(byte[] b) {
 320         return deflate(b, 0, b.length);
 321     }
 322 
 323     /**

























































 324      * Returns the ADLER-32 value of the uncompressed data.
 325      * @return the ADLER-32 value of the uncompressed data
 326      */
 327     public synchronized int getAdler() {
 328         ensureOpen();
 329         return getAdler(strm);
 330     }
 331 
 332     /**
 333      * Returns the total number of uncompressed bytes input so far.
 334      *
 335      * <p>Since the number of bytes may be greater than
 336      * Integer.MAX_VALUE, the {@link #getBytesRead()} method is now
 337      * the preferred means of obtaining this information.</p>
 338      *
 339      * @return the total number of uncompressed bytes input so far
 340      */
 341     public int getTotalIn() {
 342         return (int) getBytesRead();
 343     }


 403             buf = null;
 404         }
 405     }
 406 
 407     /**
 408      * Closes the compressor when garbage is collected.
 409      */
 410     protected void finalize() {
 411         end();
 412     }
 413 
 414     private void ensureOpen() {
 415         if (strm == 0)
 416             throw new NullPointerException();
 417     }
 418 
 419     private static native void initIDs();
 420     private native static long init(int level, int strategy, boolean nowrap);
 421     private native static void setDictionary(long strm, byte[] b, int off,
 422                                              int len);
 423     private native int deflateBytes(byte[] b, int off, int len);
 424     private native static int getAdler(long strm);
 425     private native static long getBytesRead(long strm);
 426     private native static long getBytesWritten(long strm);
 427     private native static void reset(long strm);
 428     private native static void end(long strm);
 429 }


 105      */
 106     public static final int DEFAULT_COMPRESSION = -1;
 107 
 108     /**
 109      * Compression strategy best used for data consisting mostly of small
 110      * values with a somewhat random distribution. Forces more Huffman coding
 111      * and less string matching.
 112      */
 113     public static final int FILTERED = 1;
 114 
 115     /**
 116      * Compression strategy for Huffman coding only.
 117      */
 118     public static final int HUFFMAN_ONLY = 2;
 119 
 120     /**
 121      * Default compression strategy.
 122      */
 123     public static final int DEFAULT_STRATEGY = 0;
 124 
 125     /**
 126      * Compression flush mode used to achieve best compression result.
 127      *
 128      * @see Deflater#deflate(byte[], int, int, int)
 129      * @since 1.7
 130      */
 131     public static final int NO_FLUSH = 0;
 132 
 133     /**
 134      * Compression flush mode used to flush out all pending output; may
 135      * degrade compression for some compression algorithms.
 136      *
 137      * @see Deflater#deflate(byte[], int, int, int)
 138      * @since 1.7
 139      */
 140     public static final int SYNC_FLUSH = 2;
 141 
 142     /**
 143      * Compression flush mode used to flush out all pending output and
 144      * reset the deflater. Using this mode too often can seriously degrade
 145      * compression.
 146      *
 147      * @see Deflater#deflate(byte[], int, int, int)
 148      * @since 1.7
 149      */
 150     public static final int FULL_FLUSH = 3;
 151 
 152     static {
 153         /* Zip library is loaded from System.initializeSystemClass */
 154         initIDs();
 155     }
 156 
 157     /**
 158      * Creates a new compressor using the specified compression level.
 159      * If 'nowrap' is true then the ZLIB header and checksum fields will
 160      * not be used in order to support the compression format used in
 161      * both GZIP and PKZIP.
 162      * @param level the compression level (0-9)
 163      * @param nowrap if true then use GZIP compatible compression
 164      */
 165     public Deflater(int level, boolean nowrap) {
 166         this.level = level;
 167         this.strategy = DEFAULT_STRATEGY;
 168         strm = init(level, DEFAULT_STRATEGY, nowrap);
 169     }
 170 
 171     /**


 299 
 300     /**
 301      * When called, indicates that compression should end with the current
 302      * contents of the input buffer.
 303      */
 304     public synchronized void finish() {
 305         finish = true;
 306     }
 307 
 308     /**
 309      * Returns true if the end of the compressed data output stream has
 310      * been reached.
 311      * @return true if the end of the compressed data output stream has
 312      * been reached
 313      */
 314     public synchronized boolean finished() {
 315         return finished;
 316     }
 317 
 318     /**
 319      * Compresses the input data and fills specified buffer with compressed
 320      * data. Returns actual number of bytes of compressed data. A return value
 321      * of 0 indicates that {@link needsInput() needsInput} should be called
 322      * in order to determine if more input data is required.
 323      *
 324      * <p>This method uses {@link #NO_FLUSH} as its compression flush mode.
 325      * An invocation of this method of the form {@code deflater.deflate(b, off, len)}
 326      * yields the same result as the invocation of
 327      * {@code deflater.deflate(b, off, len, Deflater.NO_FLUSH)}.
 328      *
 329      * @param b the buffer for the compressed data
 330      * @param off the start offset of the data
 331      * @param len the maximum number of bytes of compressed data
 332      * @return the actual number of bytes of compressed data written to the
 333      *         output buffer
 334      */
 335     public int deflate(byte[] b, int off, int len) {
 336         return deflateBytes(b, off, len, NO_FLUSH);

 337     }





 338 
 339     /**
 340      * Compresses the input data and fills specified buffer with compressed
 341      * data. Returns actual number of bytes of compressed data. A return value
 342      * of 0 indicates that {@link needsInput() needsInput} should be called
 343      * in order to determine if more input data is required.
 344      *
 345      * <p>This method uses {@link #NO_FLUSH} as its compression flush mode.
 346      * An invocation of this method of the form {@code deflater.deflate(b)}
 347      * yields the same result as the invocation of
 348      * {@code deflater.deflate(b, 0, b.length, Deflater.NO_FLUSH)}.
 349      *
 350      * @param b the buffer for the compressed data
 351      * @return the actual number of bytes of compressed data written to the
 352      *         output buffer
 353      */
 354     public int deflate(byte[] b) {
 355         return deflate(b, 0, b.length, NO_FLUSH);
 356     }
 357 
 358     /**
 359      * Compresses the input data and fills the specified buffer with compressed
 360      * data. Returns actual number of bytes of data compressed.
 361      *
 362      * <p>Compression flush mode is one of the following three modes:
 363      *
 364      * <ul>
 365      * <li>{@link #NO_FLUSH}: allows the deflater to decide how much data
 366      * to accumulate, before producing output, in order to achieve the best
 367      * compression (should be used in normal use scenario). A return value
 368      * of 0 in this flush mode indicates that {@link #needsInput()} should
 369      * be called in order to determine if more input data is required.
 370      *
 371      * <li>{@link #SYNC_FLUSH}: all pending output in the deflater is flushed,
 372      * to the specified output buffer, so that an inflater that works on
 373      * compressed data can get all input data available so far (In particular
 374      * the {@link #needsInput()} returns {@code true} after this invocation
 375      * if enough output space is provided). Flushing with {@link #SYNC_FLUSH}
 376      * may degrade compression for some compression algorithms and so it
 377      * should be used only when necessary.
 378      *
 379      * <li>{@link #FULL_FLUSH}: all pending output is flushed out as with
 380      * {@link #SYNC_FLUSH}. The compression state is reset so that the inflater
 381      * that works on the compressed output data can restart from this point
 382      * if previous compressed data has been damaged or if random access is
 383      * desired. Using {@link #FULL_FLUSH} too often can seriously degrade
 384      * compression.
 385      * </ul>
 386      *
 387      * <p>In the case of {@link #FULL_FLUSH} or {@link #SYNC_FLUSH}, if
 388      * the return value is {@code len}, the space available in output
 389      * buffer {@code b}, this method should be invoked again with the same
 390      * {@code flush} parameter and more output space.
 391      *
 392      * @param b the buffer for the compressed data
 393      * @param off the start offset of the data
 394      * @param len the maximum number of bytes of compressed data
 395      * @param flush the compression flush mode
 396      * @return the actual number of bytes of compressed data written to
 397      *         the output buffer
 398      *
 399      * @throws IllegalArgumentException if the flush mode is invalid
 400      * @since 1.7
 401      */
 402     public synchronized int deflate(byte[] b, int off, int len, int flush) {
 403         if (b == null) {
 404             throw new NullPointerException();
 405         }
 406         if (off < 0 || len < 0 || off > b.length - len) {
 407             throw new ArrayIndexOutOfBoundsException();
 408         }
 409         if (flush == NO_FLUSH || flush == SYNC_FLUSH ||
 410             flush == FULL_FLUSH)
 411             return deflateBytes(b, off, len, flush);
 412         throw new IllegalArgumentException();
 413     }
 414 
 415     /**
 416      * Returns the ADLER-32 value of the uncompressed data.
 417      * @return the ADLER-32 value of the uncompressed data
 418      */
 419     public synchronized int getAdler() {
 420         ensureOpen();
 421         return getAdler(strm);
 422     }
 423 
 424     /**
 425      * Returns the total number of uncompressed bytes input so far.
 426      *
 427      * <p>Since the number of bytes may be greater than
 428      * Integer.MAX_VALUE, the {@link #getBytesRead()} method is now
 429      * the preferred means of obtaining this information.</p>
 430      *
 431      * @return the total number of uncompressed bytes input so far
 432      */
 433     public int getTotalIn() {
 434         return (int) getBytesRead();
 435     }


 495             buf = null;
 496         }
 497     }
 498 
 499     /**
 500      * Closes the compressor when garbage is collected.
 501      */
 502     protected void finalize() {
 503         end();
 504     }
 505 
 506     private void ensureOpen() {
 507         if (strm == 0)
 508             throw new NullPointerException();
 509     }
 510 
 511     private static native void initIDs();
 512     private native static long init(int level, int strategy, boolean nowrap);
 513     private native static void setDictionary(long strm, byte[] b, int off,
 514                                              int len);
 515     private native int deflateBytes(byte[] b, int off, int len, int flush);
 516     private native static int getAdler(long strm);
 517     private native static long getBytesRead(long strm);
 518     private native static long getBytesWritten(long strm);
 519     private native static void reset(long strm);
 520     private native static void end(long strm);
 521 }