/** Disposal will happen automatically in {@link #finalize()} but can be done explicitly if desired. */ @SuppressWarnings("javadoc") public void dispose () { deflater.end(); }
/** Disposal will happen automatically in {@link #finalize()} but can be done explicitly if desired. */ @SuppressWarnings("javadoc") public void dispose () { deflater.end(); }
@Override protected void onRemoval(Deflater deflater) throws Exception { deflater.end(); } };
@Override public void close() throws IOException { if (closed == false) { compressor.end(); closed = true; } }
@Override public void dispose() { // Call end so that native zlib resources can be immediately released rather than relying on finalizer compress.end(); decompress.end(); } }
/** * Closes the underlying input stream and discards any remaining uncompressed * data. */ @Override public void close() throws IOException { closed = true; def.end(); in.close(); }
@Override public void end() { if (finished) { return; } finished = true; compressor.end(); super.end(); } }
@Override public int compress(byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset, int maxOutputLength) { int maxCompressedLength = maxCompressedLength(inputLength); if (maxOutputLength < maxCompressedLength) { throw new IllegalArgumentException("Output buffer must be at least " + maxCompressedLength + " bytes"); } Deflater deflater = new Deflater(COMPRESSION_LEVEL, true); try { deflater.setInput(input, inputOffset, inputLength); deflater.finish(); int compressedDataLength = deflater.deflate(output, outputOffset, maxOutputLength, FULL_FLUSH); if (!deflater.finished()) { throw new IllegalStateException("maxCompressedLength formula is incorrect, because deflate produced more data"); } return compressedDataLength; } finally { deflater.end(); } }
public static byte[] compress(final byte[] src, final int level) throws IOException { byte[] result = src; ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(src.length); java.util.zip.Deflater defeater = new java.util.zip.Deflater(level); DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteArrayOutputStream, defeater); try { deflaterOutputStream.write(src); deflaterOutputStream.finish(); deflaterOutputStream.close(); result = byteArrayOutputStream.toByteArray(); } catch (IOException e) { defeater.end(); throw e; } finally { try { byteArrayOutputStream.close(); } catch (IOException ignored) { } defeater.end(); } return result; }
@Override public void close() throws IOException { try { out.close(); } finally { deflater.end(); } } }
@Override public void close() throws IOException { if (!closed) { try { finish(); } finally { deflater.end(); out.close(); closed = true; } } }
@Override public void close() throws IOException { try { deflaterOutputStream.close(); } finally { deflater.end(); } } }
/** * Flushes remaining buffer and calls {@link Deflater#end()} to free acquired memory to prevent OutOfMemory error. * @throws IOException for any issues closing underlying stream */ @Override public void close() throws IOException { compressAndWrite(); out.write(0); // indicate that the stream is finished. out.flush(); deflater.end(); } }
@Override public synchronized void end() { if (finished) { return; } finished = true; compressor.end(); super.end(); } }
@Override public void close() throws IOException { super.close(); def.end(); } };
@Override public int compress(byte[] in, int inLen, byte[] out, int outPos) { Deflater deflater = new Deflater(level); deflater.setStrategy(strategy); deflater.setInput(in, 0, inLen); deflater.finish(); int compressed = deflater.deflate(out, outPos, out.length - outPos); while (compressed == 0) { // the compressed length is 0, meaning compression didn't work // (sounds like a JDK bug) // try again, using the default strategy and compression level strategy = Deflater.DEFAULT_STRATEGY; level = Deflater.DEFAULT_COMPRESSION; return compress(in, inLen, out, outPos); } deflater.end(); return outPos + compressed; }
footer.writeByte(uncBytes >>> 24); deflater.end(); return ctx.writeAndFlush(footer, promise);
@Override public void close() throws IOException { if (closed) return; // Emit deflated data to the underlying sink. If this fails, we still need // to close the deflater and the sink; otherwise we risk leaking resources. Throwable thrown = null; try { finishDeflate(); } catch (Throwable e) { thrown = e; } try { deflater.end(); } catch (Throwable e) { if (thrown == null) thrown = e; } try { sink.close(); } catch (Throwable e) { if (thrown == null) thrown = e; } closed = true; if (thrown != null) Util.sneakyRethrow(thrown); }
@Override public void close() throws IOException { if (closed) return; // This method delegates to the DeflaterSink for finishing the deflate process // but keeps responsibility for releasing the deflater's resources. This is // necessary because writeFooter needs to query the processed byte count which // only works when the deflater is still open. Throwable thrown = null; try { deflaterSink.finishDeflate(); writeFooter(); } catch (Throwable e) { thrown = e; } try { deflater.end(); } catch (Throwable e) { if (thrown == null) thrown = e; } try { sink.close(); } catch (Throwable e) { if (thrown == null) thrown = e; } closed = true; if (thrown != null) Util.sneakyRethrow(thrown); }
@Test public void testInflate() { byte[] testData = TestUtil.buildTestData(/*arbitrary test data size*/ 256 * 1024); byte[] compressedData = new byte[testData.length * 2]; Deflater compresser = new Deflater(9); compresser.setInput(testData); compresser.finish(); int compressedDataLength = compresser.deflate(compressedData); compresser.end(); ParsableByteArray input = new ParsableByteArray(compressedData, compressedDataLength); ParsableByteArray output = new ParsableByteArray(); assertThat(Util.inflate(input, output, /* inflater= */ null)).isTrue(); assertThat(output.limit()).isEqualTo(testData.length); assertThat(Arrays.copyOf(output.data, output.limit())).isEqualTo(testData); }