"Give you a second chance"--a brief discussion on Pushbackinputstream

Source: Internet
Author: User
Tags array character set documentation http request return
Stream Jungleford says
Pushbackinputstream and Pushbackreader are two of the more confusing classes in the Java I/O system, which I didn't know before, until one day I saw the comments and some information from the previous Jellyfish Java version of Cow Zms. This is a few months ago, and these days to write about the summary of serialization just think this is also a good topic.

A hook that allows you to renege

Java I/O system is a typical implementation of the decorator mode, it takes inputstream/outputstream as the basic core, through the inheritance relation, unceasingly adds the new function for this core, such as file flow, buffering, encryption and decryption and so on. If you are interested in the I/O system design pattern, you can refer to an article on Developerworks: Look at the design pattern from the Java class Library. Java I/o default is not buffered flow, the so-called "buffer" is the first to get from the stream of a byte sequence is called the buffer of an internal byte array, then you can get this whole block of byte data at once, no buffered stream can read only one byte, one byte at a time. There are two special input streams to implement the buffering function, one is our common bufferedinputstream, like reading files we often
Bufferedinputstream in = new Bufferedinputstream (New FileInputStream ("DataFile")), while ((b = in.read ())!=-1) {...} In.close ();
This is a code snippet that we can use almost without any JDK documentation, and think about the meaning of a bufferedinputstream when you write it.    The other is the pushbackinputstream we don't see (its corresponding character stream pattern is pushbackreader). In the usual state, "flow" means "one-time", that is, after you have done an operation of its state has changed, such as reading, whether it is a file or socket, you read the process of a potential "reading pointer" The same thing is moving, You can't relocate after reading (of course randomaccessfile is another case), and if you've ever wondered why a resultset in a database operation can't get it for the second time, this might be an explanation. But luckily Pushbackinputstream gave us the chance to read the second time. We first distinguish between the "listening" and "interception" concept, "listening" is to get the message copy, the original message does not make any changes to the destination, and "interception" is the message "seizure" down, do not let it automatically transferred to the target, Instead of doing some processing before forwarding to the target (if it is a network security professional background knowledge, probably know that "listening" is a "confidential" attack, and "interception" is not only "confidentiality" or "integrity" attacks). Some friends probably have some understanding of the term hook, it is a kind of Windows message processing mechanism, seems to be a message interception means, but I am ignorant of Windows programming//shy; Also, if you are familiar with the servlet, can also find a processing mechanism such as filter, before forwarding each HTTP request/response, first play a little trick in it, determine which to forward, which shielding off, which is "intercept" it. Through the above introduction, we might as well consider the Pushbackinputstream as a "interception" of the inflow, the most important method is unread:
public void unread (int b) throws ioexceptionpublic void unread (byte[] b) throws ioexceptionpublic void unread (byte[] B, in t off, int len) throws IOException
We can imagine that Pushbackinputstream has a buffer built into it (in fact, you can find this protected byte array from its source code), and when the lower stream comes in, it flows into the buffer, and you have a chance to trick it before you "return it" to the stream. , and then "back off" with the unread method, the contents of the buffer has been read (generally is not changed, of course, you can also change it, it loses the meaning of "to Zhao", because it is not "finished" and then inserted into the head of the stream, the next time reading is the remaining part of the stream plus from the buffer "return "Part of it.    The above three unread methods represent the "return" of a byte, a byte array, and a specified portion of a byte array, respectively, from the buffer. Pushbackinputstream is the process of binary flow, the character stream corresponds to the pushbackreader.

What's the use?

Learned to compile the words are easy to understand, such as from left to right scan the character stream "for (int i=0;i<10;i++)", scan to "for" is not it can be said to be a keyword? No, maybe it's "for1" behind it, and that's a variable instead of a keyword, knowing that "(" it suddenly dawned on me, oh, I can safely say "see the FOR keyword", but "(") "(") it has to be returned to the input stream, because you need to continue the This compensation mechanism is more necessary in context-related languages. Also, when parsing an HTML document, I need to decide which character set to use according to the "CharSet" attribute of its "meta" tag, but the HTML is not "charset" but "
Resources
Java network programming. by Elliotte R. Harold zms in the jellyfish post. by Zms (helpless, the water wood Tsinghua has not been open to Off-campus) JDK 1.4.2 documentation. by java.sun.com

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.