Pushbackinputstream (push back the input stream) Example

Source: Internet
Author: User
/* A novel buffer usage is pushback ). Pushback is used to input the stream to allow the byte to be read and then return (that is, "Push Back") to the stream. The pushbackinputstream class implements this idea. It provides a mechanism to "peat" What is generated by the input stream without being damaged. Pushbackinputstream has two constructors: pushbackinputstream (inputstream) pushbackinputstream (inputstream, int numbytes). The first form creates a stream object that allows a byte to be pushed back to the input stream. The second form creates a push-back buffer stream with a numbytes length buffer. It allows multiple bytes to be pushed back to the input stream. In addition to the same method as inputstream, pushbackinputstream provides the unread () method, which indicates the following: void unread (INT ch) void unread (byte buffer []) void unread (byte buffer, int offset, int numchars) the first form pushes back the low byte of CH, which will be the next byte returned by the subsequent call of the read () method. The second form returns the bytes in the buffer. The third form pushes back the numchars bytes starting from the offset in the buffer. If a byte is returned when the push-back buffer is full, an ioexception is thrown. Java 2 has made some minor changes 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 ();
Bytearrayinputstream in = new bytearrayinputstream (BUF );
Pushbackinputstream F = new pushbackinputstream (in );
Int C;
While (C = f. Read ())! =-1 ){
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;
}
}
}
}

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.