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 */ 73 public 74 class Deflater { 75 76 private final ZStreamRef zsRef; 77 private byte[] buf = new byte[0]; 78 private int off, len; 79 private int level, strategy; 80 private boolean setParams; 81 private boolean finish, finished; 82 83 /** 84 * Compression method for the deflate algorithm (the only one currently 85 * supported). 86 */ 87 public static final int DEFLATED = 8; 88 89 /** 90 * Compression level for no compression. 91 */ 92 public static final int NO_COMPRESSION = 0; 93 94 /** 95 * Compression level for fastest compression. 96 */ 97 public static final int BEST_SPEED = 1; 98 99 /** 100 * Compression level for best compression. 101 */ 406 * @param b the buffer for the compressed data 407 * @param off the start offset of the data 408 * @param len the maximum number of bytes of compressed data 409 * @param flush the compression flush mode 410 * @return the actual number of bytes of compressed data written to 411 * the output buffer 412 * 413 * @throws IllegalArgumentException if the flush mode is invalid 414 * @since 1.7 415 */ 416 public int deflate(byte[] b, int off, int len, int flush) { 417 if (b == null) { 418 throw new NullPointerException(); 419 } 420 if (off < 0 || len < 0 || off > b.length - len) { 421 throw new ArrayIndexOutOfBoundsException(); 422 } 423 synchronized (zsRef) { 424 ensureOpen(); 425 if (flush == NO_FLUSH || flush == SYNC_FLUSH || 426 flush == FULL_FLUSH) 427 return deflateBytes(zsRef.address(), b, off, len, flush); 428 throw new IllegalArgumentException(); 429 } 430 } 431 432 /** 433 * Returns the ADLER-32 value of the uncompressed data. 434 * @return the ADLER-32 value of the uncompressed data 435 */ 436 public int getAdler() { 437 synchronized (zsRef) { 438 ensureOpen(); 439 return getAdler(zsRef.address()); 440 } 441 } 442 443 /** 444 * Returns the total number of uncompressed bytes input so far. 445 * 446 * <p>Since the number of bytes may be greater than 447 * Integer.MAX_VALUE, the {@link #getBytesRead()} method is now 448 * the preferred means of obtaining this information.</p> 449 * 450 * @return the total number of uncompressed bytes input so far 451 */ 452 public int getTotalIn() { 453 return (int) getBytesRead(); 454 } 455 456 /** 457 * Returns the total number of uncompressed bytes input so far.</p> 458 * 459 * @return the total (non-negative) number of uncompressed bytes input so far 460 * @since 1.5 461 */ 462 public long getBytesRead() { 463 synchronized (zsRef) { 464 ensureOpen(); 465 return getBytesRead(zsRef.address()); 466 } 467 } 468 469 /** 470 * Returns the total number of compressed bytes output so far. 471 * 472 * <p>Since the number of bytes may be greater than 473 * Integer.MAX_VALUE, the {@link #getBytesWritten()} method is now 474 * the preferred means of obtaining this information.</p> 475 * 476 * @return the total number of compressed bytes output so far 477 */ 478 public int getTotalOut() { 479 return (int) getBytesWritten(); 480 } 481 482 /** 483 * Returns the total number of compressed bytes output so far.</p> 484 * 485 * @return the total (non-negative) number of compressed bytes output so far 486 * @since 1.5 487 */ 488 public long getBytesWritten() { 489 synchronized (zsRef) { 490 ensureOpen(); 491 return getBytesWritten(zsRef.address()); 492 } 493 } 494 495 /** 496 * Resets deflater so that a new set of input data can be processed. 497 * Keeps current compression level and strategy settings. 498 */ 499 public void reset() { 500 synchronized (zsRef) { 501 ensureOpen(); 502 reset(zsRef.address()); 503 finish = false; 504 finished = false; 505 off = len = 0; 506 } 507 } 508 509 /** 510 * Closes the compressor and discards any unprocessed input. 511 * This method should be called when the compressor is no longer 512 * being used, but will also be called automatically by the 513 * finalize() method. Once this method is called, the behavior 514 * of the Deflater object is undefined. 515 */ 516 public void end() { 517 synchronized (zsRef) { 518 long addr = zsRef.address(); 519 zsRef.clear(); 520 if (addr != 0) { 521 end(addr); 522 buf = null; 523 } 524 } 525 } 526 527 /** 528 * Closes the compressor when garbage is collected. 529 */ 530 protected void finalize() { 531 end(); 532 } 533 534 private void ensureOpen() { 535 assert Thread.holdsLock(zsRef); 536 if (zsRef.address() == 0) 537 throw new NullPointerException("Deflater has been closed"); 538 } 539 540 private static native void initIDs(); 541 private native static long init(int level, int strategy, boolean nowrap); 542 private native static void setDictionary(long addr, byte[] b, int off, int len); 543 private native int deflateBytes(long addr, byte[] b, int off, int len, 544 int flush); 545 private native static int getAdler(long addr); 546 private native static long getBytesRead(long addr); 547 private native static long getBytesWritten(long addr); 548 private native static void reset(long addr); 549 private native static void end(long addr); 550 } | 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 */ 73 public 74 class Deflater { 75 76 private final ZStreamRef zsRef; 77 private byte[] buf = new byte[0]; 78 private int off, len; 79 private int level, strategy; 80 private boolean setParams; 81 private boolean finish, finished; 82 private long bytesRead; 83 private long bytesWritten; 84 85 /** 86 * Compression method for the deflate algorithm (the only one currently 87 * supported). 88 */ 89 public static final int DEFLATED = 8; 90 91 /** 92 * Compression level for no compression. 93 */ 94 public static final int NO_COMPRESSION = 0; 95 96 /** 97 * Compression level for fastest compression. 98 */ 99 public static final int BEST_SPEED = 1; 100 101 /** 102 * Compression level for best compression. 103 */ 408 * @param b the buffer for the compressed data 409 * @param off the start offset of the data 410 * @param len the maximum number of bytes of compressed data 411 * @param flush the compression flush mode 412 * @return the actual number of bytes of compressed data written to 413 * the output buffer 414 * 415 * @throws IllegalArgumentException if the flush mode is invalid 416 * @since 1.7 417 */ 418 public int deflate(byte[] b, int off, int len, int flush) { 419 if (b == null) { 420 throw new NullPointerException(); 421 } 422 if (off < 0 || len < 0 || off > b.length - len) { 423 throw new ArrayIndexOutOfBoundsException(); 424 } 425 synchronized (zsRef) { 426 ensureOpen(); 427 if (flush == NO_FLUSH || flush == SYNC_FLUSH || 428 flush == FULL_FLUSH) { 429 int thisLen = this.len; 430 int n = deflateBytes(zsRef.address(), b, off, len, flush); 431 bytesWritten += n; 432 bytesRead += (thisLen - this.len); 433 return n; 434 } 435 throw new IllegalArgumentException(); 436 } 437 } 438 439 /** 440 * Returns the ADLER-32 value of the uncompressed data. 441 * @return the ADLER-32 value of the uncompressed data 442 */ 443 public int getAdler() { 444 synchronized (zsRef) { 445 ensureOpen(); 446 return getAdler(zsRef.address()); 447 } 448 } 449 450 /** 451 * Returns the total number of uncompressed bytes input so far. 452 * 453 * <p>Since the number of bytes may be greater than 454 * Integer.MAX_VALUE, the {@link #getBytesRead()} method is now 455 * the preferred means of obtaining this information.</p> 456 * 457 * @return the total number of uncompressed bytes input so far 458 */ 459 public int getTotalIn() { 460 return (int) getBytesRead(); 461 } 462 463 /** 464 * Returns the total number of uncompressed bytes input so far.</p> 465 * 466 * @return the total (non-negative) number of uncompressed bytes input so far 467 * @since 1.5 468 */ 469 public long getBytesRead() { 470 synchronized (zsRef) { 471 ensureOpen(); 472 return bytesRead; 473 } 474 } 475 476 /** 477 * Returns the total number of compressed bytes output so far. 478 * 479 * <p>Since the number of bytes may be greater than 480 * Integer.MAX_VALUE, the {@link #getBytesWritten()} method is now 481 * the preferred means of obtaining this information.</p> 482 * 483 * @return the total number of compressed bytes output so far 484 */ 485 public int getTotalOut() { 486 return (int) getBytesWritten(); 487 } 488 489 /** 490 * Returns the total number of compressed bytes output so far.</p> 491 * 492 * @return the total (non-negative) number of compressed bytes output so far 493 * @since 1.5 494 */ 495 public long getBytesWritten() { 496 synchronized (zsRef) { 497 ensureOpen(); 498 return bytesWritten; 499 } 500 } 501 502 /** 503 * Resets deflater so that a new set of input data can be processed. 504 * Keeps current compression level and strategy settings. 505 */ 506 public void reset() { 507 synchronized (zsRef) { 508 ensureOpen(); 509 reset(zsRef.address()); 510 finish = false; 511 finished = false; 512 off = len = 0; 513 bytesRead = bytesWritten = 0; 514 } 515 } 516 517 /** 518 * Closes the compressor and discards any unprocessed input. 519 * This method should be called when the compressor is no longer 520 * being used, but will also be called automatically by the 521 * finalize() method. Once this method is called, the behavior 522 * of the Deflater object is undefined. 523 */ 524 public void end() { 525 synchronized (zsRef) { 526 long addr = zsRef.address(); 527 zsRef.clear(); 528 if (addr != 0) { 529 end(addr); 530 buf = null; 531 } 532 } 533 } 534 535 /** 536 * Closes the compressor when garbage is collected. 537 */ 538 protected void finalize() { 539 end(); 540 } 541 542 private void ensureOpen() { 543 assert Thread.holdsLock(zsRef); 544 if (zsRef.address() == 0) 545 throw new NullPointerException("Deflater has been closed"); 546 } 547 548 private static native void initIDs(); 549 private native static long init(int level, int strategy, boolean nowrap); 550 private native static void setDictionary(long addr, byte[] b, int off, int len); 551 private native int deflateBytes(long addr, byte[] b, int off, int len, 552 int flush); 553 private native static int getAdler(long addr); 554 private native static void reset(long addr); 555 private native static void end(long addr); 556 } |