金三角 v1.2.0版本的 MD5 值为:6a5bce6c149ec5f5f67d04c823d5cdcf

以下内容为反编译后的 HttpUrlSource.java 源代码,内容仅作参考


package com.danikula.videocache;

import android.text.TextUtils;
import com.danikula.videocache.headers.EmptyHeadersInjector;
import com.danikula.videocache.headers.HeaderInjector;
import com.danikula.videocache.sourcestorage.SourceInfoStorage;
import com.danikula.videocache.sourcestorage.SourceInfoStorageFactory;
import com.lzy.okgo.model.HttpHeaders;
import e.a.a.a.a;
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 class HttpUrlSource implements Source {
    private static final int MAX_REDIRECTS = 5;
    private HttpURLConnection connection;
    private final HeaderInjector headerInjector;
    private InputStream inputStream;
    private SourceInfo sourceInfo;
    private final SourceInfoStorage sourceInfoStorage;

    public HttpUrlSource(String str) {
        this(str, SourceInfoStorageFactory.newEmptySourceInfoStorage());
    }

    private void fetchContentInfo() {
        HttpURLConnection httpURLConnection;
        ?? r0 = 0;
        InputStream inputStream = null;
        try {
            try {
                httpURLConnection = openConnection(0L, 10000);
                try {
                    long contentLength = getContentLength(httpURLConnection);
                    String contentType = httpURLConnection.getContentType();
                    inputStream = httpURLConnection.getInputStream();
                    SourceInfo sourceInfo = new SourceInfo(this.sourceInfo.url, contentLength, contentType);
                    this.sourceInfo = sourceInfo;
                    this.sourceInfoStorage.put(sourceInfo.url, sourceInfo);
                    ProxyCacheUtils.close(inputStream);
                    r0 = httpURLConnection;
                } catch (IOException e2) {
                    e = e2;
                    HttpProxyCacheDebuger.printfError("Error fetching info from " + this.sourceInfo.url, e);
                    ProxyCacheUtils.close(inputStream);
                    r0 = httpURLConnection;
                    if (httpURLConnection == null) {
                        return;
                    }
                    r0.disconnect();
                }
            } catch (Throwable th) {
                th = th;
                ProxyCacheUtils.close(inputStream);
                if (r0 != 0) {
                    r0.disconnect();
                }
                throw th;
            }
        } catch (IOException e3) {
            e = e3;
            httpURLConnection = null;
        } catch (Throwable th2) {
            th = th2;
            r0 = 0;
            ProxyCacheUtils.close(inputStream);
            if (r0 != 0) {
            }
            throw th;
        }
        r0.disconnect();
    }

    private long getContentLength(HttpURLConnection httpURLConnection) {
        String headerField = httpURLConnection.getHeaderField(HttpHeaders.HEAD_KEY_CONTENT_LENGTH);
        if (headerField == null) {
            return -1L;
        }
        return Long.parseLong(headerField);
    }

    private void injectCustomHeaders(HttpURLConnection httpURLConnection, String str) {
        Map<String, String> addHeaders = this.headerInjector.addHeaders(str);
        if (addHeaders == null) {
            return;
        }
        StringBuilder O = a.O("****** injectCustomHeaders ****** :");
        O.append(addHeaders.size());
        HttpProxyCacheDebuger.printfError(O.toString());
        for (Map.Entry<String, String> entry : addHeaders.entrySet()) {
            httpURLConnection.setRequestProperty(entry.getKey(), entry.getValue());
        }
    }

    private HttpURLConnection openConnection(long j2, int i2) {
        HttpURLConnection httpURLConnection;
        boolean z;
        String str = this.sourceInfo.url;
        int i3 = 0;
        do {
            httpURLConnection = (HttpURLConnection) new URL(str).openConnection();
            injectCustomHeaders(httpURLConnection, str);
            if (j2 > 0) {
                httpURLConnection.setRequestProperty(HttpHeaders.HEAD_KEY_RANGE, a.z("bytes=", j2, "-"));
            }
            if (i2 > 0) {
                httpURLConnection.setConnectTimeout(i2);
                httpURLConnection.setReadTimeout(i2);
            }
            int responseCode = httpURLConnection.getResponseCode();
            z = responseCode == 301 || responseCode == 302 || responseCode == 303;
            if (z) {
                str = httpURLConnection.getHeaderField("Location");
                i3++;
                httpURLConnection.disconnect();
            }
            if (i3 > 5) {
                throw new ProxyCacheException(a.v("Too many redirects: ", i3));
            }
        } while (z);
        return httpURLConnection;
    }

    private long readSourceAvailableBytes(HttpURLConnection httpURLConnection, long j2, int i2) {
        long contentLength = getContentLength(httpURLConnection);
        return i2 == 200 ? contentLength : i2 == 206 ? contentLength + j2 : this.sourceInfo.length;
    }

    @Override
    public void close() {
        HttpURLConnection httpURLConnection = this.connection;
        if (httpURLConnection != null) {
            try {
                httpURLConnection.disconnect();
            } catch (ArrayIndexOutOfBoundsException e2) {
                HttpProxyCacheDebuger.printfError("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 :(", e2);
            } catch (IllegalArgumentException e3) {
                e = e3;
                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 (NullPointerException e4) {
                e = e4;
                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);
            }
        }
    }

    public synchronized String getMime() {
        if (TextUtils.isEmpty(this.sourceInfo.mime)) {
            fetchContentInfo();
        }
        return this.sourceInfo.mime;
    }

    public String getUrl() {
        return this.sourceInfo.url;
    }

    @Override
    public synchronized long length() {
        if (this.sourceInfo.length == -2147483648L) {
            fetchContentInfo();
        }
        return this.sourceInfo.length;
    }

    @Override
    public void open(long j2) {
        try {
            HttpURLConnection openConnection = openConnection(j2, -1);
            this.connection = openConnection;
            String contentType = openConnection.getContentType();
            this.inputStream = new BufferedInputStream(this.connection.getInputStream(), 8192);
            HttpURLConnection httpURLConnection = this.connection;
            SourceInfo sourceInfo = new SourceInfo(this.sourceInfo.url, readSourceAvailableBytes(httpURLConnection, j2, httpURLConnection.getResponseCode()), contentType);
            this.sourceInfo = sourceInfo;
            this.sourceInfoStorage.put(sourceInfo.url, sourceInfo);
        } catch (IOException e2) {
            StringBuilder O = a.O("Error opening connection for ");
            O.append(this.sourceInfo.url);
            O.append(" with offset ");
            O.append(j2);
            throw new ProxyCacheException(O.toString(), e2);
        }
    }

    @Override
    public int read(byte[] bArr) {
        InputStream inputStream = this.inputStream;
        if (inputStream != null) {
            try {
                return inputStream.read(bArr, 0, bArr.length);
            } catch (InterruptedIOException e2) {
                throw new InterruptedProxyCacheException(a.K(a.O("Reading source "), this.sourceInfo.url, " is interrupted"), e2);
            } catch (IOException e3) {
                StringBuilder O = a.O("Error reading data from ");
                O.append(this.sourceInfo.url);
                throw new ProxyCacheException(O.toString(), e3);
            }
        }
        throw new ProxyCacheException(a.K(a.O("Error reading data from "), this.sourceInfo.url, ": connection is absent!"));
    }

    public String toString() {
        StringBuilder O = a.O("HttpUrlSource{sourceInfo='");
        O.append(this.sourceInfo);
        O.append("}");
        return O.toString();
    }

    public HttpUrlSource(String str, SourceInfoStorage sourceInfoStorage) {
        this(str, sourceInfoStorage, new EmptyHeadersInjector());
    }

    public HttpUrlSource(String str, SourceInfoStorage sourceInfoStorage, HeaderInjector headerInjector) {
        this.sourceInfoStorage = (SourceInfoStorage) Preconditions.checkNotNull(sourceInfoStorage);
        this.headerInjector = (HeaderInjector) Preconditions.checkNotNull(headerInjector);
        SourceInfo sourceInfo = sourceInfoStorage.get(str);
        this.sourceInfo = sourceInfo == null ? new SourceInfo(str, -2147483648L, ProxyCacheUtils.getSupposablyMime(str)) : sourceInfo;
    }

    public HttpUrlSource(HttpUrlSource httpUrlSource) {
        this.sourceInfo = httpUrlSource.sourceInfo;
        this.sourceInfoStorage = httpUrlSource.sourceInfoStorage;
        this.headerInjector = httpUrlSource.headerInjector;
    }
}