我有一个 InputStream,我将它传递给一个方法来进行一些处理。我将在其他方法中使用相同的 InputStream,但在第一次处理后,InputStream 似乎在方法内部关闭。
如何克隆 InputStream 以发送到关闭他的方法?还有其他解决方案吗?
编辑:关闭 InputStream 的方法是来自库的外部方法。我无法控制是否关闭。
private String getContent(HttpURLConnection con) {
InputStream content = null;
String charset = "";
try {
content = con.getInputStream();
CloseShieldInputStream csContent = new CloseShieldInputStream(content);
charset = getCharset(csContent);
return IOUtils.toString(content,charset);
} catch (Exception e) {
System.out.println("Error downloading page: " + e);
return null;
}
}
private String getCharset(InputStream content) {
try {
Source parser = new Source(content);
return parser.getEncoding();
} catch (Exception e) {
System.out.println("Error determining charset: " + e);
return "UTF-8";
}
}
最佳答案
如果您只想多次读取相同的信息,并且输入数据足够小以适合内存,您可以将数据从 InputStream
复制到 ByteArrayOutputStream .
然后您可以获得相关的字节数组并打开尽可能多的“克隆”ByteArrayInputStream随你喜欢。
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// Code simulating the copy
// You could alternatively use NIO
// And please, unlike me, do something about the Exceptions :D
byte[] buffer = new byte[1024];
int len;
while ((len = input.read(buffer)) > -1 ) {
baos.write(buffer, 0, len);
}
baos.flush();
// Open new InputStreams using recorded bytes
// Can be repeated as many times as you wish
InputStream is1 = new ByteArrayInputStream(baos.toByteArray());
InputStream is2 = new ByteArrayInputStream(baos.toByteArray());
但如果您确实需要保持原始流打开以接收新数据,那么您将需要跟踪对 close()
的外部调用。您需要防止 close()
以某种方式被调用。
更新(2019 年):
从 Java 9 开始,中间位可以替换为 InputStream.transferTo
:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
input.transferTo(baos);
InputStream firstClone = new ByteArrayInputStream(baos.toByteArray());
InputStream secondClone = new ByteArrayInputStream(baos.toByteArray());
关于java - 如何克隆 InputStream?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5923817/