< prev index next >

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

Print this page




  50  *     compresser.finish();
  51  *     int compressedDataLength = compresser.deflate(output);
  52  *     compresser.end();
  53  *
  54  *     // Decompress the bytes
  55  *     Inflater decompresser = new Inflater();
  56  *     decompresser.setInput(output, 0, compressedDataLength);
  57  *     byte[] result = new byte[100];
  58  *     int resultLength = decompresser.inflate(result);
  59  *     decompresser.end();
  60  *
  61  *     // Decode the bytes into a String
  62  *     String outputString = new String(result, 0, resultLength, "UTF-8");
  63  * } catch(java.io.UnsupportedEncodingException ex) {
  64  *     // handle
  65  * } catch (java.util.zip.DataFormatException ex) {
  66  *     // handle
  67  * }
  68  * </pre></blockquote>
  69  *














  70  * @see         Inflater
  71  * @author      David Connelly
  72  * @since 1.1
  73  */
  74 public
  75 class Deflater {
  76 
  77     private final ZStreamRef zsRef;
  78     private byte[] buf = new byte[0];
  79     private int off, len;
  80     private int level, strategy;
  81     private boolean setParams;
  82     private boolean finish, finished;
  83     private long bytesRead;
  84     private long bytesWritten;
  85 
  86     /**
  87      * Compression method for the deflate algorithm (the only one currently
  88      * supported).
  89      */


 152      * @since 1.7
 153      */
 154     public static final int FULL_FLUSH = 3;
 155 
 156     static {
 157         ZipUtils.loadLibrary();
 158         initIDs();
 159     }
 160 
 161     /**
 162      * Creates a new compressor using the specified compression level.
 163      * If 'nowrap' is true then the ZLIB header and checksum fields will
 164      * not be used in order to support the compression format used in
 165      * both GZIP and PKZIP.
 166      * @param level the compression level (0-9)
 167      * @param nowrap if true then use GZIP compatible compression
 168      */
 169     public Deflater(int level, boolean nowrap) {
 170         this.level = level;
 171         this.strategy = DEFAULT_STRATEGY;
 172         this.zsRef = new ZStreamRef(init(level, DEFAULT_STRATEGY, nowrap));


 173     }
 174 
 175     /**
 176      * Creates a new compressor using the specified compression level.
 177      * Compressed data will be generated in ZLIB format.
 178      * @param level the compression level (0-9)
 179      */
 180     public Deflater(int level) {
 181         this(level, false);
 182     }
 183 
 184     /**
 185      * Creates a new compressor with the default compression level.
 186      * Compressed data will be generated in ZLIB format.
 187      */
 188     public Deflater() {
 189         this(DEFAULT_COMPRESSION, false);
 190     }
 191 
 192     /**


 517         }
 518     }
 519 
 520     /**
 521      * Resets deflater so that a new set of input data can be processed.
 522      * Keeps current compression level and strategy settings.
 523      */
 524     public void reset() {
 525         synchronized (zsRef) {
 526             ensureOpen();
 527             reset(zsRef.address());
 528             finish = false;
 529             finished = false;
 530             off = len = 0;
 531             bytesRead = bytesWritten = 0;
 532         }
 533     }
 534 
 535     /**
 536      * Closes the compressor and discards any unprocessed input.

 537      * This method should be called when the compressor is no longer
 538      * being used, but will also be called automatically by the
 539      * finalize() method. Once this method is called, the behavior
 540      * of the Deflater object is undefined.
 541      */
 542     public void end() {
 543         synchronized (zsRef) {
 544             long addr = zsRef.address();
 545             zsRef.clear();
 546             if (addr != 0) {
 547                 end(addr);
 548                 buf = null;
 549             }
 550         }
 551     }
 552 
 553     /**
 554      * Closes the compressor when garbage is collected.
 555      *
 556      * @deprecated The {@code finalize} method has been deprecated.
 557      *     Subclasses that override {@code finalize} in order to perform cleanup
 558      *     should be modified to use alternative cleanup mechanisms and
 559      *     to remove the overriding {@code finalize} method.
 560      *     When overriding the {@code finalize} method, its implementation must explicitly
 561      *     ensure that {@code super.finalize()} is invoked as described in {@link Object#finalize}.
 562      *     See the specification for {@link Object#finalize()} for further
 563      *     information about migration options.
 564      */
 565     @Deprecated(since="9")
 566     protected void finalize() {
 567         end();
 568     }
 569 
 570     private void ensureOpen() {
 571         assert Thread.holdsLock(zsRef);
 572         if (zsRef.address() == 0)
 573             throw new NullPointerException("Deflater has been closed");
 574     }
 575 
 576     private static native void initIDs();
 577     private static native long init(int level, int strategy, boolean nowrap);
 578     private static native void setDictionary(long addr, byte[] b, int off, int len);
 579     private native int deflateBytes(long addr, byte[] b, int off, int len,
 580                                     int flush);
 581     private static native int getAdler(long addr);
 582     private static native void reset(long addr);
 583     private static native void end(long addr);
 584 }


  50  *     compresser.finish();
  51  *     int compressedDataLength = compresser.deflate(output);
  52  *     compresser.end();
  53  *
  54  *     // Decompress the bytes
  55  *     Inflater decompresser = new Inflater();
  56  *     decompresser.setInput(output, 0, compressedDataLength);
  57  *     byte[] result = new byte[100];
  58  *     int resultLength = decompresser.inflate(result);
  59  *     decompresser.end();
  60  *
  61  *     // Decode the bytes into a String
  62  *     String outputString = new String(result, 0, resultLength, "UTF-8");
  63  * } catch(java.io.UnsupportedEncodingException ex) {
  64  *     // handle
  65  * } catch (java.util.zip.DataFormatException ex) {
  66  *     // handle
  67  * }
  68  * </pre></blockquote>
  69  *
  70  * <p>
  71  * @apiNote
  72  * In earlier versions the {@link Object#finalize} method was overridden and
  73  * specified to call the {@code end} method to close the {@code deflater} and
  74  * release the resource when the instance becomes unreachable.
  75  * The {@code finalize} method is no longer defined. The recommended cleanup
  76  * for compressor is to explicitly call {@code end} method when it is no
  77  * longer in use.
  78  *
  79  * @implNote
  80  * The resource of the compressor will be released when the instance becomes
  81  * phantom-reachable, if the {@code end} is not invoked explicitly.
  82  * <p>
  83  *
  84  * @see         Inflater
  85  * @author      David Connelly
  86  * @since 1.1
  87  */
  88 public
  89 class Deflater {
  90 
  91     private final ZStreamRef zsRef;
  92     private byte[] buf = new byte[0];
  93     private int off, len;
  94     private int level, strategy;
  95     private boolean setParams;
  96     private boolean finish, finished;
  97     private long bytesRead;
  98     private long bytesWritten;
  99 
 100     /**
 101      * Compression method for the deflate algorithm (the only one currently
 102      * supported).
 103      */


 166      * @since 1.7
 167      */
 168     public static final int FULL_FLUSH = 3;
 169 
 170     static {
 171         ZipUtils.loadLibrary();
 172         initIDs();
 173     }
 174 
 175     /**
 176      * Creates a new compressor using the specified compression level.
 177      * If 'nowrap' is true then the ZLIB header and checksum fields will
 178      * not be used in order to support the compression format used in
 179      * both GZIP and PKZIP.
 180      * @param level the compression level (0-9)
 181      * @param nowrap if true then use GZIP compatible compression
 182      */
 183     public Deflater(int level, boolean nowrap) {
 184         this.level = level;
 185         this.strategy = DEFAULT_STRATEGY;
 186         this.zsRef = new ZStreamRef(this,
 187                 () -> init(level, DEFAULT_STRATEGY, nowrap),
 188                 Deflater::end);
 189     }
 190 
 191     /**
 192      * Creates a new compressor using the specified compression level.
 193      * Compressed data will be generated in ZLIB format.
 194      * @param level the compression level (0-9)
 195      */
 196     public Deflater(int level) {
 197         this(level, false);
 198     }
 199 
 200     /**
 201      * Creates a new compressor with the default compression level.
 202      * Compressed data will be generated in ZLIB format.
 203      */
 204     public Deflater() {
 205         this(DEFAULT_COMPRESSION, false);
 206     }
 207 
 208     /**


 533         }
 534     }
 535 
 536     /**
 537      * Resets deflater so that a new set of input data can be processed.
 538      * Keeps current compression level and strategy settings.
 539      */
 540     public void reset() {
 541         synchronized (zsRef) {
 542             ensureOpen();
 543             reset(zsRef.address());
 544             finish = false;
 545             finished = false;
 546             off = len = 0;
 547             bytesRead = bytesWritten = 0;
 548         }
 549     }
 550 
 551     /**
 552      * Closes the compressor and discards any unprocessed input.
 553      *
 554      * This method should be called when the compressor is no longer
 555      * being used. Once this method is called, the behavior of the
 556      * Deflater object is undefined.

 557      */
 558     public void end() {
 559         synchronized (zsRef) {
 560             zsRef.clean();



 561             buf = null;
 562         }


















 563     }
 564 
 565     private void ensureOpen() {
 566         assert Thread.holdsLock(zsRef);
 567         if (zsRef.address() == 0)
 568             throw new NullPointerException("Deflater has been closed");
 569     }
 570 
 571     private static native void initIDs();
 572     private static native long init(int level, int strategy, boolean nowrap);
 573     private static native void setDictionary(long addr, byte[] b, int off, int len);
 574     private native int deflateBytes(long addr, byte[] b, int off, int len,
 575                                     int flush);
 576     private static native int getAdler(long addr);
 577     private static native void reset(long addr);
 578     private static native void end(long addr);
 579 }
< prev index next >