private void deflateUntilInputIsNeeded() throws IOException { while (!def.needsInput()) { deflate(); } }
private void deflateUntilInputIsNeeded() throws IOException { while (!def.needsInput()) { deflate(); } }
private void deflateUntilInputIsNeeded() throws IOException { while (!def.needsInput()) { 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); } }
for (;;) { deflate(out); if (deflater.needsInput()) {
/** * Compresses {@code byteCount} bytes of data from {@code buf} starting at * {@code offset} and writes it to the underlying stream. * @throws IOException * If an error occurs during writing. */ @Override public void write(byte[] buffer, int offset, int byteCount) throws IOException { if (done) { throw new IOException("attempt to write after finish"); } Arrays.checkOffsetAndCount(buffer.length, offset, byteCount); if (!def.needsInput()) { throw new IOException(); } def.setInput(buffer, offset, byteCount); deflate(); }
compressDone = true; break; } else if (deflater.needsInput()) {
for (;;) { deflate(out); if (deflater.needsInput()) {
if (def.needsInput()) {
while (!deflater.needsInput()) { deflate(out);
if (!zlib.needsInput())
@Override public ByteBuf encode(ByteBuf source) { byte[] array; int length = source.readableBytes(); int sizeEstimate = (int) Math.ceil(source.readableBytes() * 1.001) + 14; ByteBuf compressed = PooledByteBufAllocator.DEFAULT.heapBuffer(sizeEstimate); int offset = 0; if (source.hasArray()) { array = source.array(); offset = source.arrayOffset() + source.readerIndex(); } else { // If it's a direct buffer, we need to copy it array = new byte[length]; source.getBytes(source.readerIndex(), array); } Deflater deflater = this.deflater.get(); deflater.reset(); deflater.setInput(array, offset, length); while (!deflater.needsInput()) { deflate(deflater, compressed); } return compressed; }
@Override public void compress(byte[] bytes, int off, int len, DataOutput out) throws IOException { compressor.reset(); compressor.setInput(bytes, off, len); compressor.finish(); if (compressor.needsInput()) { // no output assert len == 0 : len; out.writeVInt(0); return; } int totalCount = 0; for (;;) { final int count = compressor.deflate(compressed, totalCount, compressed.length - totalCount); totalCount += count; assert totalCount <= compressed.length; if (compressor.finished()) { break; } else { compressed = ArrayUtil.grow(compressed); } } out.writeVInt(totalCount); out.writeBytes(compressed, totalCount); }
outBuffer.compact(); } else if (deflater.needsInput() && pos == 0) { if (deflater.finished()) {
if (!deflater.needsInput()) { deflateData(false); if (!deflater.needsInput()) { return 0;
@IgnoreJRERequirement private void deflate(boolean syncFlush) throws IOException { Buffer buffer = sink.buffer(); while (true) { Segment s = buffer.writableSegment(1); // The 4-parameter overload of deflate() doesn't exist in the RI until // Java 1.7, and is public (although with @hide) on Android since 2.3. // The @hide tag means that this code won't compile against the Android // 2.3 SDK, but it will run fine there. int deflated = syncFlush ? deflater.deflate(s.data, s.limit, Segment.SIZE - s.limit, Deflater.SYNC_FLUSH) : deflater.deflate(s.data, s.limit, Segment.SIZE - s.limit); if (deflated > 0) { s.limit += deflated; buffer.size += deflated; sink.emitCompleteSegments(); } else if (deflater.needsInput()) { if (s.pos == s.limit) { // We allocated a tail segment, but didn't end up needing it. Recycle! buffer.head = s.pop(); SegmentPool.recycle(s); } return; } } }
while ((!compress.needsInput() && !compress.finished()) || !outputBuffer.hasRemaining() || (onceOnly && lastFrame)) { onceOnly = false;
while (force || !deflater.needsInput() || (shutdown && !deflater.finished())) { int count = deflater.deflate(buf.array(), buf.arrayOffset(), buf.remaining(), force ? Deflater.SYNC_FLUSH: Deflater.NO_FLUSH); Connectors.updateResponseBytesSent(exchange, count);
@Override protected Object encode(ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception { if (!(msg instanceof ChannelBuffer) || finished.get()) { return msg; } final ChannelBuffer uncompressed = (ChannelBuffer) msg; final int uncompressedLen = uncompressed.readableBytes(); if (uncompressedLen == 0) { return uncompressed; } final byte[] in = new byte[uncompressedLen]; uncompressed.readBytes(in); final int sizeEstimate = estimateCompressedSize(uncompressedLen); final ChannelBuffer compressed = ChannelBuffers.dynamicBuffer(sizeEstimate, channel.getConfig().getBufferFactory()); synchronized (deflater) { if (isGzip()) { crc.update(in); if (writeHeader) { compressed.writeBytes(gzipHeader); writeHeader = false; } } deflater.setInput(in); while (!deflater.needsInput()) { deflate(compressed); } } return compressed; }
@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; } }