PushbackInputStream example

xiaoxiao2021-04-04  293

/ *

A novel use of buffering is to implement Pushback. Pushback is used to enter the flow allowed byte and then return (ie "push back") to the stream. The PushbackInputStream class implements this idea. It provides a mechanism to "peep" what is generated in the case where it is not damaged.

PushbackInputStream has two constructor:

PushbackInputStream (InputStream InputStream)

PushbackInputStream (InputStream InputStream, Int Numbytes)

The first form creates a stream that allows one byte to push back to the input stream. The second form creates a pushback buffer with the NumBytes length buffer. It allows multiple bytes to push back to the input stream. In addition to having the same method as INPUTSTREAM, PushbackInputStream provides a unread () method, indicating as follows:

Void Unread (int CH)

Void unread (byte buffer ")

Void Unread (Byte Buffer, int offset, int number

The first form is pushed back to the low byte of the CH, which will be the next byte returned by the READ () method. The second form returns bytes in the Buffer buffer. The third form is pushed back to the NumChars bytes from the OFFSET in Buffer. If you try to return a byte when the pushback buffer is full, the IOException will be triggered. Java 2 made some small modifications to PushbackInputStream: it implements the SKIP () method.

* /

// DemonStrate unread (). Import java.io. *; Class pushbackinputstreamdemo {public static void main (string [] args) throws ioException {string s = "if (a == 4) a = 0; / n"; byte BUF [] = s.getbytes (); byterrayinputstream in = new byterrayinputstream (buf); PushbackInputStream f = new pushbackinputstream (in); int C; while ((c = f.read ())! = - 1) {switch (Switch) c) {case '=': IF ((c = f.read ()) == '=') {system.out.print (". Eq.");} else {system.out.print ("< - "); f.unread (c);} Break; default: system.out.print ((char) c); Break;}}}}}

转载请注明原文地址:https://www.9cbs.com/read-131930.html

New Post(0)