我有一个InputStream传递给一个方法来做一些处理。我将在其他方法中使用相同的InputStream,但在第一次处理后,InputStream出现在方法内部关闭。如何克隆InputStream?
如何克隆InputStream发送给关闭他的方法?还有另一个解决方案?
编辑:关闭InputStream的方法是从lib的外部方法。我没有控制关闭与否。
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";
}
}
你要“重置”的流方法返回之后?也就是说,从一开始就读流? – aioobe 2011-05-07 20:43:51
是的,关闭的InputStream方法返回它编码的字符集。第二种方法是使用第一个方法中的字符集将InputStream转换为字符串。 – 2011-05-07 20:49:14
你应该在这种情况下能够做我在我的回答中描述的内容。 – Kaj 2011-05-07 20:55:43