0

As in this example, I want to measure my upload progress, but not for an image, for a simple base64 String instead. That String is usually approx. 500 kB length.

As in the example I extended the Body from OkHttp3's RequestBody and overwrote:

@Override
    public void writeTo(BufferedSink sink) throws IOException {
        StringReader in = new StringReader(base64);
        char[] buffer = new char[2048];
        int uploaded = 0;
        try {
            int read;
            while ((read = in.read(buffer, uploaded, buffer.length)) != -1) {
                uploaded += read;
                System.out.println("BUFFER " + buffer);
                sink.write(new String(buffer).getBytes(), 0, read);
                this.callback.onProgressUpdate(uploaded);
            }
        } catch(IndexOutOfBoundsException e) {
            System.err.println("IndexOutOfBoundsException uploaded " + uploaded + " , buffer " + buffer.length + " from total " + base64.length());
        } finally {
            in.close();
            this.callback.onFinish(uploaded);
        }
}

I can read the first 2048 chars successsfully, but for the second round in while-loop I get a IndexOutOfBoundsException ?

Tranquillo
  • 235
  • 2
  • 13

0 Answers0