mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
add gzip support for okhttp
This commit is contained in:
@@ -14,6 +14,7 @@ package org.dromara.hutool.http;
|
||||
|
||||
import org.dromara.hutool.core.compress.InflaterInputStream;
|
||||
import org.dromara.hutool.core.map.CaseInsensitiveMap;
|
||||
import org.dromara.hutool.core.reflect.ConstructorUtil;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.Map;
|
||||
@@ -45,6 +46,26 @@ public enum GlobalCompressStreamRegister {
|
||||
compressMap.put("deflate", InflaterInputStream.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 包装原始响应流为指定压缩算法解压流
|
||||
*
|
||||
* @param in 原始响应流
|
||||
* @param contentEncoding 压缩编码,如gzip等
|
||||
* @return 包装后的响应流
|
||||
*/
|
||||
public InputStream wrapStream(final InputStream in, final String contentEncoding){
|
||||
final Class<? extends InputStream> streamClass = get(contentEncoding);
|
||||
if (null != streamClass) {
|
||||
try {
|
||||
return ConstructorUtil.newInstance(streamClass, in);
|
||||
} catch (final Exception ignore) {
|
||||
// 对于构造错误的压缩算法,跳过之
|
||||
}
|
||||
}
|
||||
|
||||
return in;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取解压器
|
||||
*
|
||||
|
@@ -12,7 +12,6 @@
|
||||
|
||||
package org.dromara.hutool.http.client.engine.jdk;
|
||||
|
||||
import org.dromara.hutool.core.reflect.ConstructorUtil;
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
import org.dromara.hutool.http.GlobalCompressStreamRegister;
|
||||
import org.dromara.hutool.http.HttpException;
|
||||
@@ -106,14 +105,6 @@ public class JdkHttpInputStream extends InputStream {
|
||||
return;
|
||||
}
|
||||
|
||||
final String contentEncoding = response.contentEncoding();
|
||||
final Class<? extends InputStream> streamClass = GlobalCompressStreamRegister.INSTANCE.get(contentEncoding);
|
||||
if (null != streamClass) {
|
||||
try {
|
||||
this.in = ConstructorUtil.newInstance(streamClass, this.in);
|
||||
} catch (final Exception ignore) {
|
||||
// 对于构造错误的压缩算法,跳过之
|
||||
}
|
||||
}
|
||||
this.in = GlobalCompressStreamRegister.INSTANCE.wrapStream(this.in, response.contentEncoding());
|
||||
}
|
||||
}
|
||||
|
@@ -40,9 +40,7 @@ public class OkHttpEngine implements ClientEngine {
|
||||
/**
|
||||
* 构造
|
||||
*/
|
||||
public OkHttpEngine() {
|
||||
this.client = new OkHttpClient();
|
||||
}
|
||||
public OkHttpEngine() {}
|
||||
|
||||
@Override
|
||||
public OkHttpEngine init(final ClientConfig config) {
|
||||
|
@@ -12,12 +12,14 @@
|
||||
|
||||
package org.dromara.hutool.http.client.engine.okhttp;
|
||||
|
||||
import org.dromara.hutool.core.io.stream.EmptyInputStream;
|
||||
import org.dromara.hutool.core.util.ObjUtil;
|
||||
import org.dromara.hutool.http.client.Response;
|
||||
import kotlin.Pair;
|
||||
import okhttp3.Headers;
|
||||
import okhttp3.ResponseBody;
|
||||
import org.dromara.hutool.core.io.stream.EmptyInputStream;
|
||||
import org.dromara.hutool.core.util.ObjUtil;
|
||||
import org.dromara.hutool.http.GlobalCompressStreamRegister;
|
||||
import org.dromara.hutool.http.client.Response;
|
||||
import org.dromara.hutool.http.meta.HeaderName;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.Charset;
|
||||
@@ -77,7 +79,9 @@ public class OkHttpResponse implements Response {
|
||||
if(null == body){
|
||||
return EmptyInputStream.INSTANCE;
|
||||
}
|
||||
return body.byteStream();
|
||||
|
||||
return GlobalCompressStreamRegister.INSTANCE.wrapStream(body.byteStream(),
|
||||
this.rawRes.header(HeaderName.CONTENT_ENCODING.getValue()));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Reference in New Issue
Block a user