package epson.print.inkrpln;


import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;

public class SimpleHttpClient {
    /* JADX WARNING: Removed duplicated region for block: B:38:0x00b1  */
    /* JADX WARNING: Removed duplicated region for block: B:40:0x00b6  */
    /* JADX WARNING: Removed duplicated region for block: B:42:0x00bb  */
    @android.support.annotation.Nullable
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public static java.lang.String post(java.net.URL r4, @android.support.annotation.NonNull java.lang.String r5, @android.support.annotation.NonNull java.lang.String r6) throws java.io.IOException {
        /*
            java.lang.String r0 = "UTF-8"
            byte[] r5 = r5.getBytes(r0)
            r0 = 0
            java.net.URLConnection r4 = r4.openConnection()     // Catch:{ all -> 0x00ac }
            java.net.HttpURLConnection r4 = (java.net.HttpURLConnection) r4     // Catch:{ all -> 0x00ac }
            r1 = 3000(0xbb8, float:4.204E-42)
            r4.setReadTimeout(r1)     // Catch:{ all -> 0x00a9 }
            r4.setConnectTimeout(r1)     // Catch:{ all -> 0x00a9 }
            r1 = 1
            r4.setDoOutput(r1)     // Catch:{ all -> 0x00a9 }
            java.lang.String r1 = "POST"
            r4.setRequestMethod(r1)     // Catch:{ all -> 0x00a9 }
            java.lang.String r1 = "Content-Type"
            r4.setRequestProperty(r1, r6)     // Catch:{ all -> 0x00a9 }
            java.lang.String r6 = "Content-Length"
            int r1 = r5.length     // Catch:{ all -> 0x00a9 }
            java.lang.String r1 = java.lang.Integer.toString(r1)     // Catch:{ all -> 0x00a9 }
            r4.setRequestProperty(r6, r1)     // Catch:{ all -> 0x00a9 }
            java.io.DataOutputStream r6 = new java.io.DataOutputStream     // Catch:{ all -> 0x00a9 }
            java.io.OutputStream r1 = r4.getOutputStream()     // Catch:{ all -> 0x00a9 }
            r6.<init>(r1)     // Catch:{ all -> 0x00a9 }
            r6.write(r5)     // Catch:{ all -> 0x00a4 }
            r6.close()     // Catch:{ all -> 0x00a4 }
            r4.connect()     // Catch:{ all -> 0x00a9 }
            int r5 = r4.getResponseCode()     // Catch:{ all -> 0x00a9 }
            r6 = 200(0xc8, float:2.8E-43)
            if (r5 != r6) goto L_0x0088
            int r5 = r4.getContentLength()     // Catch:{ all -> 0x00a9 }
            if (r5 <= 0) goto L_0x006c
            java.io.InputStream r6 = r4.getInputStream()     // Catch:{ all -> 0x00a9 }
            if (r6 == 0) goto L_0x0064
            java.lang.String r5 = readStream(r6, r5)     // Catch:{ all -> 0x0062 }
            if (r4 == 0) goto L_0x005c
            r4.disconnect()
        L_0x005c:
            if (r6 == 0) goto L_0x0061
            r6.close()
        L_0x0061:
            return r5
        L_0x0062:
            r5 = move-exception
            goto L_0x00af
        L_0x0064:
            java.io.IOException r5 = new java.io.IOException     // Catch:{ all -> 0x0062 }
            java.lang.String r1 = "Connection#getInputStream() returns null"
            r5.<init>(r1)     // Catch:{ all -> 0x0062 }
            throw r5     // Catch:{ all -> 0x0062 }
        L_0x006c:
            java.io.IOException r6 = new java.io.IOException     // Catch:{ all -> 0x00a9 }
            java.lang.StringBuilder r1 = new java.lang.StringBuilder     // Catch:{ all -> 0x00a9 }
            r1.<init>()     // Catch:{ all -> 0x00a9 }
            java.lang.String r2 = "connection.getContentLength(); returns <"
            r1.append(r2)     // Catch:{ all -> 0x00a9 }
            r1.append(r5)     // Catch:{ all -> 0x00a9 }
            java.lang.String r5 = ">"
            r1.append(r5)     // Catch:{ all -> 0x00a9 }
            java.lang.String r5 = r1.toString()     // Catch:{ all -> 0x00a9 }
            r6.<init>(r5)     // Catch:{ all -> 0x00a9 }
            throw r6     // Catch:{ all -> 0x00a9 }
        L_0x0088:
            java.io.IOException r6 = new java.io.IOException     // Catch:{ all -> 0x00a9 }
            java.lang.StringBuilder r1 = new java.lang.StringBuilder     // Catch:{ all -> 0x00a9 }
            r1.<init>()     // Catch:{ all -> 0x00a9 }
            java.lang.String r2 = "Http error code: <"
            r1.append(r2)     // Catch:{ all -> 0x00a9 }
            r1.append(r5)     // Catch:{ all -> 0x00a9 }
            java.lang.String r5 = ">"
            r1.append(r5)     // Catch:{ all -> 0x00a9 }
            java.lang.String r5 = r1.toString()     // Catch:{ all -> 0x00a9 }
            r6.<init>(r5)     // Catch:{ all -> 0x00a9 }
            throw r6     // Catch:{ all -> 0x00a9 }
        L_0x00a4:
            r5 = move-exception
            r3 = r0
            r0 = r6
            r6 = r3
            goto L_0x00af
        L_0x00a9:
            r5 = move-exception
            r6 = r0
            goto L_0x00af
        L_0x00ac:
            r5 = move-exception
            r4 = r0
            r6 = r4
        L_0x00af:
            if (r4 == 0) goto L_0x00b4
            r4.disconnect()
        L_0x00b4:
            if (r0 == 0) goto L_0x00b9
            r0.close()
        L_0x00b9:
            if (r6 == 0) goto L_0x00be
            r6.close()
        L_0x00be:
            throw r5
        */
        throw new UnsupportedOperationException("Method not decompiled: epson.print.inkrpln.SimpleHttpClient.post(java.net.URL, java.lang.String, java.lang.String):java.lang.String");
    }

