Skip to content

Commit c591d1e

Browse files
committed
Use java functions.
1 parent 24ac6ca commit c591d1e

5 files changed

Lines changed: 40 additions & 71 deletions

File tree

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@
2929

3030
import java.io.IOException;
3131
import java.io.InputStream;
32+
import java.io.UncheckedIOException;
3233
import java.util.Collections;
3334
import java.util.EnumMap;
3435
import java.util.Locale;
3536
import java.util.Map;
37+
import java.util.function.Function;
3638

3739
import org.apache.commons.compress.compressors.CompressorException;
3840
import org.apache.commons.compress.compressors.CompressorStreamFactory;
@@ -74,14 +76,15 @@ final class CommonsCompressDecoderFactory {
7476
/**
7577
* @return lazy decoder for the given IANA token (lower-case).
7678
*/
77-
static IOFunction<InputStream, InputStream> decoder(final String token) {
79+
static Function<InputStream, InputStream> decoder(final String token) {
7880
final String enc = token.toLowerCase(Locale.ROOT);
7981
final CompressorStreamFactory factory = new CompressorStreamFactory();
8082
return in -> {
8183
try {
8284
return factory.createCompressorInputStream(enc, in);
8385
} catch (final CompressorException | LinkageError ex) {
84-
throw new IOException("Unable to decode Content-Encoding '" + enc + '\'', ex);
86+
throw new UncheckedIOException(new IOException(
87+
"Unable to decode Content-Encoding '" + enc + '\'', ex));
8588
}
8689
};
8790
}

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

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
package org.apache.hc.client5.http.entity.compress;
2929

3030
import java.io.IOException;
31+
import java.io.InputStream;
32+
import java.io.UncheckedIOException;
3133
import java.util.Arrays;
3234
import java.util.Collections;
3335
import java.util.EnumMap;
@@ -39,7 +41,6 @@
3941
import org.apache.hc.core5.annotation.Internal;
4042
import org.apache.hc.core5.annotation.ThreadingBehavior;
4143
import org.apache.hc.core5.http.HttpEntity;
42-
import org.brotli.dec.BrotliInputStream;
4344

4445

4546
/**
@@ -62,13 +63,27 @@ private static Map<ContentCoding, Codec> build() {
6263

6364
m.put(ContentCoding.GZIP,
6465
new Codec(
65-
// encoder
6666
org.apache.hc.client5.http.entity.GzipCompressingEntity::new,
67-
ent -> new DecompressingEntity(ent, GZIPInputStream::new)));
67+
ent -> new DecompressingEntity(ent,
68+
in -> {
69+
try {
70+
return new GZIPInputStream(in);
71+
} catch (final IOException ex) {
72+
throw new UncheckedIOException(ex);
73+
}
74+
})));
75+
6876
m.put(ContentCoding.DEFLATE,
6977
new Codec(
7078
org.apache.hc.client5.http.entity.DeflateCompressingEntity::new,
71-
ent -> new DecompressingEntity(ent, DeflateInputStream::new)));
79+
ent -> new DecompressingEntity(ent,
80+
in -> {
81+
try {
82+
return new DeflateInputStream(in);
83+
} catch (final IOException ex) {
84+
throw new UncheckedIOException(ex);
85+
}
86+
})));
7287

7388
/* 2. Commons-Compress extras ---------------------------------- */
7489
if (CommonsCompressSupport.isPresent()) {
@@ -96,10 +111,20 @@ private static Map<ContentCoding, Codec> build() {
96111
if (!m.containsKey(ContentCoding.BROTLI)
97112
&& CommonsCompressDecoderFactory.runtimeAvailable(ContentCoding.BROTLI.token())) {
98113
m.put(ContentCoding.BROTLI,
99-
Codec.decodeOnly(ent ->
100-
new DecompressingEntity(ent, BrotliInputStream::new)));
114+
Codec.decodeOnly(ent -> new DecompressingEntity(ent, in -> {
115+
try {
116+
return (InputStream) Class
117+
.forName("org.brotli.dec.BrotliInputStream")
118+
.getConstructor(InputStream.class)
119+
.newInstance(in);
120+
} catch (final ReflectiveOperationException | LinkageError e) {
121+
throw new UncheckedIOException(
122+
new IOException("Brotli runtime not available", e));
123+
}
124+
})));
101125
}
102126

127+
103128
return Collections.unmodifiableMap(m);
104129
}
105130

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@ public interface Decoder {
5252
* encoded payload (never {@code null})
5353
* @return a new, undecoded {@code HttpEntity}; the caller is responsible
5454
* for consuming / closing its content stream
55-
* @throws IOException if the decoding entity cannot be created or an
56-
* underlying I/O error occurs
5755
*/
58-
HttpEntity wrap(HttpEntity src) throws IOException;
56+
HttpEntity wrap(HttpEntity src);
5957
}
6058

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.io.InputStream;
3232
import java.io.OutputStream;
3333
import java.util.concurrent.locks.ReentrantLock;
34+
import java.util.function.Function;
3435

3536
import org.apache.hc.core5.http.HttpEntity;
3637
import org.apache.hc.core5.http.io.entity.HttpEntityWrapper;
@@ -39,13 +40,13 @@
3940
public class DecompressingEntity extends HttpEntityWrapper {
4041

4142
private static final int BUF_SIZE = 8 * 1024; // 8 KiB buffer
42-
private final IOFunction<InputStream, InputStream> decoder;
43+
private final Function<InputStream, InputStream> decoder;
4344
private final ReentrantLock lock = new ReentrantLock();
4445
private volatile InputStream cached;
4546

4647
public DecompressingEntity(
4748
final HttpEntity src,
48-
final IOFunction<InputStream, InputStream> decoder) {
49+
final Function<InputStream, InputStream> decoder) {
4950
super(src);
5051
this.decoder = Args.notNull(decoder, "Stream decoder");
5152
}

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

Lines changed: 0 additions & 58 deletions
This file was deleted.

0 commit comments

Comments
 (0)