public class InputStreamCache {
/**
* 将InputStream中的字节保存到ByteArrayOutputStream中。
*/
private ByteArrayOutputStream byteArrayOutputStream = null;
public InputStreamCache(InputStream inputStream) {
if (Objects.isNull(inputStream)) {
return;
}
byteArrayOutputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
try {
while ((len = inputStream.read(buffer)) > -1) {
byteArrayOutputStream.write(buffer, 0, len);
}
byteArrayOutputStream.flush();
} catch (IOException e) {
log.error(e.getMessage(), e);
}
}
public InputStream getInputStream() {
if (Objects.isNull(byteArrayOutputStream)) {
return null;
}
return new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
}
}
网友评论