    /* JADX WARNING: Removed duplicated region for block: B:32:0x00b3  */
    /* JADX WARNING: Removed duplicated region for block: B:34:0x00b8  */
    @android.support.annotation.Nullable
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public static java.lang.String getString(java.net.URL r5) throws java.io.IOException {
        /*
            r0 = 0
            if (r5 == 0) goto L_0x00a9
            java.net.HttpURLConnection r5 = localOpenConnection(r5)     // Catch:{ all -> 0x00a6 }
            r1 = 3000(0xbb8, float:4.204E-42)
            r5.setReadTimeout(r1)     // Catch:{ all -> 0x00a4 }
            r5.setConnectTimeout(r1)     // Catch:{ all -> 0x00a4 }
            java.lang.String r1 = "GET"
            r5.setRequestMethod(r1)     // Catch:{ all -> 0x00a4 }
            java.lang.String r1 = "iprint:1Pr1nt$ncheck"
            java.lang.String r1 = epson.print.inkrpln.InkReplenishSystem.encodePrinterSerial(r1)     // Catch:{ all -> 0x00a4 }
            java.lang.String r2 = "Content-Type"
            java.lang.String r3 = "application/json"
            r5.setRequestProperty(r2, r3)     // Catch:{ all -> 0x00a4 }
            java.lang.String r2 = "Accept"
            java.lang.String r3 = "application/json"
            r5.setRequestProperty(r2, r3)     // Catch:{ all -> 0x00a4 }
            java.lang.String r2 = "Authorization"
            java.lang.StringBuilder r3 = new java.lang.StringBuilder     // Catch:{ all -> 0x00a4 }
            r3.<init>()     // Catch:{ all -> 0x00a4 }
            java.lang.String r4 = "Basic "
            r3.append(r4)     // Catch:{ all -> 0x00a4 }
            r3.append(r1)     // Catch:{ all -> 0x00a4 }
            java.lang.String r1 = r3.toString()     // Catch:{ all -> 0x00a4 }
            r5.setRequestProperty(r2, r1)     // Catch:{ all -> 0x00a4 }
            r5.connect()     // Catch:{ all -> 0x00a4 }
            int r1 = r5.getResponseCode()     // Catch:{ all -> 0x00a4 }
            r2 = 200(0xc8, float:2.8E-43)
            if (r1 != r2) goto L_0x0088
            int r1 = r5.getContentLength()     // Catch:{ all -> 0x00a4 }
            if (r1 <= 0) goto L_0x006c
            java.io.InputStream r0 = r5.getInputStream()     // Catch:{ all -> 0x00a4 }
            if (r0 == 0) goto L_0x0064
            java.lang.String r1 = readStream(r0, r1)     // Catch:{ all -> 0x00a4 }
            if (r5 == 0) goto L_0x005e
            r5.disconnect()
        L_0x005e:
            if (r0 == 0) goto L_0x0063
            r0.close()
        L_0x0063:
            return r1
        L_0x0064:
            java.io.IOException r1 = new java.io.IOException     // Catch:{ all -> 0x00a4 }
            java.lang.String r2 = "Connection#getInputStream() returns null"
            r1.<init>(r2)     // Catch:{ all -> 0x00a4 }
            throw r1     // Catch:{ all -> 0x00a4 }
        L_0x006c:
            java.io.IOException r2 = new java.io.IOException     // Catch:{ all -> 0x00a4 }
            java.lang.StringBuilder r3 = new java.lang.StringBuilder     // Catch:{ all -> 0x00a4 }
            r3.<init>()     // Catch:{ all -> 0x00a4 }
            java.lang.String r4 = "connection.getContentLength(); returns <"
            r3.append(r4)     // Catch:{ all -> 0x00a4 }
            r3.append(r1)     // Catch:{ all -> 0x00a4 }
            java.lang.String r1 = ">"
            r3.append(r1)     // Catch:{ all -> 0x00a4 }
            java.lang.String r1 = r3.toString()     // Catch:{ all -> 0x00a4 }
            r2.<init>(r1)     // Catch:{ all -> 0x00a4 }
            throw r2     // Catch:{ all -> 0x00a4 }
        L_0x0088:
            java.io.IOException r2 = new java.io.IOException     // Catch:{ all -> 0x00a4 }
            java.lang.StringBuilder r3 = new java.lang.StringBuilder     // Catch:{ all -> 0x00a4 }
            r3.<init>()     // Catch:{ all -> 0x00a4 }
            java.lang.String r4 = "Http error code: <"
            r3.append(r4)     // Catch:{ all -> 0x00a4 }
            r3.append(r1)     // Catch:{ all -> 0x00a4 }
            java.lang.String r1 = ">"
            r3.append(r1)     // Catch:{ all -> 0x00a4 }
            java.lang.String r1 = r3.toString()     // Catch:{ all -> 0x00a4 }
            r2.<init>(r1)     // Catch:{ all -> 0x00a4 }
            throw r2     // Catch:{ all -> 0x00a4 }
        L_0x00a4:
            r1 = move-exception
            goto L_0x00b1
        L_0x00a6:
            r1 = move-exception
            r5 = r0
            goto L_0x00b1
        L_0x00a9:
            java.io.IOException r5 = new java.io.IOException     // Catch:{ all -> 0x00a6 }
            java.lang.String r1 = "error: ink replenish server url"
            r5.<init>(r1)     // Catch:{ all -> 0x00a6 }
            throw r5     // Catch:{ all -> 0x00a6 }
        L_0x00b1:
            if (r5 == 0) goto L_0x00b6
            r5.disconnect()
        L_0x00b6:
            if (r0 == 0) goto L_0x00bb
            r0.close()
        L_0x00bb:
            throw r1
        */
        throw new UnsupportedOperationException("Method not decompiled: epson.print.inkrpln.SimpleHttpClient.getString(java.net.URL):java.lang.String");
    }

    @NonNull
    private static HttpURLConnection localOpenConnection(@NonNull URL url) throws IOException {
        HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection();
        httpsURLConnection.setSSLSocketFactory(new LocalSSLSocketFactory(httpsURLConnection.getSSLSocketFactory()));
        return httpsURLConnection;
    }

    /* JADX WARNING: Removed duplicated region for block: B:20:0x0034  */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    private static java.lang.String readStream(@android.support.annotation.NonNull java.io.InputStream r4, int r5) throws java.io.IOException {
        /*
            if (r5 >= 0) goto L_0x0005
            java.lang.String r4 = ""
            return r4
        L_0x0005:
            r0 = 0
            java.lang.StringBuffer r1 = new java.lang.StringBuffer
            r1.<init>()
            java.io.InputStreamReader r2 = new java.io.InputStreamReader     // Catch:{ all -> 0x0031 }
            java.lang.String r3 = "UTF-8"
            r2.<init>(r4, r3)     // Catch:{ all -> 0x0031 }
            char[] r4 = new char[r5]     // Catch:{ all -> 0x002e }
        L_0x0014:
            int r0 = r2.read(r4)     // Catch:{ all -> 0x002e }
            r3 = -1
            if (r0 == r3) goto L_0x0026
            if (r5 <= 0) goto L_0x0026
            if (r0 <= r5) goto L_0x0020
            r0 = r5
        L_0x0020:
            r3 = 0
            r1.append(r4, r3, r0)     // Catch:{ all -> 0x002e }
            int r5 = r5 - r0
            goto L_0x0014
        L_0x0026:
            r2.close()
            java.lang.String r4 = r1.toString()
            return r4
        L_0x002e:
            r4 = move-exception
            r0 = r2
            goto L_0x0032
        L_0x0031:
            r4 = move-exception
        L_0x0032:
            if (r0 == 0) goto L_0x0037
            r0.close()
        L_0x0037:
            throw r4
        */
        throw new UnsupportedOperationException("Method not decompiled: epson.print.inkrpln.SimpleHttpClient.readStream(java.io.InputStream, int):java.lang.String");
    }
}
