Input and output stream presumably, you've been using it very skillfully, however, we tend to ignore some of the most familiar things around, or we only know to do so, but do not know why, so here to share with you in the network programming in the details of the I/O flow, gossip not much, the following start:
OutputStream, as the name implies: the output stream, its subclasses use the method provided to write data to a certain medium, for example, FileOutputStream writes data to a file, Telnetoutputstream writes data to a network connection, but you hear this and you ask, There is no such thing as Telnetoutputstream in the API, but presumably everyone remembers that there is a Getoutputstream method in the Sokcet class, which is to get the output stream of the connection object and return the OutputStream. The essence of this is more fundamentally the network output stream, which is the power of polymorphism, and if you know how to use these superclass, you know how to use all of these subclasses.
The most basic method of OutputStream is undoubtedly write (int b), although here passed an int, but only receive 0-255 of the integer, we must have seen in the DataOutputStream of a writebyte (int V, this method ensures that a byte is written to the data in byte form and appends the data to the buffer.
When it comes to the buffer zone, we have to mention a flush method, presumably everyone has used it, we know that in OutputStream call write method often need to call a flush method, simply to refresh the buffer, force output data, but why to force output? Give an example of this: Suppose a server is communicating with a client, and the server waits for a message from the client, and if the client sends a 300-byte request, but the default buffer is 1024 bytes, the stream's buffer will wait for more data to arrive. And the client thought its own data has been sent out, will naturally wait for the server's response, the result of the server side because the buffer is not full, so the data has not come, the result has formed a deadlock state. So we often call the flush method, whether you think it is necessary to flush the buffer is important, depending on how you control the stream's reference, you may know that the stream is buffered, or you may not know (for example, whether you want it, System.out is buffered), flush the relative path The order error is a low-cost operation, so don't forget to write it when you need to refresh the output.
InputStream, the input stream, whose essence is exactly the opposite of the output stream, is used to read data from the media, the most basic method is the read () method without parameters, which reads a byte data from the source of the input stream, returns as a 0 to 255 int, and flows Ends with return-1, the Read () method waits and blocks the execution of any subsequent code, knowing that one byte of data is available. But when we write a communication, we often can't use-1 to indicate the end of the data, because we never know when the other party will really end the communication, just like close the connection, the other party has to tell you what under the circumstances he has said, this is related to communication protocols, the next time with the specific analysis.
See more highlights of this column: http://www.bianceng.cn/Programming/Java/