11package com .qiniu .android .http ;
22
33
4+ import com .qiniu .android .utils .AsyncRun ;
5+
46import java .io .ByteArrayInputStream ;
57import java .io .IOException ;
68import java .io .InputStream ;
@@ -15,7 +17,7 @@ public final class ByteArrayEntity extends AbstractHttpEntity implements Cloneab
1517
1618 private static final int progressStep = 8 * 1024 ;
1719 private final byte [] b ;
18- private final int off , len ;
20+ private final int offset , len ;
1921 private final ProgressHandler progressHandler ;
2022 private final CancellationHandler cancellationHandler ;
2123
@@ -30,7 +32,7 @@ public ByteArrayEntity(final byte[] b, final int off, final int len, ProgressHan
3032 throw new IndexOutOfBoundsException ("off: " + off + " len: " + len + " b.length: " + b .length );
3133 }
3234 this .b = b ;
33- this .off = off ;
35+ this .offset = off ;
3436 this .len = len ;
3537 this .progressHandler = h ;
3638 this .cancellationHandler = c ;
@@ -48,15 +50,15 @@ public long getContentLength() {
4850
4951 @ Override
5052 public InputStream getContent () {
51- return new ByteArrayInputStream (this .b , this .off , this .len );
53+ return new ByteArrayInputStream (this .b , this .offset , this .len );
5254 }
5355
5456 // @Override
5557 public void writeTo (final OutputStream outStream ) throws IOException {
5658 if (progressHandler != null || cancellationHandler != null ) {
5759 writeWithNotify (outStream );
5860 } else {
59- outStream .write (this .b , this .off , this .len );
61+ outStream .write (this .b , this .offset , this .len );
6062 }
6163 outStream .flush ();
6264 }
@@ -74,9 +76,16 @@ private void writeWithNotify(final OutputStream outStream) throws IOException {
7476 }
7577 int left = this .len - off ;
7678 int len = left < progressStep ? left : progressStep ;
77- outStream .write (this .b , this .off + off , len );
79+ outStream .write (this .b , this .offset + off , len );
7880 if (progressHandler != null ) {
79- progressHandler .onProgress (off , this .len );
81+ final int off2 = off ;
82+ final ByteArrayEntity b = this ;
83+ AsyncRun .run (new Runnable () {
84+ @ Override
85+ public void run () {
86+ progressHandler .onProgress (off2 , b .len );
87+ }
88+ });
8089 }
8190 off += len ;
8291 }
0 commit comments