標籤:pushback java回推 java-io回推
PushbackInputStream存在的意義就是允許我試探性的讀取資料流,如果不是我們想要的則返還回去,之所以能夠這樣,因為其內部維護了一個pushback buffer緩衝區。建構函式可以指定返回的位元組個數。
緩衝的新應用之一就是回推(pushback)的實現。回推用於輸入資料流,以允許讀取位元組,然後再將它們返回(回推)到流中。PushbackInputStream類實現了這一思想,提供了一種機制,可以“偷窺”來自輸入資料流的內容而不對它們進行破壞。
PushbackInputStream類具有以下建構函式:
PushbackInputStream(InputStream inputStream)PushbackInputStream(InputStream inputStream,int numBytes)
第一種形式建立的流對象允許將一個位元組返回到輸入資料流; 第二種形式建立的流對象具有一個長度為numBytes的回推緩衝,從而允許將多個位元組回推到輸入資料流中。
除了熟悉的來自InputStream的方法外,PushbackInputStream類還提供了unread()方法,如下所示:
void unread(int b)void unread(byte[] buffer)void unread(byte[] buffer,int offset,int numBytes)
第一種形式回推b的低位元組,這會使得後續的read()調用會把這個位元組再次讀取出來。第二種形式回推buffer中的位元組。第三種形式回推buffer中從offset開始的numBytes個位元組。當回推緩衝已滿時,如果試圖回推位元組,就會拋出IOException異常。
用幾個樣本測試一下:
import java.io.ByteArrayInputStream;import java.io.PushbackInputStream;public class test { public static void main(String[] args) throws Exception { String s = "abcdefg"; /* * PushbackInputStream pbin = new PushbackInputStream(in) * 這個建構函式建立的對象一次只能回推一個位元組 */ try (ByteArrayInputStream in = new ByteArrayInputStream(s.getBytes()); PushbackInputStream pbin = new PushbackInputStream(in)) { int n; while ((n = pbin.read()) != -1) { System.out.println((char) n); if(‘b‘ == n) pbin.unread(‘U‘); } } }}
a
b
U
c
d
e
f
g
樣本2:
import java.io.ByteArrayInputStream;import java.io.PushbackInputStream;public class test { public static void main(String[] args) throws Exception { String s = "abcdefg"; /* * PushbackInputStream pbin = new PushbackInputStream(in,3) * 這個建構函式建立的對象一次可以回推一個緩衝 */ try (ByteArrayInputStream in = new ByteArrayInputStream(s.getBytes()); PushbackInputStream pbin = new PushbackInputStream(in, 3)) { int n; byte[] buffer = new byte[3]; while ((n = pbin.read(buffer)) != -1) { System.out.println(new String(buffer)); if(new String(buffer).equals("abc"))pbin.unread(new byte[]{‘M‘,‘N‘,‘O‘}); buffer = new byte[3]; } } }}
abc
def
g
樣本3:
import java.io.ByteArrayInputStream;import java.io.PushbackInputStream;public class test { public static void main(String[] args) throws Exception { String s = "abcdefg"; /* * PushbackInputStream pbin = new PushbackInputStream(in,4) * 這個建構函式建立的對象一次可以回推一個緩衝 */ try (ByteArrayInputStream in = new ByteArrayInputStream(s.getBytes()); PushbackInputStream pbin = new PushbackInputStream(in, 4)) { int n; byte[] buffer = new byte[4]; while ((n = pbin.read(buffer)) != -1) { System.out.println(new String(buffer)); //取回推緩衝中的一部分資料 if(new String(buffer).equals("abcd"))pbin.unread(buffer,2,2); buffer = new byte[4]; } } }}
abcd
cdef
g
樣本4:
import java.io.ByteArrayInputStream;import java.io.PushbackInputStream;public class test { public static void main(String[] args) throws Exception { String s = "abcdefg"; /* * PushbackInputStream pbin = new PushbackInputStream(in,4) * 這個建構函式建立的對象一次可以回推一個緩衝 */ try (ByteArrayInputStream in = new ByteArrayInputStream(s.getBytes()); PushbackInputStream pbin = new PushbackInputStream(in, 4)) { int n; byte[] buffer = new byte[4]; while ((n = pbin.read(buffer)) != -1) { System.out.println(new String(buffer)); //取回推緩衝中的一部分資料 if(new String(buffer).equals("abcd"))pbin.unread(buffer,2,3); buffer = new byte[4]; } } }}
abcd
Exception in thread “main”
java.lang.ArrayIndexOutOfBoundsException at
java.lang.System.arraycopy(Native Method) at
java.io.PushbackInputStream.unread(Unknown Source) at
test.main(test.java:18)
因為abcd從下標2的c開始,數3個字元,越界了!!!所以異常!
注:PushbackInputStream對象會使得InputStream對象(用於建立PushbackInputStream對象)的mark()或reset()方法無效。對於準備使用mark()或reset()方法的任何流來說,都應當使用markSupported()方法進行檢查。
Java IO之PushbackInputStream類