About the available () method of the InputStream class

Source: Internet
Author: User

To read more than one byte at a time, the Inputstream.available () method is often used, which allows you to know how many bytes in the data stream can be read before the read and write operation. It is important to note that if this method is used to read data from a local file, it generally does not encounter problems, but if it is used for network operation, it often encounters some trouble. For example, the socket communication, the Fangmingming sent 1000 bytes, but its own program calls available () method only get 900, or 100, or even 0, feel a bit inexplicable, how can not find the reason. In fact, this is because network traffic is often intermittent, a string of bytes are often divided into several batches to send. The local program call available () method sometimes gets 0, which may be that the other party has not responded, or it may have responded, but the data has not been delivered locally. The other side sends 1000 bytes to you, perhaps into 3 batches, which you will call 3 times available () method to get the total number of data.

Whether or not it is possible to use this method depends on the specific subclass of the abstract class that implements the InputStream. Available. If you do, then you can get the size, and if you don't have it, then you're not getting it. For example, FileInputStream implements the available method, so you can use new byte[in.available ()]; However, the network programming when the socket in the InputStream, there is no way to implement this method, then you can not use this way to create an array.

If you write code like this:

int count = in.available ();    byte New byte [Count];   

In the network operation is often error, because when you call the available () method, the data sent to the send may not have arrived, you get the count is 0.
Need to change to this:

int count = 0;     while (count = = 0)    {= in.available ()   ;   } byte New byte [Count];   In.read (b);

About the available () method of the InputStream 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.