Java IO之PushbackInputStream類

來源:互聯網
上載者:User

標籤: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類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.