1 /* 2 * Copyright (c) 1996, 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package java.util.zip; 27 28 /** 29 * This class provides support for general purpose compression using the 30 * popular ZLIB compression library. The ZLIB compression library was 31 * initially developed as part of the PNG graphics standard and is not 32 * protected by patents. It is fully described in the specifications at 33 * the <a href="package-summary.html#package.description">java.util.zip 34 * package description</a>. 35 * 36 * <p>The following code fragment demonstrates a trivial compression 37 * and decompression of a string using {@code Deflater} and 38 * {@code Inflater}. 39 * 40 * <blockquote><pre> 41 * try { 42 * // Encode a String into bytes 43 * String inputString = "blahblahblah"; 44 * byte[] input = inputString.getBytes("UTF-8"); 45 * 46 * // Compress the bytes 47 * byte[] output = new byte[100]; 48 * Deflater compresser = new Deflater(); 49 * compresser.setInput(input); 50 * compresser.finish(); 51 * int compressedDataLength = compresser.deflate(output); 52 * compresser.end(); 53 * 54 * // Decompress the bytes 55 * Inflater decompresser = new Inflater(); 56 * decompresser.setInput(output, 0, compressedDataLength); 57 * byte[] result = new byte[100]; 58 * int resultLength = decompresser.inflate(result); 59 * decompresser.end(); 60 * 61 * // Decode the bytes into a String 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 * @since 1.1 73 */ 74 public 75 class Deflater { 76 77 private final ZStreamRef zsRef; 78 private byte[] buf = new byte[0]; 79 private int off, len; 80 private int level, strategy; 81 private boolean setParams; 82 private boolean finish, finished; 83 private long bytesRead; 84 private long bytesWritten; 85 86 /** 87 * Compression method for the deflate algorithm (the only one currently 88 * supported). 89 */ 90 public static final int DEFLATED = 8; 91 92 /** 93 * Compression level for no compression. 94 */ 95 public static final int NO_COMPRESSION = 0; 96 97 /** 98 * Compression level for fastest compression. 99 */ 100 public static final int BEST_SPEED = 1; 101 102 /** 103 * Compression level for best compression. 104 */ 105 public static final int BEST_COMPRESSION = 9; 106 107 /** 108 * Default compression level. 109 */ 110 public static final int DEFAULT_COMPRESSION = -1; 111 112 /** 113 * Compression strategy best used for data consisting mostly of small 114 * values with a somewhat random distribution. Forces more Huffman coding 115 * and less string matching. 116 */ 117 public static final int FILTERED = 1; 118 119 /** 120 * Compression strategy for Huffman coding only. 121 */ 122 public static final int HUFFMAN_ONLY = 2; 123 124 /** 125 * Default compression strategy. 126 */ 127 public static final int DEFAULT_STRATEGY = 0; 128 129 /** 130 * Compression flush mode used to achieve best compression result. 131 * 132 * @see Deflater#deflate(byte[], int, int, int) 133 * @since 1.7 134 */ 135 public static final int NO_FLUSH = 0; 136 137 /** 138 * Compression flush mode used to flush out all pending output; may 139 * degrade compression for some compression algorithms. 140 * 141 * @see Deflater#deflate(byte[], int, int, int) 142 * @since 1.7 143 */ 144 public static final int SYNC_FLUSH = 2; 145 146 /** 147 * Compression flush mode used to flush out all pending output and 148 * reset the deflater. Using this mode too often can seriously degrade 149 * compression. 150 * 151 * @see Deflater#deflate(byte[], int, int, int) 152 * @since 1.7 153 */ 154 public static final int FULL_FLUSH = 3; 155 156 static { 157 ZipUtils.loadLibrary(); 158 initIDs(); 159 } 160 161 /** 162 * Creates a new compressor using the specified compression level. 163 * If 'nowrap' is true then the ZLIB header and checksum fields will 164 * not be used in order to support the compression format used in 165 * both GZIP and PKZIP. 166 * @param level the compression level (0-9) 167 * @param nowrap if true then use GZIP compatible compression 168 */ 169 public Deflater(int level, boolean nowrap) { 170 this.level = level; 171 this.strategy = DEFAULT_STRATEGY; 172 this.zsRef = new ZStreamRef(this, 173 () -> init(level, DEFAULT_STRATEGY, nowrap), 174 Deflater::end); 175 } 176 177 /** 178 * Creates a new compressor using the specified compression level. 179 * Compressed data will be generated in ZLIB format. 180 * @param level the compression level (0-9) 181 */ 182 public Deflater(int level) { 183 this(level, false); 184 } 185 186 /** 187 * Creates a new compressor with the default compression level. 188 * Compressed data will be generated in ZLIB format. 189 */ 190 public Deflater() { 191 this(DEFAULT_COMPRESSION, false); 192 } 193 194 /** 195 * Sets input data for compression. This should be called whenever 196 * needsInput() returns true indicating that more input data is required. 197 * @param b the input data bytes 198 * @param off the start offset of the data 199 * @param len the length of the data 200 * @see Deflater#needsInput 201 */ 202 public void setInput(byte[] b, int off, int len) { 203 if (b== null) { 204 throw new NullPointerException(); 205 } 206 if (off < 0 || len < 0 || off > b.length - len) { 207 throw new ArrayIndexOutOfBoundsException(); 208 } 209 synchronized (zsRef) { 210 this.buf = b; 211 this.off = off; 212 this.len = len; 213 } 214 } 215 216 /** 217 * Sets input data for compression. This should be called whenever 218 * needsInput() returns true indicating that more input data is required. 219 * @param b the input data bytes 220 * @see Deflater#needsInput 221 */ 222 public void setInput(byte[] b) { 223 setInput(b, 0, b.length); 224 } 225 226 /** 227 * Sets preset dictionary for compression. A preset dictionary is used 228 * when the history buffer can be predetermined. When the data is later 229 * uncompressed with Inflater.inflate(), Inflater.getAdler() can be called 230 * in order to get the Adler-32 value of the dictionary required for 231 * decompression. 232 * @param b the dictionary data bytes 233 * @param off the start offset of the data 234 * @param len the length of the data 235 * @see Inflater#inflate 236 * @see Inflater#getAdler 237 */ 238 public void setDictionary(byte[] b, int off, int len) { 239 if (b == null) { 240 throw new NullPointerException(); 241 } 242 if (off < 0 || len < 0 || off > b.length - len) { 243 throw new ArrayIndexOutOfBoundsException(); 244 } 245 synchronized (zsRef) { 246 ensureOpen(); 247 setDictionary(zsRef.address(), b, off, len); 248 } 249 } 250 251 /** 252 * Sets preset dictionary for compression. A preset dictionary is used 253 * when the history buffer can be predetermined. When the data is later 254 * uncompressed with Inflater.inflate(), Inflater.getAdler() can be called 255 * in order to get the Adler-32 value of the dictionary required for 256 * decompression. 257 * @param b the dictionary data bytes 258 * @see Inflater#inflate 259 * @see Inflater#getAdler 260 */ 261 public void setDictionary(byte[] b) { 262 setDictionary(b, 0, b.length); 263 } 264 265 /** 266 * Sets the compression strategy to the specified value. 267 * 268 * <p> If the compression strategy is changed, the next invocation 269 * of {@code deflate} will compress the input available so far with 270 * the old strategy (and may be flushed); the new strategy will take 271 * effect only after that invocation. 272 * 273 * @param strategy the new compression strategy 274 * @exception IllegalArgumentException if the compression strategy is 275 * invalid 276 */ 277 public void setStrategy(int strategy) { 278 switch (strategy) { 279 case DEFAULT_STRATEGY: 280 case FILTERED: 281 case HUFFMAN_ONLY: 282 break; 283 default: 284 throw new IllegalArgumentException(); 285 } 286 synchronized (zsRef) { 287 if (this.strategy != strategy) { 288 this.strategy = strategy; 289 setParams = true; 290 } 291 } 292 } 293 294 /** 295 * Sets the compression level to the specified value. 296 * 297 * <p> If the compression level is changed, the next invocation 298 * of {@code deflate} will compress the input available so far 299 * with the old level (and may be flushed); the new level will 300 * take effect only after that invocation. 301 * 302 * @param level the new compression level (0-9) 303 * @exception IllegalArgumentException if the compression level is invalid 304 */ 305 public void setLevel(int level) { 306 if ((level < 0 || level > 9) && level != DEFAULT_COMPRESSION) { 307 throw new IllegalArgumentException("invalid compression level"); 308 } 309 synchronized (zsRef) { 310 if (this.level != level) { 311 this.level = level; 312 setParams = true; 313 } 314 } 315 } 316 317 /** 318 * Returns true if the input data buffer is empty and setInput() 319 * should be called in order to provide more input. 320 * @return true if the input data buffer is empty and setInput() 321 * should be called in order to provide more input 322 */ 323 public boolean needsInput() { 324 synchronized (zsRef) { 325 return len <= 0; 326 } 327 } 328 329 /** 330 * When called, indicates that compression should end with the current 331 * contents of the input buffer. 332 */ 333 public void finish() { 334 synchronized (zsRef) { 335 finish = true; 336 } 337 } 338 339 /** 340 * Returns true if the end of the compressed data output stream has 341 * been reached. 342 * @return true if the end of the compressed data output stream has 343 * been reached 344 */ 345 public boolean finished() { 346 synchronized (zsRef) { 347 return finished; 348 } 349 } 350 351 /** 352 * Compresses the input data and fills specified buffer with compressed 353 * data. Returns actual number of bytes of compressed data. A return value 354 * of 0 indicates that {@link #needsInput() needsInput} should be called 355 * in order to determine if more input data is required. 356 * 357 * <p>This method uses {@link #NO_FLUSH} as its compression flush mode. 358 * An invocation of this method of the form {@code deflater.deflate(b, off, len)} 359 * yields the same result as the invocation of 360 * {@code deflater.deflate(b, off, len, Deflater.NO_FLUSH)}. 361 * 362 * @param b the buffer for the compressed data 363 * @param off the start offset of the data 364 * @param len the maximum number of bytes of compressed data 365 * @return the actual number of bytes of compressed data written to the 366 * output buffer 367 */ 368 public int deflate(byte[] b, int off, int len) { 369 return deflate(b, off, len, NO_FLUSH); 370 } 371 372 /** 373 * Compresses the input data and fills specified buffer with compressed 374 * data. Returns actual number of bytes of compressed data. A return value 375 * of 0 indicates that {@link #needsInput() needsInput} should be called 376 * in order to determine if more input data is required. 377 * 378 * <p>This method uses {@link #NO_FLUSH} as its compression flush mode. 379 * An invocation of this method of the form {@code deflater.deflate(b)} 380 * yields the same result as the invocation of 381 * {@code deflater.deflate(b, 0, b.length, Deflater.NO_FLUSH)}. 382 * 383 * @param b the buffer for the compressed data 384 * @return the actual number of bytes of compressed data written to the 385 * output buffer 386 */ 387 public int deflate(byte[] b) { 388 return deflate(b, 0, b.length, NO_FLUSH); 389 } 390 391 /** 392 * Compresses the input data and fills the specified buffer with compressed 393 * data. Returns actual number of bytes of data compressed. 394 * 395 * <p>Compression flush mode is one of the following three modes: 396 * 397 * <ul> 398 * <li>{@link #NO_FLUSH}: allows the deflater to decide how much data 399 * to accumulate, before producing output, in order to achieve the best 400 * compression (should be used in normal use scenario). A return value 401 * of 0 in this flush mode indicates that {@link #needsInput()} should 402 * be called in order to determine if more input data is required. 403 * 404 * <li>{@link #SYNC_FLUSH}: all pending output in the deflater is flushed, 405 * to the specified output buffer, so that an inflater that works on 406 * compressed data can get all input data available so far (In particular 407 * the {@link #needsInput()} returns {@code true} after this invocation 408 * if enough output space is provided). Flushing with {@link #SYNC_FLUSH} 409 * may degrade compression for some compression algorithms and so it 410 * should be used only when necessary. 411 * 412 * <li>{@link #FULL_FLUSH}: all pending output is flushed out as with 413 * {@link #SYNC_FLUSH}. The compression state is reset so that the inflater 414 * that works on the compressed output data can restart from this point 415 * if previous compressed data has been damaged or if random access is 416 * desired. Using {@link #FULL_FLUSH} too often can seriously degrade 417 * compression. 418 * </ul> 419 * 420 * <p>In the case of {@link #FULL_FLUSH} or {@link #SYNC_FLUSH}, if 421 * the return value is {@code len}, the space available in output 422 * buffer {@code b}, this method should be invoked again with the same 423 * {@code flush} parameter and more output space. Make sure that 424 * {@code len} is greater than 6 to avoid flush marker (5 bytes) being 425 * repeatedly output to the output buffer every time this method is 426 * invoked. 427 * 428 * @param b the buffer for the compressed data 429 * @param off the start offset of the data 430 * @param len the maximum number of bytes of compressed data 431 * @param flush the compression flush mode 432 * @return the actual number of bytes of compressed data written to 433 * the output buffer 434 * 435 * @throws IllegalArgumentException if the flush mode is invalid 436 * @since 1.7 437 */ 438 public int deflate(byte[] b, int off, int len, int flush) { 439 if (b == null) { 440 throw new NullPointerException(); 441 } 442 if (off < 0 || len < 0 || off > b.length - len) { 443 throw new ArrayIndexOutOfBoundsException(); 444 } 445 synchronized (zsRef) { 446 ensureOpen(); 447 if (flush == NO_FLUSH || flush == SYNC_FLUSH || 448 flush == FULL_FLUSH) { 449 int thisLen = this.len; 450 int n = deflateBytes(zsRef.address(), b, off, len, flush); 451 bytesWritten += n; 452 bytesRead += (thisLen - this.len); 453 return n; 454 } 455 throw new IllegalArgumentException(); 456 } 457 } 458 459 /** 460 * Returns the ADLER-32 value of the uncompressed data. 461 * @return the ADLER-32 value of the uncompressed data 462 */ 463 public int getAdler() { 464 synchronized (zsRef) { 465 ensureOpen(); 466 return getAdler(zsRef.address()); 467 } 468 } 469 470 /** 471 * Returns the total number of uncompressed bytes input so far. 472 * 473 * <p>Since the number of bytes may be greater than 474 * Integer.MAX_VALUE, the {@link #getBytesRead()} method is now 475 * the preferred means of obtaining this information.</p> 476 * 477 * @return the total number of uncompressed bytes input so far 478 */ 479 public int getTotalIn() { 480 return (int) getBytesRead(); 481 } 482 483 /** 484 * Returns the total number of uncompressed bytes input so far. 485 * 486 * @return the total (non-negative) number of uncompressed bytes input so far 487 * @since 1.5 488 */ 489 public long getBytesRead() { 490 synchronized (zsRef) { 491 ensureOpen(); 492 return bytesRead; 493 } 494 } 495 496 /** 497 * Returns the total number of compressed bytes output so far. 498 * 499 * <p>Since the number of bytes may be greater than 500 * Integer.MAX_VALUE, the {@link #getBytesWritten()} method is now 501 * the preferred means of obtaining this information.</p> 502 * 503 * @return the total number of compressed bytes output so far 504 */ 505 public int getTotalOut() { 506 return (int) getBytesWritten(); 507 } 508 509 /** 510 * Returns the total number of compressed bytes output so far. 511 * 512 * @return the total (non-negative) number of compressed bytes output so far 513 * @since 1.5 514 */ 515 public long getBytesWritten() { 516 synchronized (zsRef) { 517 ensureOpen(); 518 return bytesWritten; 519 } 520 } 521 522 /** 523 * Resets deflater so that a new set of input data can be processed. 524 * Keeps current compression level and strategy settings. 525 */ 526 public void reset() { 527 synchronized (zsRef) { 528 ensureOpen(); 529 reset(zsRef.address()); 530 finish = false; 531 finished = false; 532 off = len = 0; 533 bytesRead = bytesWritten = 0; 534 } 535 } 536 537 /** 538 * Closes the compressor and discards any unprocessed input. 539 * 540 * This method should be called when the compressor is no longer 541 * being used. Once this method is called, the behavior of the 542 * Deflater object is undefined. 543 */ 544 public void end() { 545 synchronized (zsRef) { 546 zsRef.clean(); 547 buf = null; 548 } 549 } 550 551 /** 552 * Closes the compressor when garbage is collected. 553 * 554 * @deprecated The {@code finalize} method has been deprecated and 555 * implemented as a no-op. Subclasses that override {@code finalize} 556 * in order to perform cleanup should be modified to use alternative 557 * cleanup mechanisms and to remove the overriding {@code finalize} 558 * method. The recommended cleanup for compressor is to explicitly 559 * call {@code end} method when it is no longer in use. If the 560 * {@code end} is not invoked explicitly the resource of the compressor 561 * will be released when the instance becomes phantom-reachable. 562 */ 563 @Deprecated(since="9", forRemoval=true) 564 protected void finalize() {} 565 566 private void ensureOpen() { 567 assert Thread.holdsLock(zsRef); 568 if (zsRef.address() == 0) 569 throw new NullPointerException("Deflater has been closed"); 570 } 571 572 private static native void initIDs(); 573 private static native long init(int level, int strategy, boolean nowrap); 574 private static native void setDictionary(long addr, byte[] b, int off, int len); 575 private native int deflateBytes(long addr, byte[] b, int off, int len, 576 int flush); 577 private static native int getAdler(long addr); 578 private static native void reset(long addr); 579 private static native void end(long addr); 580 }