最右.apk(点击下载) / h.java


package com.kwad.sdk.core.videocache;

import android.text.TextUtils;
import android.webkit.MimeTypeMap;
import com.alibaba.sdk.android.oss.common.utils.HttpHeaders;
import com.kwad.sdk.core.network.o;
import com.kwad.sdk.core.network.q;
import com.kwad.sdk.core.videocache.c.b;
import com.kwad.sdk.utils.ai;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InterruptedIOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;

public final class h implements l {
    private final b afV;
    private final com.kwad.sdk.core.videocache.a.b afW;
    private m agr;
    private HttpURLConnection ags;
    private InputStream agt;

    public h(h hVar) {
        this.agr = hVar.agr;
        this.afV = hVar.afV;
        this.afW = hVar.afW;
    }

    public h(String str, b bVar, com.kwad.sdk.core.videocache.a.b bVar2) {
        this.afV = (b) ai.e(bVar, "");
        this.afW = (com.kwad.sdk.core.videocache.a.b) ai.e(bVar2, "");
        m bm = bVar.bm(str);
        if (bm == null) {
            MimeTypeMap singleton = MimeTypeMap.getSingleton();
            String fileExtensionFromUrl = MimeTypeMap.getFileExtensionFromUrl(str);
            bm = new m(str, -2147483648L, TextUtils.isEmpty(fileExtensionFromUrl) ? null : singleton.getMimeTypeFromExtension(fileExtensionFromUrl));
        }
        this.agr = bm;
    }

    private long a(HttpURLConnection httpURLConnection, long j, int i) {
        long b2 = b(httpURLConnection);
        return i == 200 ? b2 : i == 206 ? b2 + j : this.agr.agG;
    }

    private HttpURLConnection a(long j, int i) {
        HttpURLConnection httpURLConnection;
        boolean z;
        String str = this.agr.url;
        int i2 = 0;
        do {
            StringBuilder sb = new StringBuilder("Open connection ");
            int i3 = (j > 0 ? 1 : (j == 0 ? 0 : -1));
            sb.append(i3 > 0 ? " with offset ".concat(String.valueOf(j)) : "");
            sb.append(" to ");
            sb.append(str);
            com.kwad.sdk.core.e.b.d("HttpUrlSource", sb.toString());
            httpURLConnection = (HttpURLConnection) new URL(str).openConnection();
            q.wrapHttpURLConnection(httpURLConnection);
            c(httpURLConnection);
            if (i3 > 0) {
                httpURLConnection.setRequestProperty(HttpHeaders.RANGE, "bytes=" + j + "-");
            }
            if (i > 0) {
                httpURLConnection.setConnectTimeout(i);
                httpURLConnection.setReadTimeout(i);
            }
            httpURLConnection.setRequestProperty("User-Agent", o.getUserAgent());
            httpURLConnection.setRequestProperty("BrowserUa", o.lb());
            httpURLConnection.setRequestProperty("SystemUa", o.la());
            int responseCode = httpURLConnection.getResponseCode();
            z = responseCode == 301 || responseCode == 302 || responseCode == 303;
            if (z) {
                str = httpURLConnection.getHeaderField(HttpHeaders.LOCATION);
                i2++;
                httpURLConnection.disconnect();
            }
            if (i2 > 5) {
                throw new ProxyCacheException("Too many redirects: ".concat(String.valueOf(i2)));
            }
        } while (z);
        return httpURLConnection;
    }

    private static long b(HttpURLConnection httpURLConnection) {
        String headerField = httpURLConnection.getHeaderField("Content-Length");
        if (headerField == null) {
            return -1;
        }
        return Long.parseLong(headerField);
    }

    private void c(HttpURLConnection httpURLConnection) {
        for (Map.Entry<String, String> entry : this.afW.mB().entrySet()) {
            httpURLConnection.setRequestProperty(entry.getKey(), entry.getValue());
        }
    }

