我正在使用新的java.net.http
类来处理异步 HTTP 请求+响应交换,我正在尝试找到一种方法让 BodySubscriber 处理不同的编码类型(例如 gzip)。
然而,映射一个BodySubsriber<InputStream>
以便底层流被包装GZIPInputStream
(当在响应标头中找到“Content-Encoding:gzip”时)会导致挂起。没有例外,只是完全停止活动。
映射的代码BodySubscriber
看起来像这样:
private HttpResponse.BodySubscriber<InputStream> gzippedBodySubscriber(
HttpResponse.ResponseInfo responseInfo) {
return HttpResponse.BodySubscribers.mapping(
HttpResponse.BodySubscribers.ofInputStream(),
this::decodeGzipStream);
}
private InputStream decodeGzipStream(InputStream gzippedStream) {
System.out.println("Entered decodeGzipStream method.");
try {
InputStream decodedStream = new GZIPInputStream(gzippedStream);
System.out.println(
"Created GZIPInputStream to handle response body stream.");
return decodedStream;
} catch (IOException ex) {
System.out.println("IOException occurred while trying to create GZIPInputStream.");
throw new UncheckedIOException(ex);
}
}
接收到具有“gzip”编码的 HTTP 响应会导致控制台显示以下内容:
进入EncodedBodyHandler.apply方法。
进入decodeGzipStream方法。
没有看到更多内容,因此调用后的行GZIPInputStream
构造函数永远不会被执行。
有谁知道为什么要尝试包装InputStream
from a BodySubscriber<InputStream>
in a GZIPInputStream
是挂吗?
注意:未编码(原始文本)HTTP 响应主体的等效方法仅包含对BodySubscribers.ofInputStream()
没有映射,这使得可以毫无问题地接收和显示响应。
EDIT: JDK-8217264从 JDK13 开始已修复
这确实是一个错误。我已登录JDK-8217264。我可以建议两种解决方法:
解决方法一
不使用BodySubscribers.mapping
- 但要改变InputStream
into a GZIPInputStream
获取 Http Response body 后:
GZIPInputStream gzin = new GZIPInputStream(resp.getBody());
解决方法二
让映射函数返回一个Supplier<InputStream>
相反,要注意不要创建GZIPInputStream
until Supplier::get
叫做
static final class ISS implements Supplier<InputStream> {
final InputStream in;
GZIPInputStream gz;
ISS(InputStream in) {
this.in = in;
}
public synchronized InputStream get() {
if (gz == null) {
try {
gz = new GZIPInputStream(in);
} catch (IOException t) {
throw new UncheckedIOException(t);
}
}
return gz;
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)