session-android/src/org/thoughtcrime/securesms/glide/OkHttpStreamFetcher.java

94 lines
2.5 KiB
Java
Raw Normal View History

2016-10-17 02:05:07 +00:00
package org.thoughtcrime.securesms.glide;
2017-10-12 00:12:46 +00:00
import android.support.annotation.NonNull;
2016-10-17 02:05:07 +00:00
import com.bumptech.glide.Priority;
2017-10-12 00:12:46 +00:00
import com.bumptech.glide.load.DataSource;
2016-10-17 02:05:07 +00:00
import com.bumptech.glide.load.data.DataFetcher;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.util.ContentLengthInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
2016-10-17 02:05:07 +00:00
/**
* Fetches an {@link InputStream} using the okhttp library.
*/
2017-10-12 00:12:46 +00:00
class OkHttpStreamFetcher implements DataFetcher<InputStream> {
2018-12-06 20:14:20 +00:00
private static final String TAG = OkHttpStreamFetcher.class.getSimpleName();
2016-10-17 02:05:07 +00:00
private final OkHttpClient client;
private final GlideUrl url;
private InputStream stream;
private ResponseBody responseBody;
2016-10-17 02:05:07 +00:00
2017-10-12 00:12:46 +00:00
OkHttpStreamFetcher(OkHttpClient client, GlideUrl url) {
2016-10-17 02:05:07 +00:00
this.client = client;
this.url = url;
}
@Override
public void loadData(@NonNull Priority priority, @NonNull DataCallback<? super InputStream> callback) {
2017-10-12 00:12:46 +00:00
try {
Request.Builder requestBuilder = new Request.Builder()
.url(url.toStringUrl());
for (Map.Entry<String, String> headerEntry : url.getHeaders().entrySet()) {
String key = headerEntry.getKey();
requestBuilder.addHeader(key, headerEntry.getValue());
}
2016-10-17 02:05:07 +00:00
2017-10-12 00:12:46 +00:00
Request request = requestBuilder.build();
Response response = client.newCall(request).execute();
2016-10-17 02:05:07 +00:00
2017-10-12 00:12:46 +00:00
responseBody = response.body();
2017-10-12 00:12:46 +00:00
if (!response.isSuccessful()) {
throw new IOException("Request failed with code: " + response.code());
}
2017-10-12 00:12:46 +00:00
long contentLength = responseBody.contentLength();
stream = ContentLengthInputStream.obtain(responseBody.byteStream(), contentLength);
2016-10-17 02:05:07 +00:00
2017-10-12 00:12:46 +00:00
callback.onDataReady(stream);
} catch (IOException e) {
callback.onLoadFailed(e);
}
2016-10-17 02:05:07 +00:00
}
@Override
public void cleanup() {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
// Ignored
}
}
if (responseBody != null) {
responseBody.close();
2016-10-17 02:05:07 +00:00
}
}
@Override
2017-10-12 00:12:46 +00:00
public void cancel() {
// TODO: call cancel on the client when this method is called on a background thread. See #257
2016-10-17 02:05:07 +00:00
}
@Override
public @NonNull Class<InputStream> getDataClass() {
2017-10-12 00:12:46 +00:00
return InputStream.class;
}
@Override
public @NonNull DataSource getDataSource() {
2017-10-12 00:12:46 +00:00
return DataSource.REMOTE;
2016-10-17 02:05:07 +00:00
}
}