package com.huawei.hms.availableupdate; import android.content.Context; import com.heytap.mcssdk.a.b; import com.huawei.hms.support.log.HMSLog; import com.huawei.hms.utils.IOUtils; import com.huawei.secure.android.common.ssl.SecureSSLSocketFactory; import com.xiaomi.mipush.sdk.Constants; import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.URL; import java.net.URLConnection; import java.security.KeyManagementException; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; import java.security.cert.CertificateException; import javax.net.ssl.HttpsURLConnection; /* compiled from: HttpRequestHelper */ public class k implements l { public HttpsURLConnection a; public volatile int b = -1; @Override // com.huawei.hms.availableupdate.l public void a() { this.b = 1; } @Override // com.huawei.hms.availableupdate.l public void close() { this.b = -1; HttpsURLConnection httpsURLConnection = this.a; if (httpsURLConnection != null) { httpsURLConnection.disconnect(); } } @Override // com.huawei.hms.availableupdate.l public int a(String str, OutputStream outputStream, int i, int i2, Context context) throws IOException, j { InputStream inputStream = null; try { a(str, context); if (this.a == null) { HMSLog.i("HttpRequestHelper", "mConnection is null"); return -1; } this.a.setRequestMethod("GET"); if (i > 0) { HttpsURLConnection httpsURLConnection = this.a; httpsURLConnection.addRequestProperty("Range", "bytes=" + i + Constants.ACCEPT_TIME_SEPARATOR_SERVER + i2); } int responseCode = this.a.getResponseCode(); if ((i > 0 && responseCode == 206) || (i <= 0 && responseCode == 200)) { inputStream = this.a.getInputStream(); a(new BufferedInputStream(inputStream, b.a), outputStream); outputStream.flush(); } IOUtils.closeQuietly(inputStream); return responseCode; } finally { IOUtils.closeQuietly(inputStream); } } public final void a(String str, Context context) throws IOException { if (this.b == 0) { HMSLog.e("HttpRequestHelper", "Not allowed to repeat open http(s) connection."); } URLConnection openConnection = new URL(str).openConnection(); if (openConnection == null) { HMSLog.i("HttpRequestHelper", "urlConnection is null"); } else if (openConnection instanceof HttpsURLConnection) { this.a = (HttpsURLConnection) openConnection; try { SecureSSLSocketFactory instance = SecureSSLSocketFactory.getInstance(context); if (instance != null) { this.a.setSSLSocketFactory(instance); } this.a.setSSLSocketFactory(instance); this.a.setConnectTimeout(30000); this.a.setReadTimeout(30000); this.a.setDoInput(true); this.a.setDoOutput(true); this.a.setUseCaches(false); this.a.setInstanceFollowRedirects(true); this.b = 0; } catch (IOException | IllegalAccessException | IllegalArgumentException | KeyManagementException | KeyStoreException | NoSuchAlgorithmException | CertificateException e) { HMSLog.e("HttpRequestHelper", "Failed to new TLSSocketFactory instance." + e.getMessage()); throw new IOException("Failed to create SSLSocketFactory."); } } else { HMSLog.i("HttpRequestHelper", "current request is http not allow connection"); this.a = null; } } public final void a(InputStream inputStream, OutputStream outputStream) throws IOException, j { byte[] bArr = new byte[b.a]; do { int read = inputStream.read(bArr); if (-1 != read) { outputStream.write(bArr, 0, read); } else { return; } } while (this.b != 1); throw new j("HTTP(s) request was canceled."); } }