Search code examples
javainputstreamokhttpfileoutputstream

Binary File Download using OkHTTP Client get corrupted


I am trying to download binary file using OkHttp with progress.
The file get download properly when BUFFER_SIZE is 1.
However file get corrupted when I set BUFFER_SIZE to 1024.
With BUFFER_SIZE set to 1 file takes long time to download

Below is the code snippet:

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class DownloadTest {

    public static String url = "https://cdn.pixabay.com/photo/2017/02/06/12/34/reptile-2042906_1280.jpg";

    public static void main(String[] args) throws Exception {
        OkHttpClient client = new OkHttpClient();
        Call call = client.newCall(new Request.Builder().url(url).get().build());
        Response response = call.execute();

        System.out.println("" + response.headers().toString());
        System.out.println("" + response.body().contentLength());
        InputStream inputStream = response.body().byteStream();

        float contentLength = (float) response.body().contentLength();

        OutputStream fileOutputStream = new FileOutputStream(new File("myfile.jpg"));

        System.out.println("writing file " + contentLength);

        float downloaded = 0;

        /**
         * IF BUFFER_SIZE IS 1 file is downloaded properly
         * if BUFFER_SIZE is 1024 file is corrupted
         * open the downloaded image to test
         */
        //byte[] BUFFER_SIZE = new byte[1]; //Proper Download
        byte[] BUFFER_SIZE = new byte[1024]; //File Corrupt



        while (true) {
            int byteRead = inputStream.read(BUFFER_SIZE);
            if (byteRead == -1) {
                break;
            }

            downloaded += byteRead;

            fileOutputStream.write(BUFFER_SIZE);

            System.out.println(" " + downloaded + "/" + contentLength + " = " + ((downloaded / contentLength) * 100));

        }
        fileOutputStream.flush();
        fileOutputStream.close();

        System.out.println("file closed");
    }

}

Solution

  • If your BUFFER_SIZE is not full at the last read then you will have wrong data written in file:

    You have

    fileOutputStream.write(BUFFER_SIZE);
    

    you should have:

    fileOutputStream.write(BUFFER_SIZE, 0, byteRead);
    

    EDIT1: I would also sugest to replace this part of the code:

    while (true) {
      int byteRead = inputStream.read(BUFFER_SIZE);
      if (byteRead == -1) {
        break;
      }
    

    With a better approach:

    int byteRead;
    while ( (byteRead = inputStream.read(BUFFER_SIZE)) > 0 ) {