    /* JADX WARNING: Can't wrap try/catch for region: R(2:12|13) */
    /* JADX WARNING: Code restructure failed: missing block: B:13:?, code lost:
        com.kwad.sdk.core.e.b.e("HttpUrlSource", "Error fetching info from " + r8.agr.url);
     */
    /* JADX WARNING: Code restructure failed: missing block: B:7:0x0058, code lost:
        r1 = th;
     */
    /* JADX WARNING: Failed to process nested try/catch */
    /* JADX WARNING: Missing exception handler attribute for start block: B:12:0x005e */
    private void mx() {
        HttpURLConnection httpURLConnection;
        com.kwad.sdk.core.e.b.d("HttpUrlSource", "Read content info from " + this.agr.url);
        InputStream inputStream = null;
        try {
            httpURLConnection = a(0, 10000);
            long b2 = b(httpURLConnection);
            String contentType = httpURLConnection.getContentType();
            inputStream = httpURLConnection.getInputStream();
            m mVar = new m(this.agr.url, b2, contentType);
            this.agr = mVar;
            this.afV.a(mVar.url, mVar);
            com.kwad.sdk.core.e.b.d("HttpUrlSource", "Source info fetched: " + this.agr);
        } catch (IOException unused) {
            httpURLConnection = null;
        } catch (Throwable th) {
            Throwable th2 = th;
            httpURLConnection = null;
            com.kwad.sdk.crash.utils.b.closeQuietly(inputStream);
            com.kwad.sdk.crash.utils.b.a(httpURLConnection);
            throw th2;
        }
        com.kwad.sdk.crash.utils.b.closeQuietly(inputStream);
        com.kwad.sdk.crash.utils.b.a(httpURLConnection);
    }

    @Override // com.kwad.sdk.core.videocache.l
    public final void close() {
        HttpURLConnection httpURLConnection = this.ags;
        if (httpURLConnection != null) {
            try {
                httpURLConnection.disconnect();
            } catch (IllegalArgumentException | NullPointerException e) {
                throw new RuntimeException("Wait... but why? WTF!? Really shouldn't happen any more after fixing https://github.com/danikula/AndroidVideoCache/issues/43. If you read it on your device log, please, notify me danikula@gmail.com or create issue here https://github.com/danikula/AndroidVideoCache/issues.", e);
            } catch (ArrayIndexOutOfBoundsException unused) {
                com.kwad.sdk.core.e.b.e("HttpUrlSource", "Error closing connection correctly. Should happen only on Android L. If anybody know how to fix it, please visit https://github.com/danikula/AndroidVideoCache/issues/88. Until good solution is not know, just ignore this issue.");
            }
        }
    }

    @Override // com.kwad.sdk.core.videocache.l
    public final synchronized long length() {
        if (this.agr.agG == -2147483648L) {
            mx();
        }
        return this.agr.agG;
    }

    public final synchronized String my() {
        if (TextUtils.isEmpty(this.agr.agH)) {
            mx();
        }
        return this.agr.agH;
    }

    @Override // com.kwad.sdk.core.videocache.l
    public final int read(byte[] bArr) {
        InputStream inputStream = this.agt;
        if (inputStream != null) {
            try {
                return inputStream.read(bArr, 0, 8192);
            } catch (InterruptedIOException e) {
                throw new InterruptedProxyCacheException("Reading source " + this.agr.url + " is interrupted", e);
            } catch (IOException e2) {
                throw new ProxyCacheException("Error reading data from " + this.agr.url, e2);
            }
        } else {
            throw new ProxyCacheException("Error reading data from " + this.agr.url + ": connection is absent!");
        }
    }

    public final String toString() {
        return "HttpUrlSource{sourceInfo='" + this.agr + com.alipay.sdk.util.h.d;
    }

    @Override // com.kwad.sdk.core.videocache.l
    public final void x(long j) {
        try {
            HttpURLConnection a2 = a(j, -1);
            this.ags = a2;
            String contentType = a2.getContentType();
            this.agt = new BufferedInputStream(this.ags.getInputStream(), 8192);
            HttpURLConnection httpURLConnection = this.ags;
            m mVar = new m(this.agr.url, a(httpURLConnection, j, httpURLConnection.getResponseCode()), contentType);
            this.agr = mVar;
            this.afV.a(mVar.url, mVar);
        } catch (IOException e) {
            throw new ProxyCacheException("Error opening connection for " + this.agr.url + " with offset " + j, e);
        }
    }
}