1
1
package com .bumptech .glide .integration .okhttp3 ;
2
2
3
+ import android .os .Build ;
3
4
import android .support .annotation .NonNull ;
4
5
import android .util .Log ;
5
6
import com .bumptech .glide .Priority ;
20
21
/**
21
22
* Fetches an {@link InputStream} using the okhttp library.
22
23
*/
23
- public class OkHttpStreamFetcher implements DataFetcher <InputStream > {
24
+ public class OkHttpStreamFetcher implements DataFetcher <InputStream >,
25
+ okhttp3 .Callback {
24
26
private static final String TAG = "OkHttpFetcher" ;
25
27
private final Call .Factory client ;
26
28
private final GlideUrl url ;
27
29
@ Synthetic InputStream stream ;
28
30
@ Synthetic ResponseBody responseBody ;
29
31
private volatile Call call ;
32
+ private DataCallback <? super InputStream > callback ;
30
33
31
34
public OkHttpStreamFetcher (Call .Factory client , GlideUrl url ) {
32
35
this .client = client ;
@@ -41,29 +44,45 @@ public void loadData(Priority priority, final DataCallback<? super InputStream>
41
44
requestBuilder .addHeader (key , headerEntry .getValue ());
42
45
}
43
46
Request request = requestBuilder .build ();
47
+ this .callback = callback ;
44
48
45
49
call = client .newCall (request );
46
- call .enqueue (new okhttp3 .Callback () {
47
- @ Override
48
- public void onFailure (Call call , IOException e ) {
49
- if (Log .isLoggable (TAG , Log .DEBUG )) {
50
- Log .d (TAG , "OkHttp failed to obtain result" , e );
51
- }
52
- callback .onLoadFailed (e );
50
+ if (Build .VERSION .SDK_INT != Build .VERSION_CODES .O ) {
51
+ call .enqueue (this );
52
+ } else {
53
+ try {
54
+ // Calling execute instead of enqueue is a workaround for #2355, where okhttp throws a
55
+ // ClassCastException on O.
56
+ onResponse (call , call .execute ());
57
+ } catch (IOException e ) {
58
+ onFailure (call , e );
59
+ } catch (ClassCastException e ) {
60
+ // It's not clear that this catch is necessary, the error may only occur even on O if
61
+ // enqueue is used.
62
+ onFailure (call , new IOException ("Workaround for framework bug on O" , e ));
53
63
}
64
+ }
65
+ }
54
66
55
- @ Override
56
- public void onResponse (Call call , Response response ) throws IOException {
57
- responseBody = response .body ();
58
- if (response .isSuccessful ()) {
59
- long contentLength = responseBody .contentLength ();
60
- stream = ContentLengthInputStream .obtain (responseBody .byteStream (), contentLength );
61
- callback .onDataReady (stream );
62
- } else {
63
- callback .onLoadFailed (new HttpException (response .message (), response .code ()));
64
- }
65
- }
66
- });
67
+ @ Override
68
+ public void onFailure (Call call , IOException e ) {
69
+ if (Log .isLoggable (TAG , Log .DEBUG )) {
70
+ Log .d (TAG , "OkHttp failed to obtain result" , e );
71
+ }
72
+
73
+ callback .onLoadFailed (e );
74
+ }
75
+
76
+ @ Override
77
+ public void onResponse (Call call , Response response ) throws IOException {
78
+ responseBody = response .body ();
79
+ if (response .isSuccessful ()) {
80
+ long contentLength = responseBody .contentLength ();
81
+ stream = ContentLengthInputStream .obtain (responseBody .byteStream (), contentLength );
82
+ callback .onDataReady (stream );
83
+ } else {
84
+ callback .onLoadFailed (new HttpException (response .message (), response .code ()));
85
+ }
67
86
}
68
87
69
88
@ Override
@@ -78,6 +97,7 @@ public void cleanup() {
78
97
if (responseBody != null ) {
79
98
responseBody .close ();
80
99
}
100
+ callback = null ;
81
101
}
82
102
83
103
@ Override
0 commit comments