1 /* Deflater.java - Compress a data stream
2    Copyright (C) 1999, 2000 Free Software Foundation, Inc.
3 
4 This file is part of GNU Classpath.
5 
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10 
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING.  If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
20 
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library.  Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25 
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module.  An independent module is a module which is not derived from
33 or based on this library.  If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so.  If you do not wish to do so, delete this
36 exception statement from your version. */
37 
38 package java.util.zip;
39 
40 import gnu.gcj.RawData;
41 
42 /**
43  * @author Tom Tromey
44  * @date May 17, 1999
45  */
46 
47 /* Written using on-line Java Platform 1.2 API Specification
48  * and JCL book.
49  * Believed complete and correct.
50  */
51 public class Deflater
52 {
53   /**
54    * The best and slowest compression level.  This tries to find very
55    * long and distant string repetitions.
56    */
57   public static final int BEST_COMPRESSION = 9;
58   /**
59    * The worst but fastest compression level.
60    */
61   public static final int BEST_SPEED = 1;
62   /**
63    * The default compression level.
64    */
65   public static final int DEFAULT_COMPRESSION = -1;
66   /**
67    * This level won't compress at all but output uncompressed blocks.
68    */
69   public static final int NO_COMPRESSION = 0;
70 
71   /**
72    * The default strategy.
73    */
74   public static final int DEFAULT_STRATEGY = 0;
75   /**
76    * This strategy will only allow longer string repetitions.  It is
77    * useful for random data with a small character set.
78    */
79   public static final int FILTERED = 1;
80 
81   /**
82    * This strategy will not look for string repetitions at all.  It
83    * only encodes with Huffman trees (which means, that more common
84    * characters get a smaller encoding.
85    */
86   public static final int HUFFMAN_ONLY = 2;
87 
88   /**
89    * The compression method.  This is the only method supported so far.
90    * There is no need to use this constant at all.
91    */
92   public static final int DEFLATED = 8;
93 
deflate(byte[] buf)94   public int deflate (byte[] buf)
95   {
96     return deflate (buf, 0, buf.length);
97   }
98 
deflate(byte[] buf, int off, int len)99   public native int deflate (byte[] buf, int off, int len);
init(int level, boolean noHeader)100   private native void init (int level, boolean noHeader);
update()101   private native void update ();
102 
Deflater()103   public Deflater ()
104   {
105     this (DEFAULT_COMPRESSION, false);
106   }
107 
Deflater(int lvl)108   public Deflater (int lvl)
109   {
110     this (lvl, false);
111   }
112 
Deflater(int lvl, boolean noHeader)113   public Deflater (int lvl, boolean noHeader)
114   {
115     this.strategy = DEFAULT_STRATEGY;
116     init (lvl, noHeader);
117     setLevel (lvl);
118   }
119 
end()120   public native void end ();
121 
finalize()122   protected void finalize ()
123   {
124     end ();
125   }
126 
finish()127   public native void finish ();
128 
finished()129   public synchronized boolean finished ()
130   {
131     return is_finished;
132   }
133 
getAdler()134   public native int getAdler ();
getTotalIn()135   public native int getTotalIn ();
getTotalOut()136   public native int getTotalOut ();
needsInput()137   public native boolean needsInput ();
reset()138   public native void reset ();
139 
setDictionary(byte[] buf)140   public void setDictionary (byte[] buf)
141   {
142     setDictionary (buf, 0, buf.length);
143   }
144 
setDictionary(byte[] buf, int off, int len)145   public native void setDictionary (byte[] buf, int off, int len);
146 
setInput(byte[] buf)147   public void setInput (byte[] buf)
148   {
149     setInput (buf, 0, buf.length);
150   }
151 
setInput(byte[] buf, int off, int len)152   public native void setInput (byte[] buf, int off, int len);
153 
setLevel(int lvl)154   public synchronized void setLevel (int lvl)
155   {
156     if (lvl != -1 && (lvl < 0 || lvl > 9))
157       throw new IllegalArgumentException ();
158     level = (lvl == -1) ? 6 : lvl;
159     update ();
160   }
161 
setStrategy(int stgy)162   public synchronized void setStrategy (int stgy)
163   {
164     if (stgy != DEFAULT_STRATEGY && stgy != FILTERED
165 	&& stgy != HUFFMAN_ONLY)
166       throw new IllegalArgumentException ();
167     strategy = stgy;
168     update ();
169   }
170 
171   // Compression level.
172   private int level;
173 
174   // Compression strategy.
175   private int strategy;
176 
177   // The zlib stream.
178   private RawData zstream;
179 
180   // True if finished.
181   private boolean is_finished;
182 
183   // `Flush' flag to pass to next call to deflate.
184   private int flush_flag;
185 }
186