假設我有一個接受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()
編輯: 我想我要問的是很普通的,因此需要大量的工作的。至於我在做什麼,其實我並不需要重新設置標記&,所以我找到了一個小小的解決方法。但是,我將在這裏留下問題的第二部分,所以請隨時嘗試此問題:)。