package K1;

import androidx.exifinterface.media.ExifInterface;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.Iterator;

/* JADX INFO: loaded from: classes2.dex */
public abstract class c extends J1.d {

    /* JADX INFO: renamed from: p, reason: collision with root package name */
    protected int f1205p;

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    protected ArrayList f1206q;

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    protected boolean f1207r;

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    protected String f1208s;

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    protected String f1209t;

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    protected J1.c f1210u;

    /* JADX INFO: renamed from: v, reason: collision with root package name */
    protected boolean f1211v;

    /* JADX INFO: renamed from: w, reason: collision with root package name */
    private boolean f1212w = true;

    /* JADX INFO: renamed from: x, reason: collision with root package name */
    protected BufferedReader f1213x;

    /* JADX INFO: renamed from: y, reason: collision with root package name */
    protected BufferedWriter f1214y;

    public c() {
        p(21);
        this.f1206q = new ArrayList();
        this.f1207r = false;
        this.f1208s = null;
        this.f1209t = "ISO-8859-1";
        this.f1210u = new J1.c(this);
    }

    private boolean E(String str) {
        return str.length() <= 3 || str.charAt(3) == '-' || !Character.isDigit(str.charAt(0));
    }

    private void L(String str) throws IOException {
        try {
            this.f1214y.write(str);
            this.f1214y.flush();
        } catch (SocketException e2) {
            if (!o()) {
                throw new h("Connection unexpectedly closed.");
            }
            throw e2;
        }
    }

    private boolean Q(String str, String str2) {
        return (str.startsWith(str2) && str.charAt(3) == ' ') ? false : true;
    }

    private String s(String str, String str2) {
        StringBuilder sb = new StringBuilder();
        sb.append(str);
        if (str2 != null) {
            sb.append(' ');
            sb.append(str2);
        }
        sb.append("\r\n");
        return sb.toString();
    }

    private int z(boolean z2) throws IOException {
        this.f1207r = true;
        this.f1206q.clear();
        String line = this.f1213x.readLine();
        if (line == null) {
            throw new h("Connection closed without indication.");
        }
        int length = line.length();
        if (length < 3) {
            throw new J1.a("Truncated server reply: " + line);
        }
        try {
            String strSubstring = line.substring(0, 3);
            this.f1205p = Integer.parseInt(strSubstring);
            this.f1206q.add(line);
            if (length > 3) {
                char cCharAt = line.charAt(3);
                if (cCharAt == '-') {
                    while (true) {
                        String line2 = this.f1213x.readLine();
                        if (line2 == null) {
                            throw new h("Connection closed without indication.");
                        }
                        this.f1206q.add(line2);
                        if (C()) {
                            if (!Q(line2, strSubstring)) {
                                break;
                            }
                        } else if (!E(line2)) {
                            break;
                        }
                    }
                } else if (D()) {
                    if (length == 4) {
                        throw new J1.a("Truncated server reply: '" + line + "'");
                    }
                    if (cCharAt != ' ') {
                        throw new J1.a("Invalid server reply: '" + line + "'");
                    }
                }
            } else if (D()) {
                throw new J1.a("Truncated server reply: '" + line + "'");
            }
            if (z2) {
                k(this.f1205p, B());
            }
            int i2 = this.f1205p;
            if (i2 != 421) {
                return i2;
            }
            throw new h("FTP response 421 received.  Server closed connection.");
        } catch (NumberFormatException unused) {
            throw new J1.a("Could not parse response code.\nServer Reply: " + line);
        }
    }

    public int A() {
        return this.f1205p;
    }

    public String B() {
        if (!this.f1207r) {
            return this.f1208s;
        }
        StringBuilder sb = new StringBuilder(256);
        Iterator it = this.f1206q.iterator();
        while (it.hasNext()) {
            sb.append((String) it.next());
            sb.append("\r\n");
        }
        this.f1207r = false;
        String string = sb.toString();
        this.f1208s = string;
        return string;
    }

    public boolean C() {
        return this.f1211v;
    }

    public boolean D() {
        return this.f1212w;
    }

    public int F(int i2) {
        return N(g.MODE, "AEILNTCFRPSBC".substring(i2, i2 + 1));
    }

    public int G(String str) {
        return N(g.PASS, str);
    }

    public int H() {
        return M(g.PASV);
    }

    public int I(InetAddress inetAddress, int i2) {
        StringBuilder sb = new StringBuilder(24);
        sb.append(inetAddress.getHostAddress().replace('.', ','));
        sb.append(',');
        sb.append(i2 >>> 8);
        sb.append(',');
        sb.append(i2 & 255);
        return N(g.PORT, sb.toString());
    }

    public int J() {
        return M(g.QUIT);
    }

    public int K(String str) {
        return N(g.REST, str);
    }

    public int M(g gVar) {
        return N(gVar, null);
    }

    public int N(g gVar, String str) {
        return O(gVar.getCommand(), str);
    }

    public int O(String str, String str2) throws IOException {
        if (this.f1214y == null) {
            throw new IOException("Connection is not open");
        }
        String strS = s(str, str2);
        L(strS);
        j(str, strS);
        return y();
    }

    public void P(String str) {
        this.f1209t = str;
    }

    public int R() {
        return M(g.SYST);
    }

    public int S(int i2) {
        return N(g.TYPE, "AEILNTCFRPSBC".substring(i2, i2 + 1));
    }

    public int T(String str) {
        return N(g.USER, str);
    }

    @Override // J1.d
    public void i() {
        super.i();
        this.f1213x = null;
        this.f1214y = null;
        this.f1207r = false;
        this.f1208s = null;
    }

    @Override // J1.d
    protected J1.c l() {
        return this.f1210u;
    }

    protected void r(Reader reader) throws SocketException {
        super.b();
        if (reader == null) {
            this.f1213x = new M1.a(new InputStreamReader(this.f1161f, x()));
        } else {
            this.f1213x = new M1.a(reader);
        }
        this.f1214y = new BufferedWriter(new OutputStreamWriter(this.f1162g, x()));
        if (this.f1165j <= 0) {
            y();
            if (t.c(this.f1205p)) {
                y();
                return;
            }
            return;
        }
        int soTimeout = this.f1157b.getSoTimeout();
        this.f1157b.setSoTimeout(this.f1165j);
        try {
            try {
                y();
                if (t.c(this.f1205p)) {
                    y();
                }
            } catch (SocketTimeoutException e2) {
                IOException iOException = new IOException("Timed out waiting for initial connect reply");
                iOException.initCause(e2);
                throw iOException;
            }
        } finally {
            this.f1157b.setSoTimeout(soTimeout);
        }
    }

    public int t(String str) {
        return N(g.CWD, str);
    }

    public int u(InetAddress inetAddress, int i2) {
        StringBuilder sb = new StringBuilder();
        String hostAddress = inetAddress.getHostAddress();
        int iIndexOf = hostAddress.indexOf(37);
        if (iIndexOf > 0) {
            hostAddress = hostAddress.substring(0, iIndexOf);
        }
        sb.append("|");
        if (inetAddress instanceof Inet4Address) {
            sb.append("1");
        } else if (inetAddress instanceof Inet6Address) {
            sb.append(ExifInterface.GPS_MEASUREMENT_2D);
        }
        sb.append("|");
        sb.append(hostAddress);
        sb.append("|");
        sb.append(i2);
        sb.append("|");
        return N(g.EPRT, sb.toString());
    }

    public int v() {
        return M(g.EPSV);
    }

    public int w() {
        return M(g.FEAT);
    }

    public String x() {
        return this.f1209t;
    }

    public int y() {
        return z(true);
    }
}
