The downloaded file size of Android development is always greater than the file size of the server.

Source: Internet
Author: User

I recently learned about Android development and wrote a file downloaded from the web server.ProgramThe program can be downloaded successfully, but the downloaded files are found to be different from the file size on the server. According to the many programs on the internet, no problems have been found, finally, I found a bug. The problem occurs in a program written to sdcard:

Byte [] buffer = new byte [4*1024]; while (is. Read (buffer ))! =-1) {// The problem occurs here // is. read (buffer) does not necessarily exactly read 4*1024 bytes. After the test, it is found that the buffer is rarely fully read at a time. Most of the time it is 1440 bytes. I don't know why. Please advise! OS. Write (buffer );}

After this program is modified (as shown below), it finally runs normally. I don't know why some netizens use the above program to run successfully.

Byte [] buffer = new byte [4*1024]; int length = 0; while (length = is. Read (buffer ))! =-1) {OS. Write (buffer, 0, length); system. Out. println (length );}

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.