home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1998 February / VPR9802A.ISO / APP_DEMO / VC / MAIN.BIN / Deflater.java < prev    next >
Text File  |  1997-10-27  |  9KB  |  305 lines

  1. /*
  2.  * @(#)Deflater.java    1.17 97/02/14
  3.  * 
  4.  * Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.1_beta
  20.  * 
  21.  */
  22.  
  23. package java.util.zip;
  24.  
  25. /**
  26.  * This class provides support for general purpose decompression using
  27.  * the popular ZLIB compression library. The ZLIB compression library
  28.  * was initially developed as part of the PNG graphics standard and is
  29.  * not protected by patents. It is fully described in RFCs 1950, 1951,
  30.  * and 1952, which can be found at <a href="ftp://ds.internic.net/rfc">
  31.  * ftp://ds.internic.net/rfc</a> in the files rfc1950.txt (zlib format),
  32.  * rfc1951.txt (deflate format) and rfc1952.txt (gzip format).
  33.  * 
  34.  * @see        Inflater
  35.  * @version     1.17, 02/14/97
  36.  * @author     David Connelly
  37.  */
  38. public
  39. class Deflater {
  40.     private int strm;
  41.     private byte[] buf = new byte[0];
  42.     private int off, len;
  43.     private int level, strategy;
  44.     private boolean setParams;
  45.     private boolean finish, finished;
  46.  
  47.     /**
  48.      * Compression method for the deflate algorithm (the only one currently
  49.      * supported).
  50.      */
  51.     public static final int DEFLATED = 8;
  52.  
  53.     /**
  54.      * Compression level for no compression.
  55.      */
  56.     public static final int NO_COMPRESSION = 0;
  57.  
  58.     /**
  59.      * Compression level for fastest compression.
  60.      */
  61.     public static final int BEST_SPEED = 1;
  62.  
  63.     /**
  64.      * Compression level for best compression.
  65.      */
  66.     public static final int BEST_COMPRESSION = 9;
  67.  
  68.     /**
  69.      * Default compression level.
  70.      */
  71.     public static final int DEFAULT_COMPRESSION = -1;
  72.  
  73.     /**
  74.      * Compression strategy best used for data consisting mostly of small
  75.      * values with a somewhat random distribution. Forces more Huffman coding
  76.      * and less string matching.
  77.      */
  78.     public static final int FILTERED = 1;
  79.  
  80.     /**
  81.      * Compression strategy for Huffman coding only.
  82.      */
  83.     public static final int HUFFMAN_ONLY = 2;
  84.  
  85.     /**
  86.      * Default compression strategy.
  87.      */
  88.     public static final int DEFAULT_STRATEGY = 0;
  89.  
  90.     /*
  91.      * Loads the ZLIB library.
  92.      */
  93.     static {
  94.     System.loadLibrary("zip");
  95.     }
  96.  
  97.     /**
  98.      * Creates a new compressor using the specified compression level.
  99.      * If 'nowrap' is true then the ZLIB header and checksum fields will
  100.      * not be used in order to support the compression format used in
  101.      * both GZIP and PKZIP.
  102.      * @param level the compression level (0-9)
  103.      * @param nowrap if true then use GZIP compatible compression
  104.      */
  105.     public Deflater(int level, boolean nowrap) {
  106.     this.level = level;
  107.     this.strategy = DEFAULT_STRATEGY;
  108.     init(nowrap);
  109.     }
  110.  
  111.     /** 
  112.      * Creates a new compressor using the specified compression level.
  113.      * Compressed data will be generated in ZLIB format.
  114.      * @param level the compression level (0-9)
  115.      */
  116.     public Deflater(int level) {
  117.     this(level, false);
  118.     }
  119.  
  120.     /**
  121.      * Creates a new compressor with the default compression level.
  122.      * Compressed data will be generated in ZLIB format.
  123.      */
  124.     public Deflater() {
  125.     this(DEFAULT_COMPRESSION, false);
  126.     }
  127.  
  128.     /**
  129.      * Sets input data for compression. This should be called whenever
  130.      * needsInput() returns true indicating that more input data is required.
  131.      * @param b the input data bytes
  132.      * @param off the start offset of the data
  133.      * @param len the length of the data
  134.      * @see Deflater#needsInput
  135.      */
  136.     public synchronized void setInput(byte[] b, int off, int len) {
  137.     if (b== null) {
  138.         throw new NullPointerException();
  139.     }
  140.     if (off < 0 || len < 0 || off + len > b.length) {
  141.         throw new ArrayIndexOutOfBoundsException();
  142.     }
  143.     this.buf = b;
  144.     this.off = off;
  145.     this.len = len;
  146.     }
  147.  
  148.     /**
  149.      * Sets input data for compression. This should be called whenever
  150.      * needsInput() returns true indicating that more input data is required.
  151.      * @param b the input data bytes
  152.      * @see Deflater#needsInput
  153.      */
  154.     public void setInput(byte[] b) {
  155.     setInput(b);
  156.     }
  157.  
  158.     /**
  159.      * Sets preset dictionary for compression. A preset dictionary is used
  160.      * when the history buffer can be predetermined. When the data is later
  161.      * uncompressed with Inflater.inflate(), Inflater.getAdler() can be called
  162.      * in order to get the Adler-32 value of the dictionary required for
  163.      * decompression.
  164.      * @param b the dictionary data bytes
  165.      * @param off the start offset of the data
  166.      * @param len the length of the data
  167.      * @see Inflater#inflate
  168.      * @see Inflater#getAdler
  169.      */
  170.     public synchronized native void setDictionary(byte[] b, int off, int len);
  171.  
  172.     /**
  173.      * Sets preset dictionary for compression. A preset dictionary is used
  174.      * when the history buffer can be predetermined. When the data is later
  175.      * uncompressed with Inflater.inflate(), Inflater.getAdler() can be called
  176.      * in order to get the Adler-32 value of the dictionary required for
  177.      * decompression.
  178.      * @param b the dictionary data bytes
  179.      * @see Inflater#inflate
  180.      * @see Inflater#getAdler
  181.      */
  182.     public void setDictionary(byte[] b) {
  183.     setDictionary(b, 0, b.length);
  184.     }
  185.  
  186.     /**
  187.      * Sets the compression strategy to the specified value.
  188.      * @param strategy the new compression strategy
  189.      * @exception IllegalArgumentException if the compression strategy is
  190.      *                           invalid
  191.      */
  192.     public synchronized void setStrategy(int strategy) {
  193.     switch (strategy) {
  194.       case DEFAULT_STRATEGY:
  195.       case FILTERED:
  196.       case HUFFMAN_ONLY:
  197.         break;
  198.       default:
  199.         throw new IllegalArgumentException();
  200.     }
  201.     if (this.strategy != strategy) {
  202.         this.strategy = strategy;
  203.         setParams = true;
  204.     }
  205.     }
  206.  
  207.     /**
  208.      * Sets the current compression level to the specified value.
  209.      * @param level the new compression level (0-9)
  210.      * @exception IllegalArgumentException if the compression level is invalid
  211.      */
  212.     public synchronized void setLevel(int Level) {
  213.     if ((level < 0 || level > 9) && level != DEFAULT_COMPRESSION) {
  214.         throw new IllegalArgumentException("invalid compression level");
  215.     }
  216.     if (this.level != level) {
  217.         this.level = level;
  218.         setParams = true;
  219.     }
  220.     }
  221.  
  222.     /**
  223.      * Returns true if the input data buffer is empty and setInput()
  224.      * should be called in order to provide more input.
  225.      */
  226.     public boolean needsInput() {
  227.     return len <= 0;
  228.     }
  229.  
  230.     /**
  231.      * When called, indicates that compression should end with the current
  232.      * contents of the input buffer.
  233.      */
  234.     public synchronized void finish() {
  235.     finish = true;
  236.     }
  237.  
  238.     /**
  239.      * Returns true if the end of the compressed data output stream has
  240.      * been reached.
  241.      */
  242.     public synchronized boolean finished() {
  243.     return finished;
  244.     }
  245.  
  246.     /**
  247.      * Fills specified buffer with compressed data. Returns actual number
  248.      * of bytes of compressed data. A return value of 0 indicates that
  249.      * needsInput() should be called in order to determine if more input
  250.      * data is required.
  251.      * @param b the buffer for the compressed data
  252.      * @param off the start offset of the data
  253.      * @param len the maximum number of bytes of compressed data
  254.      * @return the actual number of bytes of compressed data
  255.      */
  256.     public synchronized native int deflate(byte[] b, int off, int len);
  257.  
  258.     /**
  259.      * Fills specified buffer with compressed data. Returns actual number
  260.      * of bytes of compressed data. A return value of 0 indicates that
  261.      * needsInput() should be called in order to determine if more input
  262.      * data is required.
  263.      * @param b the buffer for the compressed data
  264.      * @return the actual number of bytes of compressed data
  265.      */
  266.     public int deflate(byte[] b) {
  267.     return deflate(b, 0, b.length);
  268.     }
  269.  
  270.     /**
  271.      * Returns the ADLER-32 value of the uncompressed data.
  272.      */
  273.     public synchronized native int getAdler();
  274.  
  275.     /**
  276.      * Returns the total number of bytes input so far.
  277.      */
  278.     public synchronized native int getTotalIn();
  279.  
  280.     /**
  281.      * Returns the total number of bytes output so far.
  282.      */
  283.     public synchronized native int getTotalOut();
  284.  
  285.     /**
  286.      * Resets deflater so that a new set of input data can be processed.
  287.      * Keeps current compression level and strategy settings.
  288.      */
  289.     public synchronized native void reset();
  290.  
  291.     /**
  292.      * Discards unprocessed input and frees internal data.
  293.      */
  294.     public synchronized native void end();
  295.  
  296.     /**
  297.      * Frees the compressor when garbage is collected.
  298.      */
  299.     protected void finalize() {
  300.     end();
  301.     }
  302.  
  303.     private native void init(boolean nowrap);
  304. }
  305.