|
| 1 | +package com.bumptech.glide.integration.okhttp3; |
| 2 | + |
| 3 | +import android.util.Log; |
| 4 | +import androidx.annotation.NonNull; |
| 5 | +import com.bumptech.glide.Priority; |
| 6 | +import com.bumptech.glide.load.DataSource; |
| 7 | +import com.bumptech.glide.load.HttpException; |
| 8 | +import com.bumptech.glide.load.data.DataFetcher; |
| 9 | +import com.bumptech.glide.load.model.GlideUrl; |
| 10 | +import com.bumptech.glide.util.ContentLengthInputStream; |
| 11 | +import com.bumptech.glide.util.Preconditions; |
| 12 | +import java.io.IOException; |
| 13 | +import java.io.InputStream; |
| 14 | +import java.util.Map; |
| 15 | +import okhttp3.Call; |
| 16 | +import okhttp3.Request; |
| 17 | +import okhttp3.Response; |
| 18 | +import okhttp3.ResponseBody; |
| 19 | + |
| 20 | +/** Fetches an {@link InputStream} using the okhttp library. */ |
| 21 | +public class OkHttpStreamFetcher implements DataFetcher<InputStream>, okhttp3.Callback { |
| 22 | + private static final String TAG = "OkHttpFetcher"; |
| 23 | + private final Call.Factory client; |
| 24 | + private final GlideUrl url; |
| 25 | + private InputStream stream; |
| 26 | + private ResponseBody responseBody; |
| 27 | + private DataCallback<? super InputStream> callback; |
| 28 | + // call may be accessed on the main thread while the object is in use on other threads. All other |
| 29 | + // accesses to variables may occur on different threads, but only one at a time. |
| 30 | + private volatile Call call; |
| 31 | + |
| 32 | + // Public API. |
| 33 | + @SuppressWarnings("WeakerAccess") |
| 34 | + public OkHttpStreamFetcher(Call.Factory client, GlideUrl url) { |
| 35 | + this.client = client; |
| 36 | + this.url = url; |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public void loadData( |
| 41 | + @NonNull Priority priority, @NonNull final DataCallback<? super InputStream> callback) { |
| 42 | + Request.Builder requestBuilder = new Request.Builder().url(url.toStringUrl()); |
| 43 | + for (Map.Entry<String, String> headerEntry : url.getHeaders().entrySet()) { |
| 44 | + String key = headerEntry.getKey(); |
| 45 | + requestBuilder.addHeader(key, headerEntry.getValue()); |
| 46 | + } |
| 47 | + Request request = requestBuilder.build(); |
| 48 | + this.callback = callback; |
| 49 | + |
| 50 | + call = client.newCall(request); |
| 51 | + call.enqueue(this); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public void onFailure(@NonNull Call call, @NonNull IOException e) { |
| 56 | + if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 57 | + Log.d(TAG, "OkHttp failed to obtain result", e); |
| 58 | + } |
| 59 | + |
| 60 | + callback.onLoadFailed(e); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public void onResponse(@NonNull Call call, @NonNull Response response) { |
| 65 | + responseBody = response.body(); |
| 66 | + if (response.isSuccessful()) { |
| 67 | + long contentLength = Preconditions.checkNotNull(responseBody).contentLength(); |
| 68 | + stream = ContentLengthInputStream.obtain(responseBody.byteStream(), contentLength); |
| 69 | + callback.onDataReady(stream); |
| 70 | + } else { |
| 71 | + callback.onLoadFailed(new HttpException(response.message(), response.code())); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public void cleanup() { |
| 77 | + try { |
| 78 | + if (stream != null) { |
| 79 | + stream.close(); |
| 80 | + } |
| 81 | + } catch (IOException e) { |
| 82 | + // Ignored |
| 83 | + } |
| 84 | + if (responseBody != null) { |
| 85 | + responseBody.close(); |
| 86 | + } |
| 87 | + callback = null; |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public void cancel() { |
| 92 | + Call local = call; |
| 93 | + if (local != null) { |
| 94 | + local.cancel(); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + @NonNull |
| 99 | + @Override |
| 100 | + public Class<InputStream> getDataClass() { |
| 101 | + return InputStream.class; |
| 102 | + } |
| 103 | + |
| 104 | + @NonNull |
| 105 | + @Override |
| 106 | + public DataSource getDataSource() { |
| 107 | + return DataSource.REMOTE; |
| 108 | + } |
| 109 | +} |
0 commit comments