0

I'm downloading a file using a Retrofit2 implementation of a REST API, per the nice tutorial at https://futurestud.io/tutorials/retrofit-2-how-to-download-files-from-server.

My API interface declares the @GET as @Streaming, and indeed when I read from the InputStream given me by byteStream(), I can read the entire 29 MB file.

What I'd like to do is read it all in 4 MB chunks, so I'm using Okio's handy BufferedSource. The problem is that my call to request(4*1024*1024) returns true only once, though the total file size is something on the order of 29 MB.

My Java:

      @Streaming @GET Call<ResponseBody> get(@Url String url);

      // ...

      Response<ResponseBody> response = api.get("https://my.file.url");

      final int bufSize = 4*1024*1024;
      byte[] buffer;
      long total;

      InputStream is = response.body().byteStream();
      BufferedSource ss = Okio.buffer(Okio.source(is));
      // Is this the same as ss=response.body().source() ??

      while (ss.request(bufSize)) {
         buffer = ss.readByteArray();
         total += doSomethingUsefulWith(buffer);
         System.out.println("Running total: " + total);
      }

      // Capture the < 4MB residue
      buffer = ss.readByteArray();

      if (buffer.length > 0) {
         total += doSomethingUsefulWith(buffer);
         System.out.println("Total: " + total);
      }

      System.out.println("That's all, folks!");

Console output:

Running total: 4194304
That's all, folks!

Again, the raw InputStream does give me the full 29 MB, I've done it before. Do I misunderstand request()? What am I doing wrong?

QED
  • 9,803
  • 7
  • 50
  • 87

1 Answers1

1

The call to ss.readByteArray() will read the entire body into a byte array. Did you mean to do ss.readByteArray(bufSize)?

Eric Cochran
  • 8,414
  • 5
  • 50
  • 91
  • Thanks -- I think you may be right here and in your comments. I have since moved on to another solution but I may revisit this later. (Also, I did then have code to handle the last few MB, but good catch.) – QED Apr 12 '19 at 18:45