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 );}