Solve the Problem of incomplete data reading in inputstream

Source: Internet
Author: User

If you want to read this article, I hope you have some knowledge about inputstream stream reading.

When you need to use inputstream to obtain data, you need to read the data in inputstream.

Inputstream has three methods to read streams: Read (), read (byte [] B), read (byte [] B, int off, int Len ). When reading data from a data stream, the graph is simple and the inputstream. Read () method is often used. This method reads only one byte from the stream at a time, and the efficiency is very low. A better way is to use the inputstream. Read (byte [] B) or inputstream. Read (byte [] B, int off, int Len) method to read multiple bytes at a time. However, none of these methods can completely read the data in the stream at a time or do not know whether the data is completely read.

In the case of the above problem, someone suggested using the inputstream. Available () method on the Internet. This method can first know the number of bytes in the data stream before reading the write operation. It is important to note that this method is generally used to read data from a local file, but it is often used for network operations. This is because network communication is often intermittent, and a string of bytes is usually sent in batches. For example, if a local program calls the available () method and sometimes gets 0, sometimes 50, or 100, the value is 100 in most cases. This may be because the other party has not responded or the other party has responded, but the data has not yet been delivered to the local device. It may arrive in three batches, maybe in two batches, or at one time.

If you write as follows, an error may occur because of the following:

    int count = in.available();    byte[] b = new byte[count];;
So someone on the Internet proposed to change it to the following:

     int count = 0;      while (count == 0) {         count = in.available();     }     byte[] b = new byte[count];;</span></span>
The following method is used together with the above:

Byte [] B = new byte [count]; int readcount = 0; // number of successfully read bytes while (readcount <count) {readcount + = in. read (bytes, readcount, Count-readcount );}
The following code can be used to read count bytes, because the read method cannot read the number of bytes you want to read. The code below is okay, but the above section is faulty. When the condition in the while loop is counted = 0, it is obvious that sometimes the data is divided into two wholesale items, if the first batch of 100 of data is 20 and the second batch is 80, the following method can only read 20, but it is still incomplete. It may be okay for a test at the moment, but your inputstream receives data too quickly or frequently, so the problem arises.

I have encountered the problem mentioned above. The final solution is:

Let's talk about the solution: Wait 0.2 seconds for the first data to be received, and all the complete data will be sent later.

     byte[] b= new byte[1024];        int count = 0;     if(inputStream.available()>0 == false){      continue;     }else{      Thread.sleep(200);      }     count =;
When segmentation occurs: The first inputstream. available () may be 20, but after 0.5 seconds of sleep, run COUNT = inputstream. when reading (B);, the input stream should be complete, and unlike the two missing input streams executed in two segments, although the data is sent in two segments, but the code behind sleep will only be executed once. You can set it to 0.2 or 0.3 if you are afraid of incomplete reception within 0.5 seconds of sleep. Currently, I use this method to read data completely.

If you have any ideas or questions about the above method, you are welcome to provide suggestions. Thank you!

Solve the Problem of incomplete data reading in inputstream

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: 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.