package z4;

import com.bumptech.glide.Priority;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.HttpException;
import com.bumptech.glide.load.data.d;
import com.bumptech.glide.load.data.e;
import g5.o;
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;
import t5.n;

/* JADX INFO: loaded from: classes.dex */
public final class a implements e, Callback {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final Call.Factory f37055a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final o f37056b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public t5.e f37057c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public ResponseBody f37058d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public d f37059e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public volatile Call f37060f;

    public a(Call.Factory factory, o oVar) {
        this.f37055a = factory;
        this.f37056b = oVar;
    }

    @Override // com.bumptech.glide.load.data.e
    public final Class a() {
        return InputStream.class;
    }

    @Override // com.bumptech.glide.load.data.e
    public final void b() {
        try {
            t5.e eVar = this.f37057c;
            if (eVar != null) {
                eVar.close();
            }
        } catch (IOException unused) {
        }
        ResponseBody responseBody = this.f37058d;
        if (responseBody != null) {
            responseBody.close();
        }
        this.f37059e = null;
    }

    @Override // com.bumptech.glide.load.data.e
    public final void cancel() {
        Call call = this.f37060f;
        if (call != null) {
            call.cancel();
        }
    }

    @Override // com.bumptech.glide.load.data.e
    public final DataSource d() {
        return DataSource.f5590b;
    }

    @Override // com.bumptech.glide.load.data.e
    public final void e(Priority priority, d dVar) {
        Request.Builder builderUrl = new Request.Builder().url(this.f37056b.toStringUrl());
        for (Map.Entry<String, String> entry : this.f37056b.getHeaders().entrySet()) {
            builderUrl.addHeader(entry.getKey(), entry.getValue());
        }
        Request requestBuild = builderUrl.build();
        this.f37059e = dVar;
        this.f37060f = this.f37055a.newCall(requestBuild);
        this.f37060f.enqueue(this);
    }

    @Override // okhttp3.Callback
    public final void onFailure(Call call, IOException iOException) {
        this.f37059e.c(iOException);
    }

    @Override // okhttp3.Callback
    public final void onResponse(Call call, Response response) {
        this.f37058d = response.body();
        if (!response.isSuccessful()) {
            this.f37059e.c(new HttpException(response.message(), response.code(), null));
            return;
        }
        t5.e eVar = new t5.e(this.f37058d.byteStream(), ((ResponseBody) n.checkNotNull(this.f37058d)).contentLength());
        this.f37057c = eVar;
        this.f37059e.onDataReady(eVar);
    }
}
