@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(); } }
/** * Writes any unwritten data to the underlying stream. Does not close the * stream. * * @throws IOException * If an error occurs. */ public void finish() throws IOException { if (done) { return; } def.finish(); while (!def.finished()) { int byteCount = def.deflate(buf); out.write(buf, 0, byteCount); } done = true; }
void flushDeflater() throws IOException { def.finish(); while (!def.finished()) { deflate(); } }
public static byte[] compress(byte[] data) throws IOException { long startTime = System.currentTimeMillis(); Deflater deflater = new Deflater(1); deflater.setInput(data); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length); deflater.finish(); byte[] buffer = new byte[1024]; while (!deflater.finished()) { int count = deflater.deflate(buffer); // returns the generated code... index outputStream.write(buffer, 0, count); } outputStream.close(); byte[] output = outputStream.toByteArray(); logger.debug("Original: " + data.length + " bytes. " + "Compressed: " + output.length + " byte. Time: " + (System.currentTimeMillis() - startTime)); return output; }
private byte[] tryCompress(byte[] in, final int len) { // Compress the bytes final Deflater compresser = new Deflater(COMPRESSION_LEVEL, true); compresser.setInput(in); compresser.finish(); final byte[] output = new byte[len]; final int compressedDataLength = compresser.deflate(output); if (compresser.finished() == false) { return null; } final byte[] result = copyArray(output, compressedDataLength); return result; }
/** * Writes any unwritten compressed data to the underlying stream, the closes * all underlying streams. This stream can no longer be used after close() * has been called. * * @throws IOException * If an error occurs while closing the data compression * process. */ @Override public void close() throws IOException { // everything closed here should also be closed in ZipOutputStream.close() if (!def.finished()) { finish(); } def.end(); out.close(); }
/** * Override because certain implementation calls def.end() which * causes problem when resetting the stream for reuse. */ @Override public void finish() throws IOException { if (HAS_BROKEN_FINISH) { if (!def.finished()) { def.finish(); while (!def.finished()) { int i = def.deflate(this.buf, 0, this.buf.length); if ((def.finished()) && (i <= this.buf.length - TRAILER_SIZE)) { writeTrailer(this.buf, i); i += TRAILER_SIZE; out.write(this.buf, 0, i); return; } if (i > 0) { out.write(this.buf, 0, i); } } byte[] arrayOfByte = new byte[TRAILER_SIZE]; writeTrailer(arrayOfByte, 0); out.write(arrayOfByte); } } else { super.finish(); } }
while (!deflater.finished()) { deflate(footer); if (!footer.isWritable()) {
public static byte[] compress(byte[] value, int offset, int length, int compressionLevel) { ByteArrayOutputStream bos = new ByteArrayOutputStream(length); Deflater compressor = new Deflater(); try { // 将当前压缩级别设置为指定值 compressor.setLevel(compressionLevel); compressor.setInput(value, offset, length); // 调用时,指示压缩应当以输入缓冲区的当前内容结尾 compressor.finish(); final byte[] buf = new byte[1024]; while (!compressor.finished()) { // 如果已到达压缩数据输出流的结尾,则返回 true。 int count = compressor.deflate(buf); // 使用压缩数据填充指定缓冲区。 bos.write(buf, 0, count); } } finally { // 关闭解压缩器并放弃所有未处理的输入 compressor.end(); } return bos.toByteArray(); }
/** * Ensures all bytes sent to the deflater are written to the stream. */ private void flushDeflater() throws IOException { if (entry.entry.getMethod() == DEFLATED) { def.finish(); while (!def.finished()) { deflate(); } } }
/** * Compresses the specified byte range using the specified compressionLevel (constants are * defined in java.util.zip.Deflater). */ public static byte[] compress(byte[] value, int offset, int length, int compressionLevel) { /* Create an expandable byte array to hold the compressed data. * You cannot use an array that's the same size as the orginal because * there is no guarantee that the compressed data will be smaller than * the uncompressed data. */ ByteArrayOutputStream bos = new ByteArrayOutputStream(length); Deflater compressor = new Deflater(); try { compressor.setLevel(compressionLevel); compressor.setInput(value, offset, length); compressor.finish(); final byte[] buf = new byte[1024]; while (!compressor.finished()) { int count = compressor.deflate(buf); bos.write(buf, 0, count); } } finally { compressor.end(); } return bos.toByteArray(); }
/** * Ensures all bytes sent to the deflater are written to the stream. */ private void flushDeflater() throws IOException { if (entry.entry.getMethod() == DEFLATED) { def.finish(); while (!def.finished()) { deflate(); } } }
/** * {@inheritDoc} * * @since 1.1 */ @Override public void write(final byte[] buffer, final int offset, final int length) throws IOException { if (deflater.finished()) { throw new IOException("Cannot write more data, the end of the compressed data stream has been reached"); } else if (length > 0) { deflater.setInput(buffer, offset, length); while (!deflater.needsInput()) { deflate(); } crc.update(buffer, offset, length); } }
/** * @param bytes Bytes to compress. * @return Zip-compressed bytes. */ private static byte[] zip(byte[] bytes) { Deflater deflater = new Deflater(); deflater.setInput(bytes); deflater.finish(); GridByteArrayOutputStream out = new GridByteArrayOutputStream(bytes.length); final byte[] buf = new byte[bytes.length]; while (!deflater.finished()) { int cnt = deflater.deflate(buf); out.write(buf, 0, cnt); } return out.toByteArray(); }
while (!deflater.finished()) { deflate(footer); if (!footer.isWritable()) {
private void writeDeflated(final byte[] b, final int offset, final int length) throws IOException { if (length > 0 && !def.finished()) { if (length <= DEFLATER_BLOCK_SIZE) { def.setInput(b, offset, length); deflateUntilInputIsNeeded(); } else { final int fullblocks = length / DEFLATER_BLOCK_SIZE; for (int i = 0; i < fullblocks; i++) { def.setInput(b, offset + i * DEFLATER_BLOCK_SIZE, DEFLATER_BLOCK_SIZE); deflateUntilInputIsNeeded(); } final int done = fullblocks * DEFLATER_BLOCK_SIZE; if (done < length) { def.setInput(b, offset + done, length - done); deflateUntilInputIsNeeded(); } } } }
private static byte[] deflate(byte[] input) throws IOException { Deflater compressor = new Deflater(); compressor.setLevel(Deflater.BEST_COMPRESSION); compressor.setInput(input); compressor.finish(); try (ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length)) { byte[] buf = new byte[1024]; while (!compressor.finished()) { int count = compressor.deflate(buf); bos.write(buf, 0, count); } return bos.toByteArray(); } } }
/** * write implementation for DEFLATED entries. */ private void writeDeflated(byte[]b, int offset, int length) throws IOException { if (length > 0 && !def.finished()) { entry.bytesRead += length; if (length <= DEFLATER_BLOCK_SIZE) { def.setInput(b, offset, length); deflateUntilInputIsNeeded(); } else { final int fullblocks = length / DEFLATER_BLOCK_SIZE; for (int i = 0; i < fullblocks; i++) { def.setInput(b, offset + i * DEFLATER_BLOCK_SIZE, DEFLATER_BLOCK_SIZE); deflateUntilInputIsNeeded(); } final int done = fullblocks * DEFLATER_BLOCK_SIZE; if (done < length) { def.setInput(b, offset + done, length - done); deflateUntilInputIsNeeded(); } } } }
private StreamSinkConduit createNextChannel() { if (deflater.finished() && allAreSet(state, WRITTEN_TRAILER)) { //the deflater was fully flushed before we created the channel. This means that what is in the buffer is //all there is int remaining = currentBuffer.getBuffer().remaining(); if (additionalBuffer != null) { remaining += additionalBuffer.remaining(); } if(!exchange.getResponseHeaders().contains(Headers.TRANSFER_ENCODING)) { exchange.getResponseHeaders().put(Headers.CONTENT_LENGTH, Integer.toString(remaining)); } } else { exchange.getResponseHeaders().remove(Headers.CONTENT_LENGTH); } return conduitFactory.create(); }