家家有田 v1.2.8版本的 MD5 值为:62f44aac901441b9b72af508fbe86bf2
以下内容为反编译后的 HttpUrlSource.java 源代码,内容仅作参考
package com.danikula.videocache;
import android.text.TextUtils;
import com.alipay.sdk.m.l.b;
import com.alipay.sdk.m.u.i;
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 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.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Map;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
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;
private final TrustManager[] trustAllCerts;
private final HostnameVerifier v;
public HttpUrlSource(String str, HostnameVerifier hostnameVerifier, TrustManager[] trustManagerArr) {
this(str, SourceInfoStorageFactory.newEmptySourceInfoStorage(), hostnameVerifier, trustManagerArr);
}
public HttpUrlSource(String str, SourceInfoStorage sourceInfoStorage, HostnameVerifier hostnameVerifier, TrustManager[] trustManagerArr) {
this(str, sourceInfoStorage, new EmptyHeadersInjector(), hostnameVerifier, trustManagerArr);
}
public HttpUrlSource(String str, SourceInfoStorage sourceInfoStorage, HeaderInjector headerInjector, HostnameVerifier hostnameVerifier, TrustManager[] trustManagerArr) {
this.sourceInfoStorage = (SourceInfoStorage) Preconditions.checkNotNull(sourceInfoStorage);
this.headerInjector = (HeaderInjector) Preconditions.checkNotNull(headerInjector);
this.v = hostnameVerifier;
this.trustAllCerts = trustManagerArr;
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;
this.trustAllCerts = httpUrlSource.trustAllCerts;
this.v = httpUrlSource.v;
}
@Override
public synchronized long length() throws ProxyCacheException {
if (this.sourceInfo.length == -2147483648L) {
fetchContentInfo();
}
return this.sourceInfo.length;
}
@Override
public void open(long j) throws ProxyCacheException {
try {
HttpURLConnection openConnection = openConnection(j, -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, j, httpURLConnection.getResponseCode()), contentType);
this.sourceInfo = sourceInfo;
this.sourceInfoStorage.put(sourceInfo.url, this.sourceInfo);
} catch (IOException e) {
throw new ProxyCacheException("Error opening connection for " + this.sourceInfo.url + " with offset " + j, e);
}
}
private long readSourceAvailableBytes(HttpURLConnection httpURLConnection, long j, int i) throws IOException {
long contentLength = getContentLength(httpURLConnection);
return i == 200 ? contentLength : i == 206 ? contentLength + j : this.sourceInfo.length;
}
private long getContentLength(HttpURLConnection httpURLConnection) {
String headerField = httpURLConnection.getHeaderField("Content-Length");
if (headerField == null) {
return -1L;
}
return Long.parseLong(headerField);
}
@Override
public void close() throws ProxyCacheException {
HttpURLConnection httpURLConnection = this.connection;
if (httpURLConnection != null) {
try {
httpURLConnection.disconnect();
} catch (ArrayIndexOutOfBoundsException e) {
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 :(", e);
} catch (IllegalArgumentException e2) {
e = e2;
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 https://github.com/danikula or create issue here https://github.com/danikula/AndroidVideoCache/issues.", e);
} catch (NullPointerException 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 https://github.com/danikula or create issue here https://github.com/danikula/AndroidVideoCache/issues.", e);
}
}
}
@Override
public int read(byte[] bArr) throws ProxyCacheException {
InputStream inputStream = this.inputStream;
if (inputStream == null) {
throw new ProxyCacheException("Error reading data from " + this.sourceInfo.url + ": connection is absent!");
}
try {
return inputStream.read(bArr, 0, bArr.length);
} catch (InterruptedIOException e) {
throw new InterruptedProxyCacheException("Reading source " + this.sourceInfo.url + " is interrupted", e);
} catch (IOException e2) {
throw new ProxyCacheException("Error reading data from " + this.sourceInfo.url, e2);
}
}
private void fetchContentInfo() throws ProxyCacheException {
HttpURLConnection httpURLConnection;
InputStream inputStream = null;
try {
httpURLConnection = openConnection(0L, 10000);
try {
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, this.sourceInfo);
ProxyCacheUtils.close(inputStream);
if (httpURLConnection == null) {
return;
}
} catch (IOException e) {
e = e;
HttpProxyCacheDebuger.printfError("Error fetching info from " + this.sourceInfo.url, e);
ProxyCacheUtils.close(inputStream);
if (httpURLConnection == null) {
return;
}
httpURLConnection.disconnect();
}
} catch (Throwable th) {
th = th;
ProxyCacheUtils.close(inputStream);
if (httpURLConnection != null) {
httpURLConnection.disconnect();
}
throw th;
}
} catch (IOException e2) {
e = e2;
httpURLConnection = null;
} catch (Throwable th2) {
th = th2;
httpURLConnection = null;
ProxyCacheUtils.close(inputStream);
if (httpURLConnection != null) {
}
throw th;
}
httpURLConnection.disconnect();
}
private HttpURLConnection openConnection(long j, int i) throws IOException, ProxyCacheException {
HttpURLConnection httpURLConnection;
boolean z;
String str = this.sourceInfo.url;
int i2 = 0;
do {
if (str.startsWith(b.a) && this.v != null && this.trustAllCerts != null) {
httpURLConnection = (HttpURLConnection) new URL(str).openConnection();
((HttpsURLConnection) httpURLConnection).setHostnameVerifier(this.v);
try {
SSLContext sSLContext = SSLContext.getInstance("SSL");
sSLContext.init(null, this.trustAllCerts, new SecureRandom());
((HttpsURLConnection) httpURLConnection).setSSLSocketFactory(sSLContext.getSocketFactory());
((HttpsURLConnection) httpURLConnection).setHostnameVerifier(this.v);
} catch (KeyManagementException | NoSuchAlgorithmException e) {
e.printStackTrace();
}
} else {
httpURLConnection = (HttpURLConnection) new URL(str).openConnection();
}
injectCustomHeaders(httpURLConnection, str);
if (j > 0) {
if (j > 2147483647L) {
String valueOf = String.valueOf(j);
long longValue = valueOf.contains(".") ? Long.valueOf(valueOf.substring(0, valueOf.indexOf("."))).longValue() : j;
httpURLConnection.setRequestProperty("Range", "bytes=" + longValue + "-");
} else {
httpURLConnection.setRequestProperty("Range", "bytes=" + ((int) j) + "-");
}
}
if (i > 0) {
httpURLConnection.setConnectTimeout(i);
httpURLConnection.setReadTimeout(i);
}
int responseCode = httpURLConnection.getResponseCode();
z = responseCode == 301 || responseCode == 302 || responseCode == 303;
if (z) {
str = httpURLConnection.getHeaderField("Location");
i2++;
httpURLConnection.disconnect();
}
if (i2 > 5) {
throw new ProxyCacheException("Too many redirects: " + i2);
}
} while (z);
return httpURLConnection;
}
private void injectCustomHeaders(HttpURLConnection httpURLConnection, String str) {
Map<String, String> addHeaders = this.headerInjector.addHeaders(str);
if (addHeaders == null) {
return;
}
HttpProxyCacheDebuger.printfError("****** injectCustomHeaders ****** :" + addHeaders.size());
for (Map.Entry<String, String> entry : addHeaders.entrySet()) {
httpURLConnection.setRequestProperty(entry.getKey(), entry.getValue());
}
}
public synchronized String getMime() throws ProxyCacheException {
if (TextUtils.isEmpty(this.sourceInfo.mime)) {
fetchContentInfo();
}
return this.sourceInfo.mime;
}
public String getUrl() {
return this.sourceInfo.url;
}
public String toString() {
return "HttpUrlSource{sourceInfo='" + this.sourceInfo + i.d;
}
}