home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / util / zip / Deflater.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  10.2 KB  |  356 lines

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