package com.mbridge.msdk.playercommon.exoplayer2.upstream; import android.net.Uri; import android.util.Log; import com.google.firebase.perf.network.FirebasePerfUrlConnection; import com.mbridge.msdk.foundation.download.Command; import com.mbridge.msdk.playercommon.exoplayer2.upstream.HttpDataSource; import com.mbridge.msdk.playercommon.exoplayer2.util.Assertions; import com.mbridge.msdk.playercommon.exoplayer2.util.Predicate; import com.mbridge.msdk.playercommon.exoplayer2.util.Util; import java.io.EOFException; import java.io.IOException; import java.io.InputStream; import java.io.InterruptedIOException; import java.io.OutputStream; import java.lang.reflect.Method; import java.net.HttpURLConnection; import java.net.NoRouteToHostException; import java.net.ProtocolException; import java.net.URL; import java.net.URLConnection; import java.util.List; import java.util.Map; import java.util.concurrent.atomic.AtomicReference; import java.util.regex.Pattern; import org.apache.http.protocol.HTTP; /* loaded from: classes4.dex */ public class DefaultHttpDataSource implements HttpDataSource { public static final int DEFAULT_CONNECT_TIMEOUT_MILLIS = 8000; public static final int DEFAULT_READ_TIMEOUT_MILLIS = 8000; private static final long MAX_BYTES_TO_DRAIN = 2048; private static final int MAX_REDIRECTS = 20; private static final String TAG = "DefaultHttpDataSource"; private final boolean allowCrossProtocolRedirects; private long bytesRead; private long bytesSkipped; private long bytesToRead; private long bytesToSkip; private final int connectTimeoutMillis; private HttpURLConnection connection; private final Predicate contentTypePredicate; private DataSpec dataSpec; private final HttpDataSource.RequestProperties defaultRequestProperties; private InputStream inputStream; private final TransferListener listener; private boolean opened; private final int readTimeoutMillis; private final HttpDataSource.RequestProperties requestProperties; private final String userAgent; private static final Pattern CONTENT_RANGE_HEADER = Pattern.compile("^bytes (\\d+)-(\\d+)/(\\d+)$"); private static final AtomicReference skipBufferReference = new AtomicReference<>(); public final long bytesRead() { return this.bytesRead; } public final long bytesRemaining() { long j = this.bytesToRead; return j == -1 ? j : j - this.bytesRead; } public final long bytesSkipped() { return this.bytesSkipped; } public final HttpURLConnection getConnection() { return this.connection; } public DefaultHttpDataSource(String str, Predicate predicate) { this(str, predicate, null); } public DefaultHttpDataSource(String str, Predicate predicate, TransferListener transferListener) { this(str, predicate, transferListener, 8000, 8000); } public DefaultHttpDataSource(String str, Predicate predicate, TransferListener transferListener, int i, int i2) { this(str, predicate, transferListener, i, i2, false, null); } public DefaultHttpDataSource(String str, Predicate predicate, TransferListener transferListener, int i, int i2, boolean z, HttpDataSource.RequestProperties requestProperties) { this.userAgent = Assertions.checkNotEmpty(str); this.contentTypePredicate = predicate; this.listener = transferListener; this.requestProperties = new HttpDataSource.RequestProperties(); this.connectTimeoutMillis = i; this.readTimeoutMillis = i2; this.allowCrossProtocolRedirects = z; this.defaultRequestProperties = requestProperties; } @Override // com.mbridge.msdk.playercommon.exoplayer2.upstream.DataSource public Uri getUri() { HttpURLConnection httpURLConnection = this.connection; if (httpURLConnection == null) { return null; } return Uri.parse(httpURLConnection.getURL().toString()); } @Override // com.mbridge.msdk.playercommon.exoplayer2.upstream.HttpDataSource public Map> getResponseHeaders() { HttpURLConnection httpURLConnection = this.connection; if (httpURLConnection == null) { return null; } return httpURLConnection.getHeaderFields(); } @Override // com.mbridge.msdk.playercommon.exoplayer2.upstream.HttpDataSource public void setRequestProperty(String str, String str2) { Assertions.checkNotNull(str); Assertions.checkNotNull(str2); this.requestProperties.set(str, str2); } @Override // com.mbridge.msdk.playercommon.exoplayer2.upstream.HttpDataSource public void clearRequestProperty(String str) { Assertions.checkNotNull(str); this.requestProperties.remove(str); } @Override // com.mbridge.msdk.playercommon.exoplayer2.upstream.HttpDataSource public void clearAllRequestProperties() { this.requestProperties.clear(); } @Override // com.mbridge.msdk.playercommon.exoplayer2.upstream.HttpDataSource, com.mbridge.msdk.playercommon.exoplayer2.upstream.DataSource public long open(DataSpec dataSpec) throws HttpDataSource.HttpDataSourceException { this.dataSpec = dataSpec; long j = 0; this.bytesRead = 0L; this.bytesSkipped = 0L; try { HttpURLConnection makeConnection = makeConnection(dataSpec); this.connection = makeConnection; try { int responseCode = makeConnection.getResponseCode(); if (responseCode < 200 || responseCode > 299) { Map> headerFields = this.connection.getHeaderFields(); closeConnectionQuietly(); HttpDataSource.InvalidResponseCodeException invalidResponseCodeException = new HttpDataSource.InvalidResponseCodeException(responseCode, headerFields, dataSpec); if (responseCode == 416) { invalidResponseCodeException.initCause(new DataSourceException(0)); throw invalidResponseCodeException; } throw invalidResponseCodeException; } String contentType = this.connection.getContentType(); Predicate predicate = this.contentTypePredicate; if (predicate != null && !predicate.evaluate(contentType)) { closeConnectionQuietly(); throw new HttpDataSource.InvalidContentTypeException(contentType, dataSpec); } if (responseCode == 200) { long j2 = dataSpec.position; if (j2 != 0) { j = j2; } } this.bytesToSkip = j; if (!dataSpec.isFlagSet(1)) { long j3 = dataSpec.length; if (j3 != -1) { this.bytesToRead = j3; } else { long contentLength = getContentLength(this.connection); this.bytesToRead = contentLength != -1 ? contentLength - this.bytesToSkip : -1L; } } else { this.bytesToRead = dataSpec.length; } try { this.inputStream = this.connection.getInputStream(); this.opened = true; TransferListener transferListener = this.listener; if (transferListener != null) { transferListener.onTransferStart(this, dataSpec); } return this.bytesToRead; } catch (IOException e) { closeConnectionQuietly(); throw new HttpDataSource.HttpDataSourceException(e, dataSpec, 1); } } catch (IOException e2) { closeConnectionQuietly(); throw new HttpDataSource.HttpDataSourceException("Unable to connect to " + dataSpec.uri.toString(), e2, dataSpec, 1); } } catch (IOException e3) { throw new HttpDataSource.HttpDataSourceException("Unable to connect to " + dataSpec.uri.toString(), e3, dataSpec, 1); } } @Override // com.mbridge.msdk.playercommon.exoplayer2.upstream.HttpDataSource, com.mbridge.msdk.playercommon.exoplayer2.upstream.DataSource public int read(byte[] bArr, int i, int i2) throws HttpDataSource.HttpDataSourceException { try { skipInternal(); return readInternal(bArr, i, i2); } catch (IOException e) { throw new HttpDataSource.HttpDataSourceException(e, this.dataSpec, 2); } } @Override // com.mbridge.msdk.playercommon.exoplayer2.upstream.HttpDataSource, com.mbridge.msdk.playercommon.exoplayer2.upstream.DataSource public void close() throws HttpDataSource.HttpDataSourceException { try { if (this.inputStream != null) { maybeTerminateInputStream(this.connection, bytesRemaining()); try { this.inputStream.close(); } catch (IOException e) { throw new HttpDataSource.HttpDataSourceException(e, this.dataSpec, 3); } } } finally { this.inputStream = null; closeConnectionQuietly(); if (this.opened) { this.opened = false; TransferListener transferListener = this.listener; if (transferListener != null) { transferListener.onTransferEnd(this); } } } } private HttpURLConnection makeConnection(DataSpec dataSpec) throws IOException { HttpURLConnection makeConnection; URL url = new URL(dataSpec.uri.toString()); byte[] bArr = dataSpec.postBody; long j = dataSpec.position; long j2 = dataSpec.length; boolean isFlagSet = dataSpec.isFlagSet(1); if (!this.allowCrossProtocolRedirects) { return makeConnection(url, bArr, j, j2, isFlagSet, true); } int i = 0; while (true) { int i2 = i + 1; if (i <= 20) { long j3 = j; makeConnection = makeConnection(url, bArr, j, j2, isFlagSet, false); int responseCode = makeConnection.getResponseCode(); if (responseCode == 300 || responseCode == 301 || responseCode == 302 || responseCode == 303 || (bArr == null && (responseCode == 307 || responseCode == 308))) { String headerField = makeConnection.getHeaderField("Location"); makeConnection.disconnect(); url = handleRedirect(url, headerField); bArr = null; i = i2; j = j3; } } else { throw new NoRouteToHostException("Too many redirects: " + i2); } } return makeConnection; } private HttpURLConnection makeConnection(URL url, byte[] bArr, long j, long j2, boolean z, boolean z2) throws IOException { HttpURLConnection httpURLConnection = (HttpURLConnection) ((URLConnection) FirebasePerfUrlConnection.instrument(url.openConnection())); httpURLConnection.setConnectTimeout(this.connectTimeoutMillis); httpURLConnection.setReadTimeout(this.readTimeoutMillis); HttpDataSource.RequestProperties requestProperties = this.defaultRequestProperties; if (requestProperties != null) { for (Map.Entry entry : requestProperties.getSnapshot().entrySet()) { httpURLConnection.setRequestProperty(entry.getKey(), entry.getValue()); } } for (Map.Entry entry2 : this.requestProperties.getSnapshot().entrySet()) { httpURLConnection.setRequestProperty(entry2.getKey(), entry2.getValue()); } if (j != 0 || j2 != -1) { String str = "bytes=" + j + "-"; if (j2 != -1) { str = str + ((j + j2) - 1); } httpURLConnection.setRequestProperty(Command.HTTP_HEADER_RANGE, str); } httpURLConnection.setRequestProperty("User-Agent", this.userAgent); if (!z) { httpURLConnection.setRequestProperty("Accept-Encoding", HTTP.IDENTITY_CODING); } httpURLConnection.setInstanceFollowRedirects(z2); httpURLConnection.setDoOutput(bArr != null); if (bArr != null) { httpURLConnection.setRequestMethod("POST"); if (bArr.length != 0) { httpURLConnection.setFixedLengthStreamingMode(bArr.length); httpURLConnection.connect(); OutputStream outputStream = httpURLConnection.getOutputStream(); outputStream.write(bArr); outputStream.close(); return httpURLConnection; } } httpURLConnection.connect(); return httpURLConnection; } private static URL handleRedirect(URL url, String str) throws IOException { if (str == null) { throw new ProtocolException("Null location redirect"); } URL url2 = new URL(url, str); String protocol = url2.getProtocol(); if ("https".equals(protocol) || "http".equals(protocol)) { return url2; } throw new ProtocolException("Unsupported protocol redirect: " + protocol); } /* JADX WARN: Removed duplicated region for block: B:24:? A[RETURN, SYNTHETIC] */ /* JADX WARN: Removed duplicated region for block: B:6:0x003a */ /* Code decompiled incorrectly, please refer to instructions dump. To view partially-correct add '--show-bad-code' argument */ private static long getContentLength(java.net.HttpURLConnection r10) { /* java.lang.String r0 = "Content-Length" java.lang.String r0 = r10.getHeaderField(r0) boolean r1 = android.text.TextUtils.isEmpty(r0) java.lang.String r2 = "]" java.lang.String r3 = "DefaultHttpDataSource" if (r1 != 0) goto L2c long r4 = java.lang.Long.parseLong(r0) // Catch: java.lang.NumberFormatException -> L15 goto L2e L15: java.lang.StringBuilder r1 = new java.lang.StringBuilder r1.() java.lang.String r4 = "Unexpected Content-Length [" r1.append(r4) r1.append(r0) r1.append(r2) java.lang.String r1 = r1.toString() android.util.Log.e(r3, r1) L2c: r4 = -1 L2e: java.lang.String r1 = "Content-Range" java.lang.String r10 = r10.getHeaderField(r1) boolean r1 = android.text.TextUtils.isEmpty(r10) if (r1 != 0) goto La3 java.util.regex.Pattern r1 = com.mbridge.msdk.playercommon.exoplayer2.upstream.DefaultHttpDataSource.CONTENT_RANGE_HEADER java.util.regex.Matcher r1 = r1.matcher(r10) boolean r6 = r1.find() if (r6 == 0) goto La3 r6 = 2 java.lang.String r6 = r1.group(r6) // Catch: java.lang.NumberFormatException -> L8c long r6 = java.lang.Long.parseLong(r6) // Catch: java.lang.NumberFormatException -> L8c r8 = 1 java.lang.String r1 = r1.group(r8) // Catch: java.lang.NumberFormatException -> L8c long r8 = java.lang.Long.parseLong(r1) // Catch: java.lang.NumberFormatException -> L8c long r6 = r6 - r8 r8 = 1 long r6 = r6 + r8 r8 = 0 int r1 = (r4 > r8 ? 1 : (r4 == r8 ? 0 : -1)) if (r1 >= 0) goto L64 r4 = r6 goto La3 L64: int r1 = (r4 > r6 ? 1 : (r4 == r6 ? 0 : -1)) if (r1 == 0) goto La3 java.lang.StringBuilder r1 = new java.lang.StringBuilder // Catch: java.lang.NumberFormatException -> L8c r1.() // Catch: java.lang.NumberFormatException -> L8c java.lang.String r8 = "Inconsistent headers [" r1.append(r8) // Catch: java.lang.NumberFormatException -> L8c r1.append(r0) // Catch: java.lang.NumberFormatException -> L8c java.lang.String r0 = "] [" r1.append(r0) // Catch: java.lang.NumberFormatException -> L8c r1.append(r10) // Catch: java.lang.NumberFormatException -> L8c r1.append(r2) // Catch: java.lang.NumberFormatException -> L8c java.lang.String r0 = r1.toString() // Catch: java.lang.NumberFormatException -> L8c android.util.Log.w(r3, r0) // Catch: java.lang.NumberFormatException -> L8c long r4 = java.lang.Math.max(r4, r6) // Catch: java.lang.NumberFormatException -> L8c goto La3 L8c: java.lang.StringBuilder r0 = new java.lang.StringBuilder r0.() java.lang.String r1 = "Unexpected Content-Range [" r0.append(r1) r0.append(r10) r0.append(r2) java.lang.String r10 = r0.toString() android.util.Log.e(r3, r10) La3: return r4 */ throw new UnsupportedOperationException("Method not decompiled: com.mbridge.msdk.playercommon.exoplayer2.upstream.DefaultHttpDataSource.getContentLength(java.net.HttpURLConnection):long"); } private void skipInternal() throws IOException { if (this.bytesSkipped == this.bytesToSkip) { return; } byte[] andSet = skipBufferReference.getAndSet(null); if (andSet == null) { andSet = new byte[4096]; } while (true) { long j = this.bytesSkipped; long j2 = this.bytesToSkip; if (j != j2) { int read = this.inputStream.read(andSet, 0, (int) Math.min(j2 - j, andSet.length)); if (Thread.currentThread().isInterrupted()) { throw new InterruptedIOException(); } if (read == -1) { throw new EOFException(); } this.bytesSkipped += read; TransferListener transferListener = this.listener; if (transferListener != null) { transferListener.onBytesTransferred(this, read); } } else { skipBufferReference.set(andSet); return; } } } private int readInternal(byte[] bArr, int i, int i2) throws IOException { if (i2 == 0) { return 0; } long j = this.bytesToRead; if (j != -1) { long j2 = j - this.bytesRead; if (j2 == 0) { return -1; } i2 = (int) Math.min(i2, j2); } int read = this.inputStream.read(bArr, i, i2); if (read == -1) { if (this.bytesToRead == -1) { return -1; } throw new EOFException(); } this.bytesRead += read; TransferListener transferListener = this.listener; if (transferListener != null) { transferListener.onBytesTransferred(this, read); } return read; } private static void maybeTerminateInputStream(HttpURLConnection httpURLConnection, long j) { int i = Util.SDK_INT; if (i == 19 || i == 20) { try { InputStream inputStream = httpURLConnection.getInputStream(); if (j == -1) { if (inputStream.read() == -1) { return; } } else if (j <= 2048) { return; } String name = inputStream.getClass().getName(); if ("com.android.okhttp.internal.http.HttpTransport$ChunkedInputStream".equals(name) || "com.android.okhttp.internal.http.HttpTransport$FixedLengthInputStream".equals(name)) { Method declaredMethod = inputStream.getClass().getSuperclass().getDeclaredMethod("unexpectedEndOfInput", new Class[0]); declaredMethod.setAccessible(true); declaredMethod.invoke(inputStream, new Object[0]); } } catch (Exception unused) { } } } private void closeConnectionQuietly() { HttpURLConnection httpURLConnection = this.connection; if (httpURLConnection != null) { try { httpURLConnection.disconnect(); } catch (Exception e) { Log.e(TAG, "Unexpected error while disconnecting", e); } this.connection = null; } } }