Skip to content

Commit c1658f3

Browse files
committed
Made complete reflections
1 parent 654d8bb commit c1658f3

7 files changed

Lines changed: 35 additions & 15 deletions

File tree

httpclient5/src/main/java/org/apache/hc/client5/http/async/methods/DeflatingBrotliEntityProducer.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,11 @@ public int available() {
144144
if (pendingOut != null && pendingOut.hasRemaining() || pendingTrailers != null) {
145145
return 1;
146146
}
147+
try {
148+
if (AsyncBrotli.encHasMoreOutput(encoder)) {
149+
return 1;
150+
}
151+
} catch (final Exception ignore) { }
147152
final int up = upstream.available();
148153
return (state != State.STREAMING || up > 0) ? 1 : 0;
149154
}
@@ -161,7 +166,7 @@ public void produce(final DataStreamChannel channel) throws IOException {
161166
return;
162167
}
163168
if (pendingTrailers == null) {
164-
pendingTrailers = Collections.<Header>emptyList();
169+
pendingTrailers = Collections.emptyList();
165170
}
166171
channel.endStream(pendingTrailers);
167172
pendingTrailers = null;
@@ -208,7 +213,7 @@ public int write(final ByteBuffer src) throws IOException {
208213

209214
@Override
210215
public void endStream() throws IOException {
211-
endStream(Collections.<Header>emptyList());
216+
endStream(Collections.emptyList());
212217
}
213218

214219
@Override

httpclient5/src/main/java/org/apache/hc/client5/http/async/methods/InflatingBrotliDataConsumer.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import org.apache.hc.core5.http.HttpException;
3535
import org.apache.hc.core5.http.nio.AsyncDataConsumer;
3636
import org.apache.hc.core5.http.nio.CapacityChannel;
37-
import org.apache.hc.core5.util.Asserts;
3837

3938
/**
4039
* {@code AsyncDataConsumer} that inflates a Brotli-compressed byte stream and forwards
@@ -119,16 +118,29 @@ public void consume(final ByteBuffer src) throws IOException {
119118
@Override
120119
public void streamEnd(final List<? extends Header> trailers) throws IOException, HttpException {
121120
try {
122-
pump();
123-
final String st = AsyncBrotli.decStatusName(decoder);
124-
Asserts.check("DONE".equals(st) || !AsyncBrotli.decHasOutput(decoder),
125-
"Truncated brotli stream");
121+
for (; ; ) {
122+
final String st = AsyncBrotli.decStatusName(decoder);
123+
if ("DONE".equals(st)) {
124+
break;
125+
}
126+
if ("NEEDS_MORE_INPUT".equals(st) || "OK".equals(st)) {
127+
AsyncBrotli.decPush(decoder, 0);
128+
pump();
129+
continue;
130+
}
131+
if ("NEEDS_MORE_OUTPUT".equals(st)) {
132+
pump();
133+
continue;
134+
}
135+
throw new IOException("Brotli stream corrupted");
136+
}
126137
downstream.streamEnd(trailers);
127138
} catch (final Exception ex) {
128139
throw new IOException("Brotli stream end failed", ex);
129140
}
130141
}
131142

143+
132144
@Override
133145
public void releaseResources() {
134146
AsyncBrotli.decDestroy(decoder);

httpclient5/src/main/java/org/apache/hc/client5/http/entity/BrotliDecompressingEntity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public BrotliDecompressingEntity(final HttpEntity entity) {
4949

5050
public static boolean isAvailable() {
5151
try {
52-
Class.forName("org.brotli.dec.BrotliInputStream");
52+
Class.forName("com.aayushatharva.brotli4j.decoder.BrotliInputStream");
5353
return true;
5454
} catch (final ClassNotFoundException | NoClassDefFoundError e) {
5555
return false;

httpclient5/src/main/java/org/apache/hc/client5/http/entity/BrotliInputStreamFactory.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@
2828

2929
import java.io.IOException;
3030
import java.io.InputStream;
31-
32-
import com.aayushatharva.brotli4j.decoder.BrotliInputStream;
31+
import java.lang.reflect.Constructor;
3332

3433
import org.apache.hc.core5.annotation.Contract;
3534
import org.apache.hc.core5.annotation.ThreadingBehavior;
@@ -60,6 +59,13 @@ public static BrotliInputStreamFactory getInstance() {
6059

6160
@Override
6261
public InputStream create(final InputStream inputStream) throws IOException {
63-
return new BrotliInputStream(inputStream);
62+
try {
63+
// Prefer brotli4j if present
64+
final Class<?> cls = Class.forName("com.aayushatharva.brotli4j.decoder.BrotliInputStream");
65+
final Constructor<?> c = cls.getConstructor(InputStream.class);
66+
return (InputStream) c.newInstance(inputStream);
67+
} catch (final ReflectiveOperationException | NoClassDefFoundError e) {
68+
throw new IOException("Brotli decoder not available", e);
69+
}
6470
}
6571
}

httpclient5/src/main/java/org/apache/hc/client5/http/entity/compress/CommonsCompressCodecFactory.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ final class CommonsCompressCodecFactory {
7373
private static final String CC_DEFLATE64 = "org.apache.commons.compress.compressors.deflate64.Deflate64CompressorInputStream";
7474

7575
// Helper libs
76-
private static final String H_BROTLI = "org.brotli.dec.BrotliInputStream";
7776
private static final String H_ZSTD = "com.github.luben.zstd.ZstdInputStream";
7877
private static final String H_XZ = "org.tukaani.xz.XZInputStream";
7978

@@ -153,7 +152,7 @@ static boolean runtimeAvailable(final ContentCoding coding) {
153152
}
154153
switch (coding) {
155154
case BROTLI:
156-
return isPresent(CC_BROTLI) && isPresent(H_BROTLI);
155+
return isPresent(CC_BROTLI);
157156
case ZSTD:
158157
return isPresent(CC_ZSTD) && isPresent(H_ZSTD);
159158
case XZ:

httpclient5/src/main/java/org/apache/hc/client5/http/entity/compress/ContentCodecRegistry.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ private static Map<ContentCoding, Codec> build() {
8080
// 2) Commons-Compress (optional) — reflectively wired
8181
if (CommonsCompressSupport.isPresent()) {
8282
for (final ContentCoding c : Arrays.asList(
83-
ContentCoding.BROTLI,
8483
ContentCoding.ZSTD,
8584
ContentCoding.XZ,
8685
ContentCoding.LZMA,

pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@
6464
<maven.compiler.target>1.8</maven.compiler.target>
6565
<httpcore.version>5.4-alpha1</httpcore.version>
6666
<log4j.version>2.25.0</log4j.version>
67-
<brotli.version>0.1.2</brotli.version>
6867
<brotli4j.version>1.20.0</brotli4j.version>
6968
<conscrypt.version>2.5.2</conscrypt.version>
7069
<ehcache.version>3.10.8</ehcache.version>

0 commit comments

Comments
 (0)