Refine search
/** * 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; }
@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(); } }
/** * zlib compress 2 byte * * @param bytesToCompress * @return */ public static byte[] compressForZlib(byte[] bytesToCompress) { Deflater deflater = new Deflater(); deflater.setInput(bytesToCompress); deflater.finish(); byte[] bytesCompressed = new byte[Short.MAX_VALUE]; int numberOfBytesAfterCompression = deflater.deflate(bytesCompressed); byte[] returnValues = new byte[numberOfBytesAfterCompression]; System.arraycopy ( bytesCompressed, 0, returnValues, 0, numberOfBytesAfterCompression ); return returnValues; }
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; }
/** * Compresses the currently buffered chunk of data and sends it to the output stream * * @throws IOException if issues occur writing to stream */ protected void compressAndWrite() throws IOException { if (bufferIndex <= 0) { return; } deflater.setInput(buffer, 0, bufferIndex); deflater.finish(); final int compressedBytes = deflater.deflate(compressed); writeChunkHeader(compressedBytes); out.write(compressed, 0, compressedBytes); bufferIndex = 0; deflater.reset(); }
/** * 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(); } }
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; }
public static DeflaterInflaterData compressBytes(byte[] input) { Deflater compresser = new Deflater(); compresser.setInput(input); compresser.finish(); byte[] buffer = new byte[100]; byte[] result = new byte[0]; int compressedDataLength; int totalCompressedDataLength = 0; do { byte[] newResult = new byte[result.length + buffer.length]; System.arraycopy(result, 0, newResult, 0, result.length); compressedDataLength = compresser.deflate(buffer); totalCompressedDataLength += compressedDataLength; System.arraycopy(buffer, 0, newResult, result.length, buffer.length); result = newResult; } while (compressedDataLength != 0); return new DeflaterInflaterData(totalCompressedDataLength, result); }
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(); }
@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; }
/** * 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(); }