package a0;

import android.util.Log;
import java.io.IOException;
import java.io.InputStream;
import okhttp3.HttpUrl;

/* JADX INFO: loaded from: classes.dex */
public final class o implements x {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    private final w f1994a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private h f1995b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private boolean f1996c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private long f1997d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private K1.e f1998e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private InputStream f1999f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    boolean f2000g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    boolean f2001h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    long f2002i;

    public static final class a extends IOException {
        public a(String str) {
            super(str);
        }

        public a(IOException iOException) {
            super(iOException);
        }
    }

    public o(w wVar) {
        this(8000, 8388608, wVar);
    }

    private long b(K1.e eVar, String str) {
        String strSubstring;
        int iLastIndexOf = str.lastIndexOf("/");
        if (iLastIndexOf < 0) {
            Log.e("FtpDataSource", "Illegal file path:" + str);
            return 0L;
        }
        String strSubstring2 = str.substring(iLastIndexOf + 1);
        if (iLastIndexOf != 0) {
            strSubstring = str.substring(0, iLastIndexOf);
            Log.i("FtpDataSource", "changeWorkingDirectory:" + eVar.a0(strSubstring));
        } else {
            strSubstring = null;
        }
        K1.i[] iVarArrU0 = eVar.u0();
        Log.i("FtpDataSource", "filePath:" + str + ", fileDirectory:" + strSubstring + ", fileName:" + strSubstring2 + ", index:" + iLastIndexOf + ", files.length:" + iVarArrU0.length);
        for (int i2 = 0; i2 < iVarArrU0.length; i2++) {
            K1.i iVar = iVarArrU0[i2];
            if (iVar != null && iVar.d() == 0) {
                Log.i("FtpDataSource", "mlistDir[" + i2 + "], name:" + iVarArrU0[i2].a() + ", size:" + iVarArrU0[i2].c() + ", fileType:" + iVarArrU0[i2].d());
                String strA = iVarArrU0[i2].a();
                if (strSubstring2 != null && strSubstring2.equals(strA)) {
                    return iVarArrU0[i2].c();
                }
            }
        }
        return 0L;
    }

    @Override // a0.f
    public long a(h hVar) throws a {
        String strSubstring;
        String strSubstring2;
        this.f1995b = hVar;
        String host = hVar.f1940a.getHost();
        int port = hVar.f1940a.getPort();
        if (port <= 0) {
            port = 21;
        }
        String userInfo = hVar.f1940a.getUserInfo();
        Log.i("FtpDataSource", "fpt login:" + hVar.f1940a.toString() + " " + hVar.f1943d);
        int iLastIndexOf = userInfo.lastIndexOf(58);
        if (iLastIndexOf != -1) {
            strSubstring = userInfo.substring(0, iLastIndexOf);
            strSubstring2 = userInfo.substring(iLastIndexOf + 1);
        } else {
            Log.w("FtpDataSource", "No password found.");
            strSubstring = "anonymous";
            strSubstring2 = HttpUrl.FRAGMENT_ENCODE_SET;
        }
        String path = hVar.f1940a.getPath();
        Log.i("FtpDataSource", "filePath: " + path);
        try {
            long jCurrentTimeMillis = System.currentTimeMillis();
            this.f1998e.P("UTF-8");
            Log.i("FtpDataSource", "set control encoding UTF-8");
            this.f1998e.g(host, port);
            boolean zA = K1.t.a(this.f1998e.A());
            this.f2001h = zA;
            if (!zA) {
                throw new a("connect failed.");
            }
            Log.i("FtpDataSource", "ftp connect use:" + (System.currentTimeMillis() - jCurrentTimeMillis) + "ms");
            this.f2000g = this.f1998e.w0(strSubstring, strSubstring2);
            Log.i("FtpDataSource", "ftp login use:" + (System.currentTimeMillis() - jCurrentTimeMillis) + "ms");
            Log.i("FtpDataSource", "fpt login:" + strSubstring + ":" + strSubstring2 + "@" + host + ":" + port + " - " + this.f2000g);
            if (!this.f2000g) {
                throw new a("login failed.");
            }
            if (K1.t.a(this.f1998e.O("OPTS UTF8", "ON"))) {
                Log.i("FtpDataSource", "sendCommand UTF8 on");
            }
            this.f1998e.d0();
            this.f2002i = b(this.f1998e, path);
            this.f1998e.C0(2);
            this.f1998e.B0(12);
            this.f1998e.D0(hVar.f1943d);
            this.f1999f = this.f1998e.z0(path);
            int iA = this.f1998e.A();
            long j2 = hVar.f1944e;
            if (j2 == -1) {
                j2 = this.f2002i - hVar.f1943d;
            }
            this.f1997d = j2;
            Log.i("FtpDataSource", "fileSize: " + this.f2002i + ", stream:" + this.f1999f + ", replyCode:" + iA + ", bytesRemaining:" + this.f1997d);
            this.f1996c = true;
            w wVar = this.f1994a;
            if (wVar != null) {
                wVar.b();
            }
            return this.f1997d;
        } catch (IOException e2) {
            throw new a(e2);
        }
    }

    @Override // a0.f
    public void close() {
        w wVar;
        Log.i("FtpDataSource", "close");
        if (this.f1996c) {
            this.f1996c = false;
            try {
                try {
                    InputStream inputStream = this.f1999f;
                    if (inputStream != null) {
                        inputStream.close();
                        if (!this.f1998e.b0()) {
                            Log.e("FtpDataSource", "completePendingCommand failed");
                        }
                    }
                    this.f1998e.x0();
                    this.f1998e.i();
                    wVar = this.f1994a;
                    if (wVar == null) {
                        return;
                    }
                } catch (IOException e2) {
                    e2.printStackTrace();
                    wVar = this.f1994a;
                    if (wVar == null) {
                        return;
                    }
                }
                wVar.a();
            } catch (Throwable th) {
                w wVar2 = this.f1994a;
                if (wVar2 != null) {
                    wVar2.a();
                }
                throw th;
            }
        }
    }

    @Override // a0.x
    public String getUri() {
        h hVar = this.f1995b;
        if (hVar == null) {
            return null;
        }
        return hVar.f1940a.toString();
    }

    @Override // a0.f
    public int read(byte[] bArr, int i2, int i3) throws a {
        long j2 = this.f1997d;
        if (j2 == 0) {
            return -1;
        }
        try {
            int i4 = this.f1999f.read(bArr, i2, (int) Math.min(j2, i3));
            if (i4 > 0) {
                this.f1997d -= (long) i4;
                w wVar = this.f1994a;
                if (wVar != null) {
                    wVar.c(i4);
                }
            }
            return i4;
        } catch (IOException e2) {
            throw new a(e2);
        }
    }

    public o(int i2, int i3, w wVar) {
        this.f1996c = false;
        this.f2000g = false;
        this.f2001h = false;
        this.f1994a = wVar;
        K1.e eVar = new K1.e();
        this.f1998e = eVar;
        eVar.A0(i2);
        Log.i("FtpDataSource", "new FtpDataSource");
    }
}
