forked from fanruan/finekit
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
1.6 KiB
56 lines
1.6 KiB
6 years ago
|
package com.fanruan.api.net.http.rs;
|
||
|
|
||
|
import com.fr.third.org.apache.http.HttpEntity;
|
||
|
import com.fr.third.org.apache.http.client.methods.CloseableHttpResponse;
|
||
|
|
||
|
import java.io.ByteArrayInputStream;
|
||
|
import java.io.ByteArrayOutputStream;
|
||
|
import java.io.IOException;
|
||
|
import java.io.InputStream;
|
||
|
|
||
|
/**
|
||
|
* 流响应解析器
|
||
|
*
|
||
|
* @author vito
|
||
|
* @date 2019-07-14
|
||
|
*/
|
||
|
public class StreamResponseHandle extends BaseHttpResponseHandle<ByteArrayInputStream> {
|
||
|
|
||
|
public static final StreamResponseHandle DEFAULT = new StreamResponseHandle();
|
||
|
private static final int BUFFER_LENGTH = 8000;
|
||
|
|
||
|
public StreamResponseHandle() {
|
||
|
}
|
||
|
|
||
|
public StreamResponseHandle(String encoding) {
|
||
|
super(encoding);
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public ByteArrayInputStream parse(CloseableHttpResponse response) throws IOException {
|
||
|
InputStream in = null;
|
||
|
try {
|
||
|
HttpEntity entity = response.getEntity();
|
||
|
if (entity != null) {
|
||
|
in = entity.getContent();
|
||
|
byte[] buff = new byte[BUFFER_LENGTH];
|
||
|
int bytesRead;
|
||
|
ByteArrayOutputStream bao = new ByteArrayOutputStream();
|
||
|
while ((bytesRead = in.read(buff)) != -1) {
|
||
|
bao.write(buff, 0, bytesRead);
|
||
|
}
|
||
|
byte[] data = bao.toByteArray();
|
||
|
return new ByteArrayInputStream(data);
|
||
|
}
|
||
|
return null;
|
||
|
} finally {
|
||
|
if (response != null) {
|
||
|
response.close();
|
||
|
}
|
||
|
if (in != null) {
|
||
|
in.close();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|