package com.danikula.videocache; import android.text.TextUtils; 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 org.slf4j.Logger; import org.slf4j.LoggerFactory; public class HttpUrlSource implements Source { private static final Logger LOG = LoggerFactory.getLogger("HttpUrlSource"); private static final int MAX_REDIRECTS = 5; private HttpURLConnection connection; private InputStream inputStream; private SourceInfo sourceInfo; private final SourceInfoStorage sourceInfoStorage; public HttpUrlSource(String str) { this(str, SourceInfoStorageFactory.newEmptySourceInfoStorage()); } public HttpUrlSource(String str, SourceInfoStorage sourceInfoStorage2) { this.sourceInfoStorage = (SourceInfoStorage) Preconditions.checkNotNull(sourceInfoStorage2); SourceInfo sourceInfo2 = sourceInfoStorage2.get(str); this.sourceInfo = sourceInfo2 == null ? new SourceInfo(str, -2147483648L, ProxyCacheUtils.getSupposablyMime(str)) : sourceInfo2; } public HttpUrlSource(HttpUrlSource httpUrlSource) { this.sourceInfo = httpUrlSource.sourceInfo; this.sourceInfoStorage = httpUrlSource.sourceInfoStorage; } @Override // com.danikula.videocache.Source public synchronized long length() throws ProxyCacheException { if (this.sourceInfo.length == -2147483648L) { fetchContentInfo(); } return this.sourceInfo.length; } @Override // com.danikula.videocache.Source 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); SourceInfo sourceInfo2 = new SourceInfo(this.sourceInfo.url, readSourceAvailableBytes(this.connection, j, this.connection.getResponseCode()), contentType); this.sourceInfo = sourceInfo2; this.sourceInfoStorage.put(sourceInfo2.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); if (i == 200) { return contentLength; } if (i == 206) { return contentLength + j; } return this.sourceInfo.length; } private long getContentLength(HttpURLConnection httpURLConnection) { String headerField = httpURLConnection.getHeaderField("Content-Length"); if (headerField == null) { return -1; } return Long.parseLong(headerField); } @Override // com.danikula.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) { LOG.error("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 :(", (Throwable) e2); } } } @Override // com.danikula.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!"); } } /* JADX WARNING: Removed duplicated region for block: B:20:0x0092 */ /* JADX WARNING: Removed duplicated region for block: B:24:? A[RETURN, SYNTHETIC] */ private void fetchContentInfo() throws ProxyCacheException { Throwable th; HttpURLConnection httpURLConnection; IOException e; LOG.debug("Read content info from " + this.sourceInfo.url); InputStream inputStream2 = null; try { httpURLConnection = openConnection(0, 10000); try { long contentLength = getContentLength(httpURLConnection); String contentType = httpURLConnection.getContentType(); inputStream2 = httpURLConnection.getInputStream(); SourceInfo sourceInfo2 = new SourceInfo(this.sourceInfo.url, contentLength, contentType); this.sourceInfo = sourceInfo2; this.sourceInfoStorage.put(sourceInfo2.url, this.sourceInfo); LOG.debug("Source info fetched: " + this.sourceInfo); ProxyCacheUtils.close(inputStream2); if (httpURLConnection == null) { return; } } catch (IOException e2) { e = e2; try { LOG.error("Error fetching info from " + this.sourceInfo.url, (Throwable) e); ProxyCacheUtils.close(inputStream2); if (httpURLConnection == null) { return; } httpURLConnection.disconnect(); } catch (Throwable th2) { th = th2; ProxyCacheUtils.close(inputStream2); if (httpURLConnection != null) { httpURLConnection.disconnect(); } throw th; } } } catch (IOException e3) { e = e3; httpURLConnection = null; LOG.error("Error fetching info from " + this.sourceInfo.url, (Throwable) e); ProxyCacheUtils.close(inputStream2); if (httpURLConnection == null) { } httpURLConnection.disconnect(); } catch (Throwable th3) { th = th3; httpURLConnection = null; ProxyCacheUtils.close(inputStream2); if (httpURLConnection != null) { } throw th; } httpURLConnection.disconnect(); } private HttpURLConnection openConnection(long j, int i) throws IOException, ProxyCacheException { String str; HttpURLConnection httpURLConnection; boolean z; String str2 = this.sourceInfo.url; int i2 = 0; do { Logger logger = LOG; StringBuilder sb = new StringBuilder(); sb.append("Open connection "); int i3 = (j > 0 ? 1 : (j == 0 ? 0 : -1)); if (i3 > 0) { str = " with offset " + j; } else { str = ""; } sb.append(str); sb.append(" to "); sb.append(str2); logger.debug(sb.toString()); httpURLConnection = (HttpURLConnection) new URL(str2).openConnection(); if (i3 > 0) { httpURLConnection.setRequestProperty("Range", "bytes=" + j + "-"); } if (i > 0) { httpURLConnection.setConnectTimeout(i); httpURLConnection.setReadTimeout(i); } int responseCode = httpURLConnection.getResponseCode(); z = responseCode == 301 || responseCode == 302 || responseCode == 303; if (z) { str2 = httpURLConnection.getHeaderField("Location"); i2++; httpURLConnection.disconnect(); } if (i2 > 5) { throw new ProxyCacheException("Too many redirects: " + i2); } } while (z); return httpURLConnection; } 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 + "}"; } }