1 7 8 package java.util.zip; 9 10 49 public 50 class Deflater { 51 private long strm; 52 private byte[] buf = new byte[0]; 53 private int off, len; 54 private int level, strategy; 55 private boolean setParams; 56 private boolean finish, finished; 57 58 62 public static final int DEFLATED = 8; 63 64 67 public static final int NO_COMPRESSION = 0; 68 69 72 public static final int BEST_SPEED = 1; 73 74 77 public static final int BEST_COMPRESSION = 9; 78 79 82 public static final int DEFAULT_COMPRESSION = -1; 83 84 89 public static final int FILTERED = 1; 90 91 94 public static final int HUFFMAN_ONLY = 2; 95 96 99 public static final int DEFAULT_STRATEGY = 0; 100 101 static { 102 103 initIDs(); 104 } 105 106 114 public Deflater(int level, boolean nowrap) { 115 this.level = level; 116 this.strategy = DEFAULT_STRATEGY; 117 strm = init(level, DEFAULT_STRATEGY, nowrap); 118 } 119 120 125 public Deflater(int level) { 126 this(level, false); 127 } 128 129 133 public Deflater() { 134 this(DEFAULT_COMPRESSION, false); 135 } 136 137 145 public synchronized void setInput(byte[] b, int off, int len) { 146 if (b== null) { 147 throw new NullPointerException (); 148 } 149 if (off < 0 || len < 0 || off > b.length - len) { 150 throw new ArrayIndexOutOfBoundsException (); 151 } 152 this.buf = b; 153 this.off = off; 154 this.len = len; 155 } 156 157 163 public void setInput(byte[] b) { 164 setInput(b, 0, b.length); 165 } 166 167 179 public synchronized void setDictionary(byte[] b, int off, int len) { 180 if (strm == 0 || b == null) { 181 throw new NullPointerException (); 182 } 183 if (off < 0 || len < 0 || off > b.length - len) { 184 throw new ArrayIndexOutOfBoundsException (); 185 } 186 setDictionary(strm, b, off, len); 187 } 188 189 199 public void setDictionary(byte[] b) { 200 setDictionary(b, 0, b.length); 201 } 202 203 209 public synchronized void setStrategy(int strategy) { 210 switch (strategy) { 211 case DEFAULT_STRATEGY: 212 case FILTERED: 213 case HUFFMAN_ONLY: 214 break; 215 default: 216 throw new IllegalArgumentException (); 217 } 218 if (this.strategy != strategy) { 219 this.strategy = strategy; 220 setParams = true; 221 } 222 } 223 224 229 public synchronized void setLevel(int level) { 230 if ((level < 0 || level > 9) && level != DEFAULT_COMPRESSION) { 231 throw new IllegalArgumentException ("invalid compression level"); 232 } 233 if (this.level != level) { 234 this.level = level; 235 setParams = true; 236 } 237 } 238 239 245 public boolean needsInput() { 246 return len <= 0; 247 } 248 249 253 public synchronized void finish() { 254 finish = true; 255 } 256 257 263 public synchronized boolean finished() { 264 return finished; 265 } 266 267 277 public synchronized int deflate(byte[] b, int off, int len) { 278 if (b == null) { 279 throw new NullPointerException (); 280 } 281 if (off < 0 || len < 0 || off > b.length - len) { 282 throw new ArrayIndexOutOfBoundsException (); 283 } 284 return deflateBytes(b, off, len); 285 } 286 287 295 public int deflate(byte[] b) { 296 return deflate(b, 0, b.length); 297 } 298 299 303 public synchronized int getAdler() { 304 ensureOpen(); 305 return getAdler(strm); 306 } 307 308 317 public int getTotalIn() { 318 return (int) getBytesRead(); 319 } 320 321 326 public synchronized long getBytesRead() { 327 ensureOpen(); 328 return getBytesRead(strm); 329 } 330 331 340 public int getTotalOut() { 341 return (int) getBytesWritten(); 342 } 343 344 349 public synchronized long getBytesWritten() { 350 ensureOpen(); 351 return getBytesWritten(strm); 352 } 353 354 358 public synchronized void reset() { 359 ensureOpen(); 360 reset(strm); 361 finish = false; 362 finished = false; 363 off = len = 0; 364 } 365 366 373 public synchronized void end() { 374 if (strm != 0) { 375 end(strm); 376 strm = 0; 377 buf = null; 378 } 379 } 380 381 384 protected void finalize() { 385 end(); 386 } 387 388 private void ensureOpen() { 389 if (strm == 0) 390 throw new NullPointerException (); 391 } 392 393 private static native void initIDs(); 394 private native static long init(int level, int strategy, boolean nowrap); 395 private native static void setDictionary(long strm, byte[] b, int off, 396 int len); 397 private native int deflateBytes(byte[] b, int off, int len); 398 private native static int getAdler(long strm); 399 private native static long getBytesRead(long strm); 400 private native static long getBytesWritten(long strm); 401 private native static void reset(long strm); 402 private native static void end(long strm); 403 } 404 | Popular Tags |