100 lines
3.2 KiB
Java
100 lines
3.2 KiB
Java
package com.bumptech.glide.integration.okhttp3;
|
|
|
|
import android.util.Log;
|
|
import com.bumptech.glide.Priority;
|
|
import com.bumptech.glide.load.DataSource;
|
|
import com.bumptech.glide.load.HttpException;
|
|
import com.bumptech.glide.load.data.DataFetcher;
|
|
import com.bumptech.glide.load.model.GlideUrl;
|
|
import com.bumptech.glide.util.ContentLengthInputStream;
|
|
import com.bumptech.glide.util.Preconditions;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.util.Map;
|
|
import okhttp3.Call;
|
|
import okhttp3.Callback;
|
|
import okhttp3.Request;
|
|
import okhttp3.Response;
|
|
import okhttp3.ResponseBody;
|
|
|
|
/* loaded from: classes.dex */
|
|
public class OkHttpStreamFetcher implements DataFetcher<InputStream>, Callback {
|
|
private final Call.Factory a;
|
|
private final GlideUrl b;
|
|
private InputStream c;
|
|
private ResponseBody d;
|
|
private DataFetcher.DataCallback<? super InputStream> e;
|
|
private volatile Call f;
|
|
|
|
public OkHttpStreamFetcher(Call.Factory factory, GlideUrl glideUrl) {
|
|
this.a = factory;
|
|
this.b = glideUrl;
|
|
}
|
|
|
|
@Override // com.bumptech.glide.load.data.DataFetcher
|
|
public void a(Priority priority, DataFetcher.DataCallback<? super InputStream> dataCallback) {
|
|
Request.Builder url = new Request.Builder().url(this.b.c());
|
|
for (Map.Entry<String, String> entry : this.b.b().entrySet()) {
|
|
url.addHeader(entry.getKey(), entry.getValue());
|
|
}
|
|
Request build = url.build();
|
|
this.e = dataCallback;
|
|
this.f = this.a.newCall(build);
|
|
this.f.enqueue(this);
|
|
}
|
|
|
|
@Override // com.bumptech.glide.load.data.DataFetcher
|
|
public void b() {
|
|
try {
|
|
if (this.c != null) {
|
|
this.c.close();
|
|
}
|
|
} catch (IOException unused) {
|
|
}
|
|
ResponseBody responseBody = this.d;
|
|
if (responseBody != null) {
|
|
responseBody.close();
|
|
}
|
|
this.e = null;
|
|
}
|
|
|
|
@Override // com.bumptech.glide.load.data.DataFetcher
|
|
public void cancel() {
|
|
Call call = this.f;
|
|
if (call != null) {
|
|
call.cancel();
|
|
}
|
|
}
|
|
|
|
@Override // com.bumptech.glide.load.data.DataFetcher
|
|
public DataSource getDataSource() {
|
|
return DataSource.REMOTE;
|
|
}
|
|
|
|
@Override // okhttp3.Callback
|
|
public void onFailure(Call call, IOException iOException) {
|
|
if (Log.isLoggable("OkHttpFetcher", 3)) {
|
|
Log.d("OkHttpFetcher", "OkHttp failed to obtain result", iOException);
|
|
}
|
|
this.e.a((Exception) iOException);
|
|
}
|
|
|
|
@Override // okhttp3.Callback
|
|
public void onResponse(Call call, Response response) {
|
|
this.d = response.body();
|
|
if (!response.isSuccessful()) {
|
|
this.e.a((Exception) new HttpException(response.message(), response.code()));
|
|
return;
|
|
}
|
|
ResponseBody responseBody = this.d;
|
|
Preconditions.a(responseBody);
|
|
this.c = ContentLengthInputStream.a(this.d.byteStream(), responseBody.contentLength());
|
|
this.e.a((DataFetcher.DataCallback<? super InputStream>) this.c);
|
|
}
|
|
|
|
@Override // com.bumptech.glide.load.data.DataFetcher
|
|
public Class<InputStream> a() {
|
|
return InputStream.class;
|
|
}
|
|
}
|