package com.live.eggplant.player.videocache; import android.text.TextUtils; import com.alipay.sdk.util.i; import com.live.eggplant.player.videocache.headers.EmptyHeadersInjector; import com.live.eggplant.player.videocache.headers.HeaderInjector; import com.live.eggplant.player.videocache.sourcestorage.SourceInfoStorage; import com.live.eggplant.player.videocache.sourcestorage.SourceInfoStorageFactory; import com.xiaomi.mipush.sdk.Constants; 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(HttpUrlSource httpUrlSource) { this.sourceInfo = httpUrlSource.sourceInfo; this.sourceInfoStorage = httpUrlSource.sourceInfoStorage; this.headerInjector = httpUrlSource.headerInjector; } public HttpUrlSource(String str) { this(str, SourceInfoStorageFactory.newEmptySourceInfoStorage()); } public HttpUrlSource(String str, SourceInfoStorage sourceInfoStorage2) { this(str, sourceInfoStorage2, new EmptyHeadersInjector()); } public HttpUrlSource(String str, SourceInfoStorage sourceInfoStorage2, HeaderInjector headerInjector2) { this.sourceInfoStorage = (SourceInfoStorage) Preconditions.checkNotNull(sourceInfoStorage2); this.headerInjector = (HeaderInjector) Preconditions.checkNotNull(headerInjector2); SourceInfo sourceInfo2 = sourceInfoStorage2.get(str); this.sourceInfo = sourceInfo2 == null ? new SourceInfo(str, -2147483648L, ProxyCacheUtils.c(str)) : sourceInfo2; } /* JADX WARNING: Removed duplicated region for block: B:20:0x0060 */ /* JADX WARNING: Removed duplicated region for block: B:24:? A[RETURN, SYNTHETIC] */ private void fetchContentInfo() throws ProxyCacheException { Throwable th; HttpURLConnection httpURLConnection; IOException e; InputStream inputStream2 = null; try { httpURLConnection = openConnection(0, 10000); try { long contentLength = getContentLength(httpURLConnection); String contentType = httpURLConnection.getContentType(); inputStream2 = httpURLConnection.getInputStream(); this.sourceInfo = new SourceInfo(this.sourceInfo.url, contentLength, contentType); this.sourceInfoStorage.put(this.sourceInfo.url, this.sourceInfo); ProxyCacheUtils.a(inputStream2); if (httpURLConnection == null) { return; } } catch (IOException e2) { e = e2; try { HttpProxyCacheDebuger.printfError("Error fetching info from " + this.sourceInfo.url, e); ProxyCacheUtils.a(inputStream2); if (httpURLConnection == null) { return; } httpURLConnection.disconnect(); } catch (Throwable th2) { th = th2; ProxyCacheUtils.a(inputStream2); if (httpURLConnection != null) { httpURLConnection.disconnect(); } throw th; } } } catch (IOException e3) { e = e3; httpURLConnection = null; HttpProxyCacheDebuger.printfError("Error fetching info from " + this.sourceInfo.url, e); ProxyCacheUtils.a(inputStream2); if (httpURLConnection == null) { } httpURLConnection.disconnect(); } catch (Throwable th3) { th = th3; httpURLConnection = null; ProxyCacheUtils.a(inputStream2); if (httpURLConnection != null) { } throw th; } httpURLConnection.disconnect(); } private long getContentLength(HttpURLConnection httpURLConnection) { String headerField = httpURLConnection.getHeaderField("Content-Length"); if (headerField == null) { return -1; } return Long.parseLong(headerField); } private void injectCustomHeaders(HttpURLConnection httpURLConnection, String str) { Map<String, String> addHeaders = this.headerInjector.addHeaders(str); if (addHeaders != null) { HttpProxyCacheDebuger.printfError("****** injectCustomHeaders ****** :" + addHeaders.size()); for (Map.Entry<String, String> entry : addHeaders.entrySet()) { httpURLConnection.setRequestProperty(entry.getKey(), entry.getValue()); } } } private HttpURLConnection openConnection(long j, int i) throws IOException, ProxyCacheException { HttpURLConnection httpURLConnection; boolean z; String str = this.sourceInfo.url; int i2 = 0; do { httpURLConnection = (HttpURLConnection) new URL(str).openConnection(); injectCustomHeaders(httpURLConnection, str); if (j > 0) { httpURLConnection.setRequestProperty("Range", "bytes=" + j + Constants.ACCEPT_TIME_SEPARATOR_SERVER); } 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 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; } @Override // com.live.eggplant.player.videocache.Source public void close() throws ProxyCacheException { HttpURLConnection httpURLConnection = this.connection; 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 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); } } } public synchronized String getMime() throws ProxyCacheException { if (TextUtils.isEmpty(this.sourceInfo.mime)) { fetchContentInfo(); } return this.sourceInfo.mime; } public String getUrl() { return this.sourceInfo.url; } @Override // com.live.eggplant.player.videocache.Source public synchronized long length() throws ProxyCacheException { if (this.sourceInfo.length == -2147483648L) { fetchContentInfo(); } return this.sourceInfo.length; } @Override // com.live.eggplant.player.videocache.Source public void open(long j) throws ProxyCacheException { try { this.connection = openConnection(j, -1); String contentType = this.connection.getContentType(); this.inputStream = new BufferedInputStream(this.connection.getInputStream(), 8192); this.sourceInfo = new SourceInfo(this.sourceInfo.url, readSourceAvailableBytes(this.connection, j, this.connection.getResponseCode()), contentType); this.sourceInfoStorage.put(this.sourceInfo.url, this.sourceInfo); } catch (IOException e) { throw new ProxyCacheException("Error opening connection for " + this.sourceInfo.url + " with offset " + j, e); } } @Override // com.live.eggplant.player.videocache.Source public int read(byte[] bArr) throws ProxyCacheException { InputStream inputStream2 = this.inputStream; if (inputStream2 != null) { try { return inputStream2.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); } } else { throw new ProxyCacheException("Error reading data from " + this.sourceInfo.url + ": connection is absent!"); } } public String toString() { return "HttpUrlSource{sourceInfo='" + this.sourceInfo + i.d; } }