package csdk.glucentralservices.network; import android.text.TextUtils; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import com.google.firebase.perf.network.FirebasePerfUrlConnection; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintStream; import java.net.URL; import java.net.URLConnection; import java.util.Collections; import java.util.Map; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLHandshakeException; import org.apache.http.client.methods.HttpPut; /* loaded from: classes4.dex */ public class NetworkUtils { private static final int REQUEST_TIME_OUT_MS = 3000; /* JADX WARN: Multi-variable type inference failed */ /* JADX WARN: Type inference failed for: r1v0 */ /* JADX WARN: Type inference failed for: r1v1 */ /* JADX WARN: Type inference failed for: r1v2, types: [java.io.Closeable] */ /* JADX WARN: Type inference failed for: r1v3 */ /* JADX WARN: Type inference failed for: r3v1 */ /* JADX WARN: Type inference failed for: r3v4, types: [java.net.HttpURLConnection] */ /* JADX WARN: Type inference failed for: r3v8 */ /* JADX WARN: Type inference failed for: r3v9 */ public static NetworkResponse sendRequest(String str, URL url, Map map, String str2, long j) { ?? r3; HttpsURLConnection httpsURLConnection; InputStream inputStream; ?? r1 = 0; r1 = 0; try { try { httpsURLConnection = openConnection(str, url, map, str2, j); } catch (Throwable th) { th = th; r1 = map; r3 = str; } try { int responseCode = httpsURLConnection.getResponseCode(); inputStream = responseCode >= 400 ? httpsURLConnection.getErrorStream() : httpsURLConnection.getInputStream(); try { NetworkResponse networkResponse = new NetworkResponse(responseCode, readAsByteArrayAndClose(inputStream), httpsURLConnection.getHeaderFields(), null); close(inputStream); httpsURLConnection.disconnect(); return networkResponse; } catch (SSLHandshakeException e) { e = e; PrintStream printStream = System.out; printStream.println("SSLHandshakeException occurred: " + e.getMessage()); Throwable cause = e.getCause(); if (cause != null) { printStream.println("Cause: " + cause.getMessage()); } e.printStackTrace(); NetworkResponse networkResponse2 = new NetworkResponse(-1, null, null, e); close(inputStream); if (httpsURLConnection != null) { httpsURLConnection.disconnect(); } return networkResponse2; } catch (Exception e2) { e = e2; System.out.println("Exception type: " + e.getClass()); NetworkResponse networkResponse3 = new NetworkResponse(-1, null, null, e); close(inputStream); if (httpsURLConnection != null) { httpsURLConnection.disconnect(); } return networkResponse3; } } catch (SSLHandshakeException e3) { e = e3; inputStream = null; } catch (Exception e4) { e = e4; inputStream = null; } catch (Throwable th2) { th = th2; r3 = httpsURLConnection; close(r1); if (r3 != 0) { r3.disconnect(); } throw th; } } catch (SSLHandshakeException e5) { e = e5; httpsURLConnection = null; inputStream = null; } catch (Exception e6) { e = e6; httpsURLConnection = null; inputStream = null; } catch (Throwable th3) { th = th3; r3 = 0; } } /* JADX WARN: Multi-variable type inference failed */ /* JADX WARN: Not initialized variable reg: 10, insn: 0x002f: MOVE (r0 I:??[OBJECT, ARRAY]) = (r10 I:??[OBJECT, ARRAY]), block:B:35:0x002f */ /* JADX WARN: Type inference failed for: r7v0, types: [java.net.URL] */ /* JADX WARN: Type inference failed for: r7v1 */ /* JADX WARN: Type inference failed for: r7v2 */ /* JADX WARN: Type inference failed for: r7v3, types: [java.net.HttpURLConnection] */ /* JADX WARN: Type inference failed for: r7v4, types: [java.net.HttpURLConnection] */ /* JADX WARN: Type inference failed for: r7v5, types: [java.net.HttpURLConnection, java.net.URLConnection, javax.net.ssl.HttpsURLConnection] */ public static NetworkResponse downloadIntoFile(URL url, File file, long j) { InputStream inputStream; Closeable closeable; Closeable closeable2 = null; try { try { url = openConnection("GET", url, Collections.emptyMap(), null, j); } catch (Throwable th) { th = th; closeable2 = closeable; } try { int responseCode = url.getResponseCode(); if (responseCode >= 200 && responseCode < 300) { inputStream = url.getInputStream(); try { copyIntoFile(inputStream, file); NetworkResponse networkResponse = new NetworkResponse(responseCode, null, null, null); close(inputStream); url.disconnect(); return networkResponse; } catch (Exception e) { e = e; NetworkResponse networkResponse2 = new NetworkResponse(-1, null, null, e); close(inputStream); if (url != 0) { url.disconnect(); } return networkResponse2; } } NetworkResponse networkResponse3 = new NetworkResponse(responseCode, null, null, null); close(null); url.disconnect(); return networkResponse3; } catch (Exception e2) { e = e2; inputStream = null; } catch (Throwable th2) { th = th2; close(closeable2); if (url != 0) { url.disconnect(); } throw th; } } catch (Exception e3) { e = e3; url = 0; inputStream = null; } catch (Throwable th3) { th = th3; url = 0; } } public static void copyData(@NonNull InputStream inputStream, @NonNull OutputStream outputStream) throws IOException { byte[] bArr = new byte[16384]; while (true) { int read = inputStream.read(bArr); if (read < 0) { return; } else { outputStream.write(bArr, 0, read); } } } public static void copyIntoFile(@NonNull InputStream inputStream, @NonNull File file) throws IOException { FileOutputStream fileOutputStream = new FileOutputStream(file); try { copyData(inputStream, fileOutputStream); } finally { fileOutputStream.close(); } } private static HttpsURLConnection openConnection(String str, URL url, Map map, String str2, long j) throws IOException { boolean z = TextUtils.equals(str, "POST") || TextUtils.equals(str, HttpPut.METHOD_NAME); HttpsURLConnection httpsURLConnection = (HttpsURLConnection) ((URLConnection) FirebasePerfUrlConnection.instrument(url.openConnection())); GluTLS12SocketFactory.fixTLSPre21(httpsURLConnection); httpsURLConnection.setRequestMethod(str); for (Map.Entry entry : map.entrySet()) { httpsURLConnection.setRequestProperty(entry.getKey(), entry.getValue()); } httpsURLConnection.setAllowUserInteraction(false); httpsURLConnection.setConnectTimeout(3000); httpsURLConnection.setReadTimeout((int) j); httpsURLConnection.setUseCaches(false); if (z) { httpsURLConnection.setDoOutput(true); httpsURLConnection.connect(); OutputStream outputStream = httpsURLConnection.getOutputStream(); try { outputStream.write(str2.getBytes("UTF-8")); } finally { outputStream.close(); } } else { httpsURLConnection.connect(); } return httpsURLConnection; } private static byte[] readAsByteArrayAndClose(InputStream inputStream) throws IOException { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); try { copyData(inputStream, byteArrayOutputStream); return byteArrayOutputStream.toByteArray(); } finally { byteArrayOutputStream.close(); } } private static void close(@Nullable Closeable closeable) { if (closeable == null) { return; } try { closeable.close(); } catch (IOException unused) { } } }