@@ -30,6 +30,48 @@ public class OkHttpResponse {
3030 public OkHttpResponseProgressCallback progressCallback = null ;
3131 public OkHttpResponseCloseCallback closeCallback = null ;
3232
33+ public static class ProgressInputStream extends InputStream {
34+ private final InputStream inputStream ;
35+ private final ProgressListener listener ;
36+ private long totalBytesRead = 0 ;
37+ private long contentLength = 0 ;
38+
39+ public interface ProgressListener {
40+ void update (long bytesRead , long contentLength , boolean done );
41+ }
42+
43+ public ProgressInputStream (InputStream inputStream , ProgressListener listener , long contentLength ) {
44+ this .inputStream = inputStream ;
45+ this .listener = listener ;
46+ this .contentLength = contentLength ;
47+ }
48+
49+ @ Override
50+ public int read () throws IOException {
51+ int bytesRead = inputStream .read ();
52+ if (bytesRead >= 0 ) {
53+ totalBytesRead ++;
54+ listener .update (totalBytesRead , contentLength , totalBytesRead == contentLength );
55+ }
56+ return bytesRead ;
57+ }
58+
59+ @ Override
60+ public int read (byte [] b , int off , int len ) throws IOException {
61+ int bytesRead = inputStream .read (b , off , len );
62+ if (bytesRead >= 0 ) {
63+ totalBytesRead += bytesRead ;
64+ listener .update (totalBytesRead , contentLength , totalBytesRead == contentLength );
65+ }
66+ return bytesRead ;
67+ }
68+
69+ @ Override
70+ public void close () throws IOException {
71+ inputStream .close ();
72+ }
73+ }
74+
3375 public static interface OkHttpResponseAsyncCallback {
3476 void onException (final Exception exc );
3577
@@ -66,7 +108,6 @@ public boolean isFinished() {
66108 @ Override
67109 public void run () {
68110 synchronized (mHandler ) {
69- Log .d ("JS" , "NotifyRunnable run" );
70111 mRunnable .run ();
71112 mFinished = true ;
72113 mHandler .notifyAll ();
@@ -80,16 +121,12 @@ public static void postAndWait(final Handler handler, final Runnable r) {
80121 r .run ();
81122 } else {
82123 synchronized (handler ) {
83- Log .d ("JS" , "postAndWait1" );
84124 NotifyRunnable runnable = new NotifyRunnable (handler , r );
85125 handler .post (runnable );
86- Log .d ("JS" , "postAndWait2" );
87126 while (!runnable .isFinished ()) {
88- Log .d ("JS" , "postAndWait3" );
89127 try {
90128 handler .wait ();
91129 } catch (InterruptedException is ) {
92- Log .d ("JS" , "postAndWait4" , is );
93130 // ignore
94131 }
95132 }
@@ -210,46 +247,27 @@ private static void closeResponseBody(ResponseBody responseBody, OkHttpResponseC
210247 static Bitmap responseBodyToBitmap (OkHttpResponse response , OkHttpResponseProgressCallback progressCallback )
211248 throws Exception {
212249
213- BufferedInputStream input = null ;
214- OutputStream output = null ;
250+ InputStream inputStream = null ;
215251 try {
216- PipedInputStream in = new PipedInputStream ();
217- InputStream is = response .responseBody .byteStream ();
218-
219- input = new BufferedInputStream (is );
220-
221- output = new PipedOutputStream (in );
222252
223- byte [] data = new byte [1024 ];
224253 long contentLength = response .responseBody .contentLength ();
225-
226- long total = 0 ;
227- int count = 0 ;
228- if (progressCallback != null ) {
229- progressCallback .onProgress (total , contentLength );
230- }
231- while ((count = input .read (data )) != -1 && !response .cancelled ) {
232- total += count ;
233- output .write (data , 0 , count );
234- if (progressCallback != null ) {
235- progressCallback .onProgress (total , contentLength );
254+ inputStream = new ProgressInputStream (response .responseBody .byteStream (), new ProgressInputStream .ProgressListener () {
255+ @ Override
256+ public void update (long bytesRead , long contentLength , boolean done ) {
257+ if (progressCallback != null ) {
258+ progressCallback .onProgress (bytesRead , contentLength );
259+ }
236260 }
237- }
238- if (response .cancelled && total != contentLength ) {
239- throw new Exception ("cancelled" );
240- }
241- return BitmapFactory .decodeStream (in );
261+ }, contentLength );
262+
263+ // Decode the bitmap
264+ return BitmapFactory .decodeStream (inputStream );
242265 } catch (Exception e ) {
243266 throw e ;
244267 } finally {
245- if (output != null ) {
246- output .flush ();
247- output .close ();
268+ if (inputStream != null ) {
269+ inputStream .close ();
248270 }
249- if (input != null ) {
250- input .close ();
251- }
252- input .close ();
253271 response .closeResponseBody ();
254272 }
255273 }
@@ -260,7 +278,6 @@ public File toFile(String filePath) throws Exception {
260278
261279 public void toFileAsync (final String filePath , final OkHttpResponseAsyncCallback callback ) {
262280 final OkHttpResponse fme = this ;
263- // Log.d(TAG, "toFileAsync");
264281 Thread thread = new Thread (new Runnable () {
265282 @ Override
266283 public void run () {
0 commit comments