Java IO Pushbackinputstream class

Source: Internet
Author: User

The significance of the existence of Pushbackinputstream is to allow me to tentatively read the data stream, and if it is not what we want to return it, it is possible to do so because it internally maintains a pushback buffer. The constructor can specify the number of bytes returned.
One of the new applications for caching is the implementation of the push back (pushback). The push-back is used for the input stream to allow the bytes to be read and then returned (pushed back) into the stream. The Pushbackinputstream class implements this idea and provides a mechanism for "peeping" from the contents of the input stream without destroying them.

The Pushbackinputstream class has the following constructors:

PushbackInputStream(InputStream inputStream)PushbackInputStream(InputStream inputStream,int numBytes)

The first form creates a stream object that allows one byte to be returned to the input stream, and the second form creates a stream object with a numbytes-length push-back cache, allowing multiple bytes to be pushed back into the input stream.

In addition to the familiar approach from InputStream, the Pushbackinputstream class also provides the unread () method, as follows:

void unread(int b)void unread(byte[] buffer)void unread(byte[] buffer,int offset,int numBytes)

The first form pushes back the low byte of B, which causes the subsequent read () call to fetch the byte again. The second form pushes back the bytes in buffer. The third form pushes back the NumBytes bytes in buffer starting with offset. When the rollback cache is full, an IOException exception is thrown if an attempt is made to push back the bytes.

Test it with a few examples:
  

Import Java.io.bytearrayinputstream;import Java.io.PushbackInputStream; Public classTest { Public Static void Main(string[] args) throws Exception {String s ="ABCDEFG";/* * Pushbackinputstream Pbin = new Pushbackinputstream (in) * The object created by this constructor can only be pushed back one byte at a time */        Try(Bytearrayinputstreaminch=NewBytearrayinputstream (S.getbytes ()); Pushbackinputstream Pbin =NewPushbackinputstream (inch)) {intN while((n = pbin.read ())! =-1) {System. out. println (Char) n);if(' B '= = N) pbin.unread (' U '); }        }    }}

A
B
U
C
D
E
F
G

Example 2:
  

Import Java.io.bytearrayinputstream;import Java.io.PushbackInputStream; Public classTest { Public Static void Main(string[] args) throws Exception {String s ="ABCDEFG";/* * Pushbackinputstream Pbin = new Pushbackinputstream (in,3) * The object created by this constructor can be pushed back one cache at a time */        Try(Bytearrayinputstreaminch=NewBytearrayinputstream (S.getbytes ()); Pushbackinputstream Pbin =NewPushbackinputstream (inch,3)) {intNbyte[] buffer =New byte[3]; while((n = pbin.read (buffer))! =-1) {System. out. println (NewString (buffer));if(NewString (buffer). Equals ("ABC")) Pbin.unread (New byte[]{' M ',' N ',' O '}); Buffer =New byte[3]; }        }    }}

Abc
Def
G

Example 3:
  

Import Java.io.bytearrayinputstream;import Java.io.PushbackInputStream; Public classTest { Public Static void Main(string[] args) throws Exception {String s ="ABCDEFG";/* * Pushbackinputstream Pbin = new Pushbackinputstream (in,4) * The object created by this constructor can be pushed back one cache at a time */        Try(Bytearrayinputstreaminch=NewBytearrayinputstream (S.getbytes ()); Pushbackinputstream Pbin =NewPushbackinputstream (inch,4)) {intNbyte[] buffer =New byte[4]; while((n = pbin.read (buffer))! =-1) {System. out. println (NewString (buffer));//Retrieving part of the data from the push cache                if(NewString (buffer). Equals ("ABCD")) Pbin.unread (buffer,2,2); Buffer =New byte[4]; }        }    }}

Abcd
Cdef
G

Example 4:
  

Import Java.io.bytearrayinputstream;import Java.io.PushbackInputStream; Public classTest { Public Static void Main(string[] args) throws Exception {String s ="ABCDEFG";/* * Pushbackinputstream Pbin = new Pushbackinputstream (in,4) * The object created by this constructor can be pushed back one cache at a time */        Try(Bytearrayinputstreaminch=NewBytearrayinputstream (S.getbytes ()); Pushbackinputstream Pbin =NewPushbackinputstream (inch,4)) {intNbyte[] buffer =New byte[4]; while((n = pbin.read (buffer))! =-1) {System. out. println (NewString (buffer));//Retrieving part of the data from the push cache                if(NewString (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)

Since ABCD starts with the C of subscript 2, the number of 3 characters, out of bounds!!! So unusual!

Note : The Pushbackinputstream object makes the mark () or Reset () method of the InputStream object (used to create the Pushbackinputstream object) invalid. You should use the marksupported () method for any flow that is ready to use the mark () or Reset () method.

Java IO Pushbackinputstream class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.