GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Codec-compression: Add zlib decompressors by yawkat · Pull Request #17155 · netty/netty

Codec-compression: Add zlib decompressors #17155

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
chrisvest merged 25 commits into 4.2 from extract-zlib-decompressors
Jul 28, 2026

Uh oh!

There was an error while loading. Please reload this page.

Merged

Codec-compression: Add zlib decompressors #17155

Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
5737cd5
Codec-compression: Add zlib decompressor base
yawkat Jul 27, 2026
731fa0b
Codec-compression: Add JZlib decompressor
yawkat Jul 27, 2026
5b23fd4
Codec-compression: Add JDK zlib decompressor
yawkat Jul 27, 2026
fb6b4d6
ZlibCodecFactory: Add decompressor builder
yawkat Jul 27, 2026
1c8a165
JZlibDecompressor: Restore parent PR implementation
yawkat Jul 27, 2026
faa1113
JdkZlibDecompressor: Restore parent PR implementation
yawkat Jul 27, 2026
238f04d
ZlibDecompressor: Restore parent PR implementation
yawkat Jul 27, 2026
29321f0
JdkZlibDecompressor: Fix GZIP FEXTRA handling
yawkat Jul 27, 2026
0685084
JdkZlibDecompressor: Report complete after GZIP footer
yawkat Jul 27, 2026
754c82e
JZlibDecompressor: Release output on failure
yawkat Jul 27, 2026
d0af116
JdkZlibDecompressor: Release inflater on close
yawkat Jul 27, 2026
517d309
ZlibDecompressor: Reject negative max allocation
yawkat Jul 27, 2026
58fcb85
JZlibDecompressor: Reject truncated input
yawkat Jul 27, 2026
1ac2e09
JdkZlibDecompressor: Reject truncated input
yawkat Jul 27, 2026
3fe084a
JdkZlibDecompressor: Release output on failure
yawkat Jul 27, 2026
31a58ca
JZlibDecompressor: Release inflater on close
yawkat Jul 27, 2026
6a980a6
JdkZlibDecompressor: Validate GZIP magic
yawkat Jul 27, 2026
b6029da
JZlibDecompressorTest: Use try-with-resources
yawkat Jul 27, 2026
80a44f1
JZlibDecompressor: Simplify inflater lifecycle
yawkat Jul 27, 2026
732cf87
JZlibDecompressorTest: Cover direct input across outputs
yawkat Jul 27, 2026
cc1cc0c
JdkZlibDecompressor: Fix dictionary error message
yawkat Jul 27, 2026
b50e971
ZlibCodecFactory: Mark decompressor APIs unstable
yawkat Jul 27, 2026
914dfb7
ZlibCodecFactory: Remove decompressor builder
yawkat Jul 28, 2026
86a0db0
Codec-compression: Remove JZlib decompressor
yawkat Jul 28, 2026
553478f
JdkZlibDecompressor: Inline zlib decompressor base
yawkat Jul 28, 2026
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view
Prev Previous commit
Next Next commit
JdkZlibDecompressor: Restore parent PR implementation
Co-Authored-By: multicode <[email protected]>
  • Loading branch information

Uh oh!

There was an error while loading. Please reload this page.

@yawkat @multicode-agent
yawkat and multicode-agent committed Jul 27, 2026
commit faa1113926812d058d1d988ce4a2d99e4e2b2a97

Some comments aren't visible on the classic Files Changed page.

There are no files selected for viewing

123 changes: 46 additions & 77 deletions 123 codec-compression/src/main/java/io/netty/handler/codec/compression/JdkZlibDecompressor.java
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.ByteBufUtil;
import io.netty.util.internal.EmptyArrays;
import io.netty.util.internal.UnstableApi;

import java.util.zip.CRC32;
Expand All @@ -38,7 +37,6 @@ public final class JdkZlibDecompressor extends ZlibDecompressor {
private static final int FRESERVED = 0xE0;

private Inflater inflater;
Comment thread
yawkat marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.

private final ZlibWrapper wrapper;

// GZIP related
private final ByteBufChecksum crc;
Expand All @@ -61,8 +59,6 @@ private enum GzipState {

private boolean decideZlibOrNone;
private boolean finished;
private boolean gzipMemberFinished;
private boolean closed;
/**
* If this is true, part of the input buffer is still in use by the {@link #inflater}, so we shouldn't touch that
* buffer too much (e.g. compact it).
Expand All @@ -71,7 +67,6 @@ private enum GzipState {

JdkZlibDecompressor(Builder builder, ByteBufAllocator allocator) {
super(builder, allocator);
this.wrapper = builder.wrapper;
this.decompressConcatenated = builder.decompressConcatenated;
switch (builder.wrapper) {
case GZIP:
Expand All @@ -98,10 +93,10 @@ private enum GzipState {

@Override
public Status status() throws DecompressionException {
if (finished) {
return Status.COMPLETE;
} else if (inflater == null || inflater.needsInput() || gzipState == GzipState.FOOTER_START) {
if (inflater == null || (inflater.needsInput() && !finished) || gzipState == GzipState.FOOTER_START) {
Comment thread
yawkat marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.

return Status.NEED_INPUT;
} else if (finished) {
return Status.COMPLETE;
} else {
return Status.NEED_OUTPUT;
}
Expand All @@ -110,14 +105,14 @@ public Status status() throws DecompressionException {
@Override
public void endOfInput() throws DecompressionException {
if (finished) {
// make sure we return COMPLETE from status()
gzipState = GzipState.HEADER_START;
return;
}
if (wrapper == ZlibWrapper.GZIP && decompressConcatenated && gzipMemberFinished &&
gzipState == GzipState.HEADER_START && available() == 0) {
finished = true;
return;
if (gzipState != GzipState.HEADER_START) {
throw new DecompressionException("Incomplete gzip framing");
}
throw new DecompressionException("Compressed stream ended before the end-of-stream marker");
finished = true;
Comment thread
yawkat marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.

}

@Override
Expand Down Expand Up @@ -179,69 +174,60 @@ private void consumeInput(ByteBuf buf) {
buf.readerIndex(buf.writerIndex() - inflater.getRemaining());
if (inflater.getRemaining() == 0) {
inputBufferInInflater = false;
inflater.setInput(EmptyArrays.EMPTY_BYTES);
}
}
}

@Override
ByteBuf processOutput(ByteBuf buf) throws DecompressionException {
ByteBuf decompressed = allocator.heapBuffer(outputBufferSize(inflater.getRemaining()));
boolean success = false;
int proposedCapacity = inflater.getRemaining() << 1;
int targetCapacity = maxAllocation == 0
? proposedCapacity : Math.min(maxAllocation, proposedCapacity);
ByteBuf decompressed = allocator.heapBuffer(targetCapacity);
Comment thread
yawkat marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.

byte[] outArray = decompressed.array();
int writerIndex = decompressed.writerIndex();
int outIndex = decompressed.arrayOffset() + writerIndex;
int writable = decompressed.writableBytes();
int outputLength;
try {
byte[] outArray = decompressed.array();
int writerIndex = decompressed.writerIndex();
int outIndex = decompressed.arrayOffset() + writerIndex;
int outputLength;
try {
outputLength = inflater.inflate(outArray, outIndex, decompressed.writableBytes());
} catch (DataFormatException e) {
throw new DecompressionException("decompression failure", e);
}
consumeInput(buf);
if (outputLength > 0) {
decompressed.writerIndex(writerIndex + outputLength);
if (crc != null) {
crc.update(outArray, outIndex, outputLength);
}
}
if (inflater.needsDictionary()) {
if (dictionary == null) {
throw new DecompressionException(
"decompression failure, unable to set dictionary as none was specified");
}
inflater.setDictionary(dictionary);
} else if (outputLength == 0 && !inflater.needsInput() && !inflater.finished()) {
throw new DecompressionException("Decompression made no progress");
outputLength = inflater.inflate(outArray, outIndex, writable);
} catch (DataFormatException e) {
throw new DecompressionException("decompression failure", e);
}
consumeInput(buf);
if (outputLength > 0) {
decompressed.writerIndex(writerIndex + outputLength);
if (crc != null) {
crc.update(outArray, outIndex, outputLength);
}
if (inflater.finished()) {
inputBufferInInflater = false;
if (crc == null) {
finished = true; // Do not decode anymore.
} else {
gzipState = GzipState.FOOTER_START;
// potentially consume footer
processInput(buf);
}
}
if (inflater.needsDictionary()) {
if (dictionary == null) {
throw new DecompressionException(
"decompression failure, unable to set dictionary as non was specified");
}
success = true;
return decompressed;
} finally {
if (!success) {
decompressed.release();
inflater.setDictionary(dictionary);
}
if (inflater.finished()) {
inputBufferInInflater = false;
if (crc == null) {
finished = true; // Do not decode anymore.
} else {
gzipState = GzipState.FOOTER_START;
// potentially consume footer
processInput(buf);
}
}
return decompressed;
}

private boolean handleGzipFooter(ByteBuf in) {
if (readGZIPFooter(in)) {
gzipMemberFinished = true;
finished = !decompressConcatenated;

if (!finished) {
inflater.reset();
crc.reset();
xlen = -1;
gzipState = GzipState.HEADER_START;
return true;
}
Expand All @@ -256,10 +242,10 @@ private boolean readGZIPHeader(ByteBuf in) {
return false;
}
// read magic numbers
int magic0 = in.readUnsignedByte();
int magic1 = in.readUnsignedByte();
int magic0 = in.readByte();
int magic1 = in.readByte();

if (magic0 != 31 || magic1 != 139) {
if (magic0 != 31) {
Comment thread
yawkat marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.

throw new DecompressionException("Input is not in the GZIP format");
}
crc.update(magic0);
Expand Down Expand Up @@ -299,7 +285,7 @@ private boolean readGZIPHeader(ByteBuf in) {
crc.update(xlen1);
crc.update(xlen2);

xlen = xlen2 << 8 | xlen1;
xlen |= xlen1 << 8 | xlen2;
Comment thread
yawkat marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.

}
gzipState = GzipState.XLEN_READ;
// fall through
Expand Down Expand Up @@ -440,22 +426,6 @@ private static boolean looksLikeZlib(short cmf_flg) {
cmf_flg % 31 == 0;
}

@Override
public void close() {
if (closed) {
return;
}
closed = true;
try {
super.close();
} finally {
if (inflater != null) {
inflater.end();
inflater = null;
}
}
}

@UnstableApi
public static Builder builder() {
return new Builder();
Expand Down Expand Up @@ -499,7 +469,6 @@ public Builder decompressConcatenated(boolean decompressConcatenated) {

@Override
public Decompressor build(ByteBufAllocator allocator) throws DecompressionException {
validate();
return new DefensiveDecompressor(new JdkZlibDecompressor(this, allocator));
}
}
Expand Down
112 changes: 0 additions & 112 deletions 112 ...compression/src/test/java/io/netty/handler/codec/compression/JdkZlibDecompressorTest.java
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,14 @@
*/
package io.netty.handler.codec.compression;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.CompositeByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandler;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.Parameter;
import org.junit.jupiter.params.ParameterizedClass;
import org.junit.jupiter.params.provider.MethodSource;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.GZIPOutputStream;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
@ParameterizedClass
@MethodSource("wrappers")
public class JdkZlibDecompressorTest extends AbstractDecompressorTest {
Expand All @@ -54,104 +42,4 @@ protected ChannelHandler createCompressor() {
protected Decompressor.AbstractDecompressorBuilder createDecompressor() {
return JdkZlibDecompressor.builder().wrapper(wrapper);
}

@Test
public void testGzipWithExtraField() throws Exception {
assumeTrue(wrapper == ZlibWrapper.GZIP);
byte[] expected = "gzip extra field".getBytes("UTF-8");
byte[] extra = { 0x42, 0x43, 0x02, 0x00, (byte) 0x99, 0x00 };

assertArrayEquals(expected, decompress(
JdkZlibDecompressor.builder().wrapper(ZlibWrapper.GZIP).build(ByteBufAllocator.DEFAULT),
gzipWithExtraField(expected, extra)));
}

@Test
public void testConcatenatedGzipResetsExtraFieldLength() throws Exception {
assumeTrue(wrapper == ZlibWrapper.GZIP);
byte[] first = "first".getBytes("UTF-8");
byte[] second = "second".getBytes("UTF-8");
byte[] extra = { 0x42, 0x43, 0x02, 0x00, (byte) 0x99, 0x00 };
byte[] firstGzip = gzipWithExtraField(first, extra);
byte[] secondGzip = gzip(second);
byte[] compressed = new byte[firstGzip.length + secondGzip.length];
System.arraycopy(firstGzip, 0, compressed, 0, firstGzip.length);
System.arraycopy(secondGzip, 0, compressed, firstGzip.length, secondGzip.length);

assertArrayEquals("firstsecond".getBytes("UTF-8"), decompress(
JdkZlibDecompressor.builder().wrapper(ZlibWrapper.GZIP).decompressConcatenated(true)
.build(ByteBufAllocator.DEFAULT), compressed));
}

@Test
public void testTruncatedInputRejected() throws Exception {
assumeTrue(wrapper == ZlibWrapper.ZLIB);
byte[] compressed = deflate("truncated".getBytes("UTF-8"));

assertThrows(DecompressionException.class, () -> decompress(
JdkZlibDecompressor.builder().wrapper(ZlibWrapper.ZLIB).build(ByteBufAllocator.DEFAULT),
Arrays.copyOf(compressed, compressed.length - 1)));
}

@Test
public void testNegativeMaxAllocationRejected() {
assumeTrue(wrapper == ZlibWrapper.GZIP);
assertThrows(IllegalArgumentException.class, () ->
JdkZlibDecompressor.builder().maxAllocation(-1).build(ByteBufAllocator.DEFAULT));
}

private static byte[] decompress(Decompressor decompressor, byte[] compressed) throws Exception {
CompositeByteBuf output = ByteBufAllocator.DEFAULT.compositeBuffer();
try (Decompressor ignored = decompressor) {
decompressor.status();
decompressor.addInput(Unpooled.wrappedBuffer(compressed));
for (;;) {
switch (decompressor.status()) {
case NEED_INPUT:
decompressor.endOfInput();
break;
case NEED_OUTPUT:
output.addComponent(true, decompressor.takeOutput());
break;
case COMPLETE:
byte[] bytes = new byte[output.readableBytes()];
output.readBytes(bytes);
return bytes;
default:
throw new AssertionError("Unknown decompressor status");
}
}
} finally {
output.release();
}
}

private static byte[] gzip(byte[] data) throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(output);
gzip.write(data);
gzip.close();
return output.toByteArray();
}

private static byte[] deflate(byte[] data) throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
DeflaterOutputStream deflater = new DeflaterOutputStream(output);
deflater.write(data);
deflater.close();
return output.toByteArray();
}

private static byte[] gzipWithExtraField(byte[] data, byte[] extra) throws IOException {
byte[] standard = gzip(data);
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] header = Arrays.copyOfRange(standard, 0, 10);
header[3] |= 0x04;
output.write(header);
output.write(extra.length & 0xff);
output.write((extra.length >>> 8) & 0xff);
output.write(extra);
output.write(standard, 10, standard.length - 10);
return output.toByteArray();
}
}
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.