假设我有一个接受InputStream的方法。使用BufferedInputStream包装后对原始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()
编辑: 我想我要问的是很普通的,因此需要大量的工作的。至于我在做什么,其实我并不需要重新设置标记&,所以我找到了一个小小的解决方法。但是,我将在这里留下问题的第二部分,所以请随时尝试此问题:)。