Refine search
@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; }
final byte[] array = src.array(); final int arrayOffset = src.arrayOffset(); deflater.setInput(array, arrayOffset + pos, rem); c1 = deflater.getTotalIn(); final int dc = deflater.deflate(outArray, outBuffer.position(), outBuffer.remaining()); outBuffer.position(outBuffer.position() + dc); t = deflater.getTotalIn() - c1; } else { final byte[] bytes = Buffers.take(src); deflater.setInput(bytes); c1 = deflater.getTotalIn(); final int dc = deflater.deflate(outArray, outBuffer.position(), outBuffer.remaining()); outBuffer.position(outBuffer.position() + dc); t = deflater.getTotalIn() - c1;
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); }
final byte[] array = src.array(); final int arrayOffset = src.arrayOffset(); deflater.setInput(array, arrayOffset + pos, rem); c1 = deflater.getTotalIn(); final int dc = deflater.deflate(outArray, outBuffer.position(), outBuffer.remaining()); outBuffer.position(outBuffer.position() + dc); t = deflater.getTotalIn() - c1; } else { final byte[] bytes = Buffers.take(src); deflater.setInput(bytes); c1 = deflater.getTotalIn(); final int dc = deflater.deflate(outArray, outBuffer.position(), outBuffer.remaining()); outBuffer.position(outBuffer.position() + dc); t = deflater.getTotalIn() - c1;
PooledByteBuffer inputBuffer = null; if (buffer.hasArray()) { compress.setInput(buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.remaining()); } else { inputBuffer = toArrayBacked(buffer, channel.getWebSocketChannel().getBufferPool()); compress.setInput(inputBuffer.getBuffer().array(), inputBuffer.getBuffer().arrayOffset() + inputBuffer.getBuffer().position(), inputBuffer.getBuffer().remaining()); int n = compress.deflate( outputBuffer.array(), outputBuffer.arrayOffset() + outputBuffer.position(),
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 public void write(byte[] b, int off, int len) throws IOException { deflater.setInput(b, off, len); for (;;) { if (outPtr == zbuf.length) throw new EOFException(); int n = deflater.deflate(zbuf, outPtr, zbuf.length - outPtr); if (n == 0) { if (deflater.needsInput()) break; throw new IOException(); } outPtr += n; } }
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; }
@Override public void write(byte []buffer, int offset, int length) throws IOException { _deflater.setInput(buffer, offset, length); _crc.update(buffer, offset, length); byte []tBuffer = _tBuf.buffer(); int sublen; while ((sublen = _deflater.deflate(tBuffer, 0, tBuffer.length)) > 0) { _out.write(tBuffer, 0, sublen); } }
/** * 压缩. * * @param inputByte * 需要解压缩的byte[]数组 * @return 压缩后的数据 * @throws IOException */ public static byte[] deflater(final byte[] inputByte) throws IOException { int compressedDataLength = 0; Deflater compresser = new Deflater(); compresser.setInput(inputByte); compresser.finish(); ByteArrayOutputStream o = new ByteArrayOutputStream(inputByte.length); byte[] result = new byte[1024]; try { while (!compresser.finished()) { compressedDataLength = compresser.deflate(result); o.write(result, 0, compressedDataLength); } } finally { o.close(); } compresser.end(); return o.toByteArray(); }
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; }