【发布时间】:2014-02-12 07:52:16
【问题描述】:
假设我有一个接收 InputStream 的方法。
此方法需要用 BufferedInputStream 包装此 InputStream 以使用其标记和重置功能。但是,传入的 InputStream 可能仍会被方法的调用者使用。
public static void foo(InputStream is) throws Exception {
BufferedInputStream bis = new BufferedInputStream(is);
int b = bis.read();
}
public static void main(String[] args) {
try {
InputStream is = new FileInputStream(someFile);
foo(is);
int b = is.read(); // return -1
}catch (Exception e) {
e.printStackTrace();
}
}
我的问题是:当 BufferedInputStream 被读取(或初始化)时,原始 InputStream 到底发生了什么?
我的假设是,如果 BufferedInputStream 被读取,原始 InputStream 也会向前移动。但是,在调试我的代码后,我发现 InputStream 在读取时会返回 -1。
如果在这样的过程之后原始的 InputStream 不可读,我应该如何实现我的目的:
InputStream is;
foo(is); // Method only take in generic InputStream object
// Processing of the passed in InputStream object require mark and reset functionality
int b = is.read(); // Return the next byte after the last byte that is read by foo()
编辑: 我想我所要求的内容很笼统,因此需要做很多工作。至于我正在做的事情,我实际上不需要完整的标记和重置功能,所以我找到了一个小工作。但是,我将把问题的第二部分留在这里,所以请随意尝试这个问题:)。
【问题讨论】:
标签: java inputstream bufferedinputstream