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 — JdkZlibDecompressor: Inline zlib decompressor base · netty/netty@553478f

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • codec-compression/src
    • main/java/io/netty/handler/codec/compression
      • JdkZlibDecompressor.java
      • ZlibDecompressor.java
    • test/java/io/netty/handler/codec/compression
      • JdkZlibDecompressorTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎codec-compression/src/main/java/io/netty/handler/codec/compression/JdkZlibDecompressor.java‎

Copy file name to clipboardExpand all lines: codec-compression/src/main/java/io/netty/handler/codec/compression/JdkZlibDecompressor.java
+30-12Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
import io.netty.buffer.ByteBuf;
1919
import io.netty.buffer.ByteBufAllocator;
2020
import io.netty.buffer.ByteBufUtil;
21+
import io.netty.util.internal.ObjectUtil;
2122
import io.netty.util.internal.UnstableApi;
2223

24+
import java.util.Objects;
2325
import java.util.zip.CRC32;
2426
import java.util.zip.DataFormatException;
2527
import java.util.zip.Deflater;
@@ -29,14 +31,16 @@
2931
* Decompress a {@link ByteBuf} using the inflate algorithm.
3032
*/
3133
@UnstableApi
32-
public final class JdkZlibDecompressor extends ZlibDecompressor {
34+
public final class JdkZlibDecompressor extends InputBufferingDecompressor {
3335
private static final int FHCRC = 0x02;
3436
private static final int FEXTRA = 0x04;
3537
private static final int FNAME = 0x08;
3638
private static final int FCOMMENT = 0x10;
3739
private static final int FRESERVED = 0xE0;
3840

3941
private Inflater inflater;
42+
private final int maxAllocation;
43+
private final byte[] dictionary;
4044

4145
// GZIP related
4246
private final ByteBufChecksum crc;
@@ -67,7 +71,9 @@ private enum GzipState {
6771
private boolean inputBufferInInflater;
6872

6973
JdkZlibDecompressor(Builder builder, ByteBufAllocator allocator) {
70-
super(builder, allocator);
74+
super(allocator);
75+
maxAllocation = builder.maxAllocation;
76+
dictionary = builder.dictionary;
7177
this.decompressConcatenated = builder.decompressConcatenated;
7278
switch (builder.wrapper) {
7379
case GZIP:
@@ -454,34 +460,46 @@ public static Builder builder() {
454460
}
455461

456462
@UnstableApi
457-
public static final class Builder extends AbstractZlibDecompressorBuilder {
463+
public static final class Builder extends Decompressor.AbstractDecompressorBuilder {
464+
private ZlibWrapper wrapper = ZlibWrapper.ZLIB;
465+
private byte[] dictionary;
466+
private int maxAllocation = 1024 * 1024;
458467
boolean decompressConcatenated;
459468

460469
Builder() {
461470
}
462471

463472
/**
464-
* {@inheritDoc}
473+
* Set the wrapper format for the deflated data. Defaults to {@link ZlibWrapper#ZLIB}.
474+
*
475+
* @param wrapper The wrapper format
476+
* @return This builder
465477
*/
466-
@Override
467478
public Builder wrapper(ZlibWrapper wrapper) {
468-
return (Builder) super.wrapper(wrapper);
479+
this.wrapper = Objects.requireNonNull(wrapper, "wrapper");
480+
return this;
469481
}
470482

471483
/**
472-
* {@inheritDoc}
484+
* Set the preset dictionary to use. Defaults to no dictionary.
485+
*
486+
* @param dictionary The dictionary
487+
* @return This builder
473488
*/
474-
@Override
475489
public Builder dictionary(byte[] dictionary) {
476-
return (Builder) super.dictionary(dictionary);
490+
this.dictionary = dictionary;
491+
return this;
477492
}
478493

479494
/**
480-
* {@inheritDoc}
495+
* Set the maximum output buffer size. Defaults to 1M.
496+
*
497+
* @param maxAllocation The maximum output buffer size.
498+
* @return This builder
481499
*/
482-
@Override
483500
public Builder maxAllocation(int maxAllocation) {
484-
return (Builder) super.maxAllocation(maxAllocation);
501+
this.maxAllocation = ObjectUtil.checkPositiveOrZero(maxAllocation, "maxAllocation");
502+
return this;
485503
}
486504

487505
public Builder decompressConcatenated(boolean decompressConcatenated) {
Collapse file

‎codec-compression/src/main/java/io/netty/handler/codec/compression/ZlibDecompressor.java‎

Copy file name to clipboardExpand all lines: codec-compression/src/main/java/io/netty/handler/codec/compression/ZlibDecompressor.java
-74Lines changed: 0 additions & 74 deletions
This file was deleted.
Collapse file

‎codec-compression/src/test/java/io/netty/handler/codec/compression/JdkZlibDecompressorTest.java‎

Copy file name to clipboardExpand all lines: codec-compression/src/test/java/io/netty/handler/codec/compression/JdkZlibDecompressorTest.java
+39Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
3939
import static org.junit.jupiter.api.Assertions.assertEquals;
4040
import static org.junit.jupiter.api.Assertions.assertThrows;
41+
import static org.junit.jupiter.api.Assertions.fail;
4142
import static org.junit.jupiter.api.Assumptions.assumeTrue;
4243
@ParameterizedClass
4344
@MethodSource("wrappers")
@@ -119,6 +120,44 @@ public void testMalformedInputDoesNotLeakOutput() {
119120
}
120121
}
121122

123+
@Test
124+
public void testNegativeMaxAllocationRejected() {
125+
assertThrows(IllegalArgumentException.class, () -> JdkZlibDecompressor.builder().maxAllocation(-1));
126+
}
127+
128+
@Test
129+
public void testDirectInputRetainedAcrossOutputChunks() throws Exception {
130+
assumeTrue(wrapper == ZlibWrapper.ZLIB);
131+
byte[] expected = new byte[8192];
132+
Arrays.fill(expected, (byte) 'a');
133+
byte[] compressed = deflate(expected);
134+
CompositeByteBuf output = ByteBufAllocator.DEFAULT.compositeBuffer();
135+
try (Decompressor decompressor = JdkZlibDecompressor.builder().maxAllocation(1)
136+
.build(ByteBufAllocator.DEFAULT)) {
137+
decompressor.status();
138+
decompressor.addInput(Unpooled.directBuffer(compressed.length).writeBytes(compressed));
139+
for (;;) {
140+
switch (decompressor.status()) {
141+
case NEED_OUTPUT:
142+
output.addComponent(true, decompressor.takeOutput());
143+
break;
144+
case COMPLETE:
145+
byte[] actual = new byte[output.readableBytes()];
146+
output.readBytes(actual);
147+
assertArrayEquals(expected, actual);
148+
return;
149+
case NEED_INPUT:
150+
fail("Decompressor requested more input before reaching the stream end");
151+
break;
152+
default:
153+
throw new AssertionError("Unknown decompressor status");
154+
}
155+
}
156+
} finally {
157+
output.release();
158+
}
159+
}
160+
122161
@Test
123162
public void testBadSecondGzipMagicRejected() throws Exception {
124163
assumeTrue(wrapper == ZlibWrapper.GZIP);

0 commit comments

Comments
0 (0)
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.