h.java 源代码


package k2;

import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.os.SystemClock;
import android.text.TextUtils;
import android.util.Log;
import com.anythink.expressad.foundation.d.b;
import com.mbridge.msdk.foundation.download.Command;
import com.mbridge.msdk.playercommon.exoplayer2.util.MimeTypes;
import com.mbridge.msdk.thrid.okhttp.internal.http.StatusLine;
import com.x8zs.plugin.apache.http.HttpStatus;
import com.x8zs.plugin.apache.http.client.methods.HttpHead;
import com.x8zs.plugin.apache.http.protocol.HTTP;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.Future;
import k2.g;

public class h extends HandlerThread {
    c A;
    private f B;
    private ArrayList<b> C;
    private g D;
    private Throwable E;
    private long F;
    private long G;
    private volatile long H;
    private volatile long I;
    private long J;
    private int K;
    private volatile long L;
    private int M;
    private int N;
    private int O;
    private l P;
    private long Q;
    private long R;
    private k2.c S;
    private URL T;
    private int U;
    private String V;
    private boolean W;
    private boolean X;
    private boolean Y;
    private boolean Z;

    private int f23507b0;

    private RandomAccessFile f23508c0;

    private n f23509d0;

    private long f23510e0;

    private boolean f23511f0;

    private int f23512g0;

    private d f23513q;

    private j f23514r;

    private String f23515s;

    private k2.b f23516t;

    private String f23517u;

    private URL f23518v;

    private URL f23519w;

    private URL f23520x;

    private String f23521y;

    private long f23522z;

    public class a implements Comparator<g.a> {
        a() {
        }

        @Override
        public int compare(g.a aVar, g.a aVar2) {
            return (int) (aVar.f23504a - aVar2.f23504a);
        }
    }

    public class c extends Handler {
        c(Looper looper) {
            super(looper);
        }

        @Override
        public void handleMessage(Message message) {
            try {
                switch (message.what) {
                    case 1:
                        h.this.S();
                        break;
                    case 2:
                        h.this.T();
                        break;
                    case 3:
                        h.this.V();
                        break;
                    case 4:
                        h.this.W();
                        break;
                    case 5:
                        h.this.X();
                        break;
                    case 6:
                        h.this.r0(message.arg1);
                        break;
                    case 7:
                        h.this.n0();
                        break;
                    case 8:
                        h.this.q0((Throwable) message.obj);
                        break;
                    case 9:
                        h.this.U();
                        break;
                    case 10:
                        h.this.A0();
                        break;
                    default:
                        Log.e(h.class.getName(), "default msg error. msg=" + message.toString());
                        break;
                }
                super.handleMessage(message);
            } catch (Exception e6) {
                if (e6.getMessage() != null) {
                    Log.e(h.class.getName(), e6.getMessage());
                }
            }
        }
    }

    public h(d dVar, String str, String str2, j jVar, k2.c cVar) {
        super("com.fiftyone.module.download.DownloadTask");
        this.f23522z = -1L;
        this.J = 0L;
        this.K = 0;
        this.L = 0L;
        this.M = 0;
        this.N = 0;
        this.O = 0;
        this.P = new l(120, 50);
        this.U = -1;
        this.V = "";
        this.W = false;
        this.X = false;
        this.Y = false;
        this.Z = true;
        this.f23510e0 = 0L;
        this.f23511f0 = true;
        this.f23513q = dVar;
        this.f23514r = jVar;
        this.S = cVar;
        this.f23517u = str;
        this.f23521y = str2;
    }

    public void A0() {
        if (!e0()) {
            return;
        }
        b bVar = new b();
        try {
            bVar.v();
        } catch (Exception e6) {
            if (!v0(e6)) {
                p0(e6, bVar.i());
            }
        }
    }

    public void B0(long j6) {
        this.f23522z = j6;
        k2.b bVar = this.f23516t;
        if (bVar != null) {
            bVar.f23474b = j6;
        }
    }

    private int C0(int i6) {
        List<g.a> list = null;
        try {
            try {
                list = this.D.e(i6);
                if (list.isEmpty()) {
                    Iterator<g.a> it = list.iterator();
                    while (it.hasNext()) {
                        this.D.a(it.next());
                    }
                    list.clear();
                    return 0;
                }
                if (list.size() > 1) {
                    Collections.sort(list, new a());
                }
                System.currentTimeMillis();
                int i7 = 0;
                while (!list.isEmpty()) {
                    g.a aVar = list.get(0);
                    int d6 = (int) aVar.d();
                    n nVar = this.f23509d0;
                    if (nVar != null && nVar.c(d6)) {
                        if (!this.f23509d0.d(new File(this.f23521y).getParent(), this.B.i())) {
                            o0(new m(307, "not enough free space in the filesystem"));
                            Iterator<g.a> it2 = list.iterator();
                            while (it2.hasNext()) {
                                this.D.a(it2.next());
                            }
                            list.clear();
                            return -1;
                        }
                    }
                    this.f23508c0.seek(aVar.f23504a);
                    this.f23508c0.write(aVar.f23506c, 0, d6);
                    this.B.b(new e(aVar.f23504a, aVar.f23505b));
                    this.D.a(aVar);
                    i7 += d6;
                    list.remove(aVar);
                }
                return i7;
            } catch (Exception e6) {
                if (e6 instanceof IOException) {
                    String message = e6.getMessage();
                    if (message != null) {
                        String lowerCase = message.toLowerCase();
                        if (lowerCase.contains("enospc") || lowerCase.contains("no space")) {
                            o0(new m(307, "not enough free space in the filesystem"));
                            if (list != null) {
                                Iterator<g.a> it3 = list.iterator();
                                while (it3.hasNext()) {
                                    this.D.a(it3.next());
                                }
                                list.clear();
                            }
                            return -1;
                        }
                    }
                    o0(new m(StatusLine.HTTP_PERM_REDIRECT, e6));
                    if (list != null) {
                        Iterator<g.a> it4 = list.iterator();
                        while (it4.hasNext()) {
                            this.D.a(it4.next());
                        }
                        list.clear();
                    }
                    return -1;
                }
                o0(new m(304, e6));
                if (list != null) {
                    Iterator<g.a> it5 = list.iterator();
                    while (it5.hasNext()) {
                        this.D.a(it5.next());
                    }
                    list.clear();
                }
                return -1;
            }
        } catch (Throwable th) {
            if (list != null) {
                Iterator<g.a> it6 = list.iterator();
                while (it6.hasNext()) {
                    this.D.a(it6.next());
                }
                list.clear();
            }
            throw th;
        }
    }

    public void N(int i6) {
        this.L += i6;
        this.P.a(i6);
    }

    private void O() {
        this.A.removeMessages(10);
    }

    private void P() {
        RandomAccessFile randomAccessFile = this.f23508c0;
        if (randomAccessFile != null) {
            try {
                randomAccessFile.close();
            } catch (IOException e6) {
                e6.printStackTrace();
            }
            this.f23508c0 = null;
        }
    }

    private void Q() {
        ArrayList<b> arrayList = this.C;
        if (arrayList != null && arrayList.size() > 0) {
            Iterator<b> it = this.C.iterator();
            while (it.hasNext()) {
                it.next().l();
            }
            if (f0() && this.Z) {
                SystemClock.sleep(500L);
            }
            Iterator<b> it2 = this.C.iterator();
            while (it2.hasNext()) {
                it2.next().h();
            }
            this.C.clear();
            this.C = null;
        }
        if (f0()) {
            C0(0);
        }
        P();
        if (this.F > 0) {
            this.J += (System.currentTimeMillis() - this.F) / 1000;
            this.F = 0L;
        }
        k2.b bVar = this.f23516t;
        if (bVar != null) {
            bVar.f23477e = this.J;
            bVar.f23478f = this.B.g();
            this.f23516t.f23479g = this.B.d();
            this.f23516t.j();
            this.f23516t.b();
            this.f23516t = null;
        }
    }

    public void S() {
        this.G = System.currentTimeMillis();
    }

    public void T() {
        getLooper().quit();
        this.A = null;
    }

    public void U() {
        if (this.W) {
            return;
        }
        this.M = 0;
        Q();
        if (this.B != null) {
            this.B = null;
        }
        this.T = this.f23519w;
        this.f23519w = this.f23518v;
        if (this.f23515s != null) {
            new File(this.f23515s).delete();
        }
        this.W = true;
        V();
    }

    public void V() {
        if (e0()) {
            return;
        }
        try {
            this.M = 1;
            URL url = new URL(this.f23517u);
            this.f23518v = url;
            this.f23519w = url;
            this.E = null;
            Z();
            j jVar = this.f23514r;
            if (jVar != null) {
                jVar.f();
            }
            if (this.D != null) {
                this.D = null;
            }
            this.D = new g();
            this.f23508c0 = new RandomAccessFile(this.f23521y, "rw");
            this.f23509d0 = new n();
            if (this.B != null) {
                this.B = null;
            }
            f fVar = new f(262144);
            this.B = fVar;
            k2.b bVar = this.f23516t;
            if (bVar != null) {
                fVar.k(bVar.f23474b);
                this.B.l(this.f23516t.f23478f);
                this.B.a(this.f23516t.f23479g);
                this.J = this.f23516t.f23477e;
            }
            if (this.B.j()) {
                n0();
                return;
            }
            this.H = this.B.e();
            this.L = 0L;
            this.P.d();
            this.Z = true;
            this.f23507b0 = 0;
            this.K = 0;
            this.X = false;
            this.Y = false;
            this.F = System.currentTimeMillis();
            A0();
        } catch (Exception e6) {
            if (e6 instanceof IOException) {
                o0(new m(306, e6));
            } else {
                p0(e6, -100);
            }
        }
    }

    public void W() {
        if (!e0()) {
            return;
        }
        this.M = 4;
        Q();
        s0();
        j jVar = this.f23514r;
        if (jVar != null) {
            jVar.c();
        }
        O();
    }

    public void X() {
        int C0;
        if (e0() && (C0 = C0(50)) != -1) {
            r0(C0);
            if (this.B.j()) {
                n0();
            } else {
                m0();
            }
        }
    }

    public void Y() {
    }

    private boolean Z() {
        boolean z6;
        if (this.f23516t != null) {
            return true;
        }
        this.f23515s = this.f23521y + ".dat";
        File file = new File(this.f23521y);
        File file2 = new File(this.f23515s);
        if (file.exists() && file.isFile()) {
            if (file2.exists() && file2.isFile()) {
                k2.b e6 = k2.b.e(this.f23515s);
                this.f23516t = e6;
                if (e6 != null) {
                    e6.d();
                } else {
                    file2.delete();
                }
            }
            z6 = true;
        } else {
            if (file2.exists() && file2.isFile()) {
                file2.delete();
            }
            z6 = false;
        }
        if (this.f23516t == null) {
            k2.b f6 = k2.b.f(this.f23515s);
            this.f23516t = f6;
            if (f6 != null && z6) {
                if (!this.f23516t.k(this.f23521y + ".cfg")) {
                    this.f23516t.l(this.f23517u);
                }
            }
            this.O = 0;
        } else {
            this.O = 1;
        }
        k2.b bVar = this.f23516t;
        if (bVar == null) {
            return false;
        }
        if (bVar.f23480h == null) {
            bVar.f23480h = this.S;
        }
        bVar.f23475c = this.f23517u;
        return true;
    }

    private boolean a0() {
        k2.c cVar;
        RandomAccessFile randomAccessFile;
        Throwable th;
        if (!this.W && (cVar = this.S) != null && cVar.f23485c > 0 && !TextUtils.isEmpty(cVar.f23484b) && !"0".equals(this.S.f23483a)) {
            RandomAccessFile randomAccessFile2 = null;
            try {
                randomAccessFile = new RandomAccessFile(this.f23521y, "rw");
                try {
                    int i6 = this.S.f23485c * 1024;
                    long j6 = i6;
                    if (randomAccessFile.length() >= j6) {
                        randomAccessFile.seek(randomAccessFile.length() - j6);
                        byte[] bArr = new byte[i6];
                        randomAccessFile.readFully(bArr);
                        String d6 = k2.a.d(bArr, 0, i6);
                        if (!this.S.f23484b.equals(d6)) {
                            this.U = 2;
                            this.V = String.format("check crc not passed - crc:%s local crc:%s", this.S.f23484b, d6);
                            o2.f.c(randomAccessFile);
                            return false;
                        }
                    }
                    o2.f.c(randomAccessFile);
                } catch (FileNotFoundException unused) {
                    randomAccessFile2 = randomAccessFile;
                } catch (IOException unused2) {
                    randomAccessFile2 = randomAccessFile;
                } catch (Throwable th2) {
                    th = th2;
                    if (randomAccessFile != null) {
                        o2.f.c(randomAccessFile);
                    }
                    throw th;
                }
            } catch (FileNotFoundException unused3) {
            } catch (IOException unused4) {
            } catch (Throwable th3) {
                randomAccessFile = null;
                th = th3;
            }
        }
        return true;
    }

    public boolean b0(long j6, long j7, byte[] bArr) {
        k2.c cVar;
        if (!this.Y && !this.W && (cVar = this.S) != null && j6 == 0) {
            if (cVar.f23485c > 0 && !TextUtils.isEmpty(cVar.f23483a) && !"0".equals(this.S.f23483a)) {
                int i6 = this.S.f23485c;
                if (j7 >= i6 * 1024) {
                    String e6 = k2.a.e(bArr, 0, i6 * 1024);
                    if (!this.S.f23483a.equals(e6)) {
                        this.U = 1;
                        this.V = String.format("check headMd5 not passed - md5:%s,local md5:%s", this.S.f23483a, e6);
                        this.Y = true;
                        return false;
                    }
                }
            }
            this.Y = true;
        }
        return true;
    }

    public boolean c0() {
        k2.c cVar;
        if (this.X) {
            return true;
        }
        if (!this.W && (cVar = this.S) != null) {
            long j6 = cVar.f23486d;
            if (j6 > 0 && j6 != this.f23522z) {
                this.U = 0;
                this.V = String.format("check size not passed - size:%d local size:%d", Long.valueOf(j6), Long.valueOf(this.f23522z));
                this.X = true;
                return false;
            }
        }
        this.X = true;
        return true;
    }

    private boolean e0() {
        if (this.M == 1) {
            return true;
        }
        return false;
    }

    private boolean f0() {
        if (this.M == 4) {
            return true;
        }
        return false;
    }

    private boolean g0() {
        if (this.N == 4) {
            return true;
        }
        return false;
    }

    public void i0() {
        Message obtain = Message.obtain();
        obtain.what = 7;
        this.A.sendMessage(obtain);
    }

    public void j0(Exception exc) {
        Message obtain = Message.obtain();
        obtain.what = 8;
        obtain.obj = exc;
        this.A.sendMessage(obtain);
    }

    public void k0(int i6) {
        Message obtain = Message.obtain();
        obtain.what = 6;
        obtain.arg1 = i6;
        this.A.sendMessage(obtain);
    }

    public void l0(long j6) {
        Message obtain = Message.obtain();
        obtain.what = 10;
        this.A.sendMessageDelayed(obtain, j6);
    }

    public void m0() {
        Message obtain = Message.obtain();
        obtain.what = 5;
        this.A.sendMessage(obtain);
    }

    private void o0(Throwable th) {
        p0(th, 0);
    }

    private void p0(Throwable th, int i6) {
        String str;
        if (!e0()) {
            return;
        }
        this.M = 3;
        Q();
        if (th instanceof m) {
            ((m) th).g();
        }
        s0();
        Throwable th2 = this.E;
        boolean z6 = true;
        if (th2 == null) {
            this.E = th;
        } else {
            String th3 = th2.toString();
            if (th != null) {
                str = th.toString();
            } else {
                str = "";
            }
            if (th3 != null && !th3.equals(str)) {
                this.E = th;
            } else {
                z6 = false;
            }
        }
        j jVar = this.f23514r;
        if (jVar != null && z6) {
            jVar.b(this.H, th, i6);
        }
        if (z6) {
            String.valueOf(i6);
            if (th != null) {
                th.getMessage();
            }
        }
    }

    public void q0(Throwable th) {
        int i6 = this.f23507b0 + 1;
        this.f23507b0 = i6;
        if (i6 >= this.C.size()) {
            p0(th, this.f23512g0);
        }
    }

    private void s0() {
    }

    public synchronized void u0() {
        Message obtain = Message.obtain();
        obtain.what = 9;
        this.A.sendMessage(obtain);
    }

    private boolean v0(Exception exc) {
        int i6 = this.K + 1;
        this.K = i6;
        if (i6 <= 5) {
            int min = Math.min(10000, i6 * 2000);
            j jVar = this.f23514r;
            if (jVar != null) {
                jVar.d(this.K, min / 1000);
            }
            l0(min);
            return true;
        }
        return false;
    }

    public Future z0(Runnable runnable) {
        return this.f23513q.b().submit(runnable);
    }

    public synchronized void R() {
        if (!super.isAlive()) {
            super.start();
        }
        this.A = new c(getLooper());
        Message obtain = Message.obtain();
        obtain.what = 1;
        this.A.sendMessage(obtain);
    }

    public boolean d0() {
        if (this.N == 1) {
            return true;
        }
        return false;
    }

    public boolean h0() {
        if (this.M == 2) {
            return true;
        }
        return false;
    }

    public void n0() {
        f fVar = this.B;
        if (fVar != null && fVar.j() && !h0()) {
            if (!a0()) {
                U();
                return;
            }
            long e6 = this.B.e();
            if (this.H != e6) {
                this.H = e6;
            }
            k2.b bVar = this.f23516t;
            if (bVar != null) {
                bVar.f23478f = this.H;
                this.f23516t.f23479g.clear();
            }
            this.M = 2;
            Q();
            s0();
            j jVar = this.f23514r;
            if (jVar != null) {
                jVar.e(this.H, this.f23522z, 0);
                this.f23514r.a(this.H, this.f23522z, (int) this.J);
                return;
            }
            return;
        }
        o0(new m(312, "onDownloadComplete Error"));
    }

    public void r0(int i6) {
        this.H += i6;
        long currentTimeMillis = System.currentTimeMillis();
        k2.b bVar = this.f23516t;
        if (bVar != null && currentTimeMillis - this.R >= 500) {
            bVar.f23478f = this.B.g();
            this.f23516t.f23479g = this.B.d();
            this.f23516t.j();
            this.R = currentTimeMillis;
        }
        if (currentTimeMillis - this.Q >= 2000) {
            long e6 = this.B.e();
            if (this.H != e6) {
                this.H = e6;
            }
            this.I = this.P.b();
            this.Q = currentTimeMillis;
            if (this.f23514r != null && e0() && !g0()) {
                this.f23514r.e(this.H, this.f23522z, (int) this.I);
            }
        }
    }

    public synchronized void t0() {
        Message obtain = Message.obtain();
        obtain.what = 2;
        this.A.sendMessage(obtain);
    }

    public void w0(k2.c cVar) {
        this.S = cVar;
        k2.b bVar = this.f23516t;
        if (bVar != null) {
            bVar.f23480h = cVar;
        }
    }

    public synchronized void x0() {
        if (!super.isAlive()) {
            super.start();
        }
        Message obtain = Message.obtain();
        obtain.what = 3;
        this.A.sendMessage(obtain);
        this.N = 1;
    }

    public synchronized void y0() {
        Message obtain = Message.obtain();
        obtain.what = 4;
        this.A.sendMessage(obtain);
        this.N = 4;
    }

    public class b implements Runnable {

        private int f23524q;

        private int f23525r;

        private int f23526s;

        private HttpURLConnection f23527t;

        private boolean f23528u;

        private Future f23529v;

        public class a implements Runnable {
            a() {
            }

            @Override
            public void run() {
                FileOutputStream fileOutputStream;
                g.a aVar = new g.a();
                int a7 = (int) aVar.a();
                InputStream inputStream = null;
                try {
                    try {
                        fileOutputStream = new FileOutputStream(h.this.f23521y);
                        try {
                            inputStream = b.this.f23527t.getInputStream();
                            int i6 = 0;
                            int i7 = 0;
                            long j6 = 0;
                            while (b.this.f23528u && i6 >= 0) {
                                i6 = inputStream.read(aVar.f23506c, i7, a7 - i7);
                                if (i6 > 0) {
                                    if (h.this.f23511f0) {
                                        h.this.f23511f0 = false;
                                    }
                                    i7 += i6;
                                    if (i7 >= a7) {
                                        h.this.N(i7);
                                        if (h.this.c0() && h.this.b0(aVar.f23504a, aVar.f23505b, aVar.f23506c)) {
                                            fileOutputStream.write(aVar.f23506c, 0, i7);
                                            long j7 = i7;
                                            j6 += j7;
                                            h.this.B.l(j6);
                                            aVar.b(j7);
                                            h.this.k0(i7);
                                            i7 = 0;
                                        }
                                        h.this.u0();
                                        o2.f.c(inputStream);
                                        o2.f.c(fileOutputStream);
                                        b.this.p();
                                        return;
                                    }
                                } else if (i6 != 0) {
                                    h.this.N(i7);
                                    if (h.this.c0()) {
                                        fileOutputStream.write(aVar.f23506c, 0, i7);
                                        long j72 = i7;
                                        j6 += j72;
                                        h.this.B.l(j6);
                                        aVar.b(j72);
                                        h.this.k0(i7);
                                        i7 = 0;
                                    }
                                    h.this.u0();
                                    o2.f.c(inputStream);
                                    o2.f.c(fileOutputStream);
                                    b.this.p();
                                    return;
                                }
                            }
                            if (h.this.B.j()) {
                                h.this.i0();
                            } else if (h.this.B.e() != 0 || !h.this.Z) {
                                h.this.j0(new m(314, ""));
                            } else {
                                h.this.Z = false;
                                h.this.l0(0L);
                            }
                        } catch (Exception e6) {
                            e = e6;
                            b bVar = b.this;
                            h.this.f23512g0 = bVar.f23526s;
                            if (e instanceof IOException) {
                                h.this.j0(new m(StatusLine.HTTP_PERM_REDIRECT, e));
                            } else {
                                h.this.j0(e);
                            }
                            o2.f.c(inputStream);
                            o2.f.c(fileOutputStream);
                            b.this.p();
                        }
                    } catch (Throwable th) {
                        th = th;
                        o2.f.c(null);
                        o2.f.c(null);
                        b.this.p();
                        throw th;
                    }
                } catch (Exception e7) {
                    e = e7;
                    fileOutputStream = null;
                } catch (Throwable th2) {
                    th = th2;
                    o2.f.c(null);
                    o2.f.c(null);
                    b.this.p();
                    throw th;
                }
                o2.f.c(inputStream);
                o2.f.c(fileOutputStream);
                b.this.p();
            }
        }

        public b() {
            this.f23524q = 0;
            this.f23528u = true;
            this.f23525r = -1;
        }

        public void h() {
            Future future = this.f23529v;
            if (future != null && !future.isDone()) {
                this.f23529v.cancel(true);
            }
        }

        private boolean j() {
            HttpURLConnection httpURLConnection = this.f23527t;
            if (httpURLConnection == null) {
                return false;
            }
            String contentType = httpURLConnection.getContentType();
            if (!TextUtils.isEmpty(contentType)) {
                if (contentType.contains("application/javascript") || contentType.contains("application/x-javascript") || contentType.contains(b.c.f3477e) || contentType.contains(MimeTypes.BASE_TYPE_TEXT) || contentType.contains(MimeTypes.BASE_TYPE_AUDIO) || contentType.contains("video")) {
                    return false;
                }
                return true;
            }
            return true;
        }

        private void k() throws Exception {
            if (h.this.f23514r != null) {
                h.this.f23514r.g(h.this.H, h.this.f23522z);
            }
            p();
            long j6 = h.this.f23522z - h.this.H;
            if (j6 < 0) {
                j6 = 0;
            }
            int min = Math.min(Math.max((int) (j6 / 262144), 1), 4);
            if (h.this.C == null) {
                h.this.C = new ArrayList(min);
            } else {
                h.this.C.clear();
            }
            for (int i6 = 0; i6 < min; i6++) {
                b bVar = new b(i6);
                bVar.f23529v = h.this.z0(bVar);
                h.this.C.add(bVar);
            }
            h.this.m0();
        }

        public void l() {
            this.f23528u = false;
        }

        private long m() throws m {
            String headerField = this.f23527t.getHeaderField("Content-Range");
            if (headerField != null) {
                try {
                    return Long.parseLong(headerField.substring(headerField.lastIndexOf(47) + 1));
                } catch (Exception unused) {
                    throw new m(301, "Content-Range parse 0");
                }
            }
            return 0L;
        }

        private e n() {
            String headerField = this.f23527t.getHeaderField("Content-Range");
            if (headerField != null) {
                try {
                    return new e(Long.parseLong(headerField.substring(headerField.indexOf(32) + 1, headerField.indexOf(45))), Long.parseLong(headerField.substring(headerField.indexOf(45) + 1, headerField.indexOf(47))) + 1);
                } catch (Exception unused) {
                }
            }
            return null;
        }

        private boolean o(long j6, long j7) throws Exception {
            long j8;
            long j9;
            int read;
            b bVar = this;
            InputStream inputStream = null;
            try {
                try {
                    e n6 = n();
                    if (n6 != null) {
                        j8 = n6.f23493a;
                        j9 = n6.f23494b;
                    } else {
                        j8 = j6;
                        j9 = j7;
                    }
                    inputStream = bVar.f23527t.getInputStream();
                    boolean z6 = false;
                    int i6 = 0;
                    while (bVar.f23528u) {
                        j8 += i6;
                        g.a d6 = h.this.D.d();
                        if (d6 != null) {
                            int a7 = (int) d6.a();
                            int i7 = 0;
                            do {
                                read = inputStream.read(d6.f23506c, i7, a7 - i7);
                                if (read > 0) {
                                    if (h.this.f23511f0) {
                                        h.this.f23511f0 = z6;
                                    }
                                    i7 += read;
                                }
                                if (read < 0 || i7 >= a7) {
                                    break;
                                }
                            } while (bVar.f23528u);
                            h.this.N(i7);
                            long j10 = i7 + j8;
                            if (j10 > j9) {
                                j10 = j9;
                            }
                            d6.c(j8, j10);
                            if (h.this.c0()) {
                                try {
                                    if (!h.this.b0(d6.f23504a, d6.f23505b, d6.f23506c)) {
                                        bVar = this;
                                    } else {
                                        bVar = this;
                                        if (!h.this.D.b(d6)) {
                                            h.this.D.a(d6);
                                        }
                                        if (read < 0 || j10 == j9) {
                                            break;
                                        }
                                        i6 = i7;
                                        z6 = false;
                                    }
                                } catch (Exception e6) {
                                    e = e6;
                                    if (!(e instanceof InterruptedException)) {
                                        throw new m(305, e.getMessage());
                                    }
                                    o2.f.c(inputStream);
                                    p();
                                    return true;
                                } catch (Throwable th) {
                                    th = th;
                                    o2.f.c(inputStream);
                                    p();
                                    throw th;
                                }
                            }
                            h.this.u0();
                            h.this.D.a(d6);
                            o2.f.c(inputStream);
                            p();
                            return false;
                        }
                    }
                } catch (Exception e7) {
                    e = e7;
                }
                o2.f.c(inputStream);
                p();
                return true;
            } catch (Throwable th2) {
                th = th2;
            }
        }

        public void p() {
            HttpURLConnection httpURLConnection = this.f23527t;
            if (httpURLConnection != null) {
                httpURLConnection.disconnect();
                this.f23527t = null;
            }
        }

        private boolean q(Exception exc) {
            int i6 = this.f23524q + 1;
            this.f23524q = i6;
            if (i6 <= 5) {
                int min = Math.min(10000, i6 * 2000);
                int i7 = this.f23524q;
                if (i7 != 1) {
                    if (i7 <= 3) {
                        h hVar = h.this;
                        hVar.f23519w = hVar.f23518v;
                    } else if (i7 > 3) {
                        h hVar2 = h.this;
                        hVar2.f23519w = hVar2.f23518v;
                    }
                }
                try {
                    Thread.sleep(min);
                } catch (InterruptedException e6) {
                    e6.printStackTrace();
                }
                return true;
            }
            return false;
        }

        private void r(HttpURLConnection httpURLConnection) throws ProtocolException {
            httpURLConnection.setInstanceFollowRedirects(false);
            httpURLConnection.setConnectTimeout(15000);
            httpURLConnection.setReadTimeout(com.anythink.expressad.foundation.f.f.g.c.f3888f);
            httpURLConnection.setRequestProperty(HTTP.CONN_DIRECTIVE, "Keep-Alive");
            httpURLConnection.setRequestProperty("Accept-Encoding", HTTP.IDENTITY_CODING);
            httpURLConnection.setRequestProperty("User-Agent", "NineGameClient/android");
            httpURLConnection.setRequestMethod("GET");
        }

        private void s(HttpURLConnection httpURLConnection) throws ProtocolException {
            httpURLConnection.setInstanceFollowRedirects(false);
            httpURLConnection.setConnectTimeout(15000);
            httpURLConnection.setReadTimeout(com.anythink.expressad.foundation.f.f.g.c.f3888f);
            httpURLConnection.setRequestProperty(HTTP.CONN_DIRECTIVE, "Keep-Alive");
            httpURLConnection.setRequestProperty("Accept-Encoding", HTTP.IDENTITY_CODING);
            httpURLConnection.setRequestProperty("User-Agent", "NineGameClient/android");
            httpURLConnection.setRequestMethod(HttpHead.METHOD_NAME);
        }

        private void t() throws Exception {
            h.this.H = 0L;
            if (h.this.f23514r != null) {
                h.this.f23514r.g(h.this.H, h.this.f23522z);
            }
            if (h.this.C == null) {
                h.this.C = new ArrayList(1);
            } else {
                h.this.C.clear();
            }
            this.f23529v = h.this.z0(new a());
            h.this.C.add(this);
        }

        private void u() throws Exception {
            int i6 = 0;
            loop0: while (this.f23528u) {
                e h6 = h.this.B.h(this.f23525r);
                if (h6 != null) {
                    while (true) {
                        this.f23527t = (HttpURLConnection) h.this.f23519w.openConnection();
                        if (h.this.f23520x != null) {
                            this.f23527t.setRequestProperty(HTTP.TARGET_HOST, h.this.f23520x.getHost());
                        }
                        r(this.f23527t);
                        long j6 = h6.f23493a;
                        long j7 = h6.f23494b;
                        this.f23527t.setRequestProperty(Command.HTTP_HEADER_RANGE, "bytes=" + j6 + "-" + (j7 - 1));
                        int responseCode = this.f23527t.getResponseCode();
                        this.f23526s = responseCode;
                        if (responseCode != 206) {
                            if ((responseCode == 301 || responseCode == 302) && i6 < 5) {
                                i6++;
                                h.this.f23519w = new URL(h.this.f23519w, this.f23527t.getHeaderField("Location"));
                                p();
                            }
                        } else if (!o(j6, j7)) {
                            p();
                            return;
                        }
                    }
                } else if (!h.this.B.j()) {
                    h.this.B.c();
                } else {
                    return;
                }
            }
        }

        public void v() throws Exception {
            int i6 = 0;
            while (true) {
                int i7 = i6 + 1;
                if (i6 < 5) {
                    h.this.Y();
                    this.f23527t = (HttpURLConnection) h.this.f23519w.openConnection();
                    if (h.this.f23520x != null) {
                        this.f23527t.setRequestProperty(HTTP.TARGET_HOST, h.this.f23520x.getHost());
                    }
                    if (h.this.Z) {
                        s(this.f23527t);
                        this.f23527t.setRequestProperty(Command.HTTP_HEADER_RANGE, "bytes=0-127");
                    } else {
                        r(this.f23527t);
                    }
                    int responseCode = this.f23527t.getResponseCode();
                    this.f23526s = responseCode;
                    if (responseCode != 200) {
                        if (responseCode != 202) {
                            switch (responseCode) {
                                case 204:
                                case HttpStatus.SC_RESET_CONTENT:
                                    break;
                                case HttpStatus.SC_PARTIAL_CONTENT:
                                    if (j()) {
                                        h.this.B0(m());
                                        if (h.this.B.f() == 0) {
                                            h.this.B.k(h.this.f23522z);
                                        } else if (h.this.B.f() != h.this.f23522z) {
                                            throw new m(309, "saved file length is not match with content length");
                                        }
                                        k();
                                        return;
                                    }
                                    if (h.this.Z) {
                                        h.this.Z = false;
                                        break;
                                    } else {
                                        throw new m(302, "wrong content type");
                                    }
                                default:
                                    switch (responseCode) {
                                        case 301:
                                        case 302:
                                        case 303:
                                            h.this.f23519w = new URL(h.this.f23519w, this.f23527t.getHeaderField("Location"));
                                            p();
                                            break;
                                        default:
                                            if (h.this.Z && h.this.O != 1) {
                                                h.this.Z = false;
                                                break;
                                            }
                                            break;
                                    }
                            }
                        }
                        h.this.Z = false;
                    } else {
                        if (j()) {
                            h.this.B0(this.f23527t.getContentLength());
                            if (h.this.B.f() == 0) {
                                h.this.B.k(h.this.f23522z);
                            } else if (h.this.B.f() != h.this.f23522z) {
                                throw new m(309, "saved file length is not match with content length");
                            }
                            t();
                            return;
                        }
                        if (h.this.Z) {
                            h.this.Z = false;
                        } else {
                            throw new m(302, "wrong content type");
                        }
                    }
                    i6 = i7;
                } else {
                    return;
                }
            }
            throw new m(300, "wrong response code:" + this.f23526s);
        }

        public int i() {
            return this.f23526s;
        }

        @Override
        public void run() {
            this.f23524q = 0;
            while (this.f23528u) {
                try {
                    u();
                    return;
                } catch (InterruptedException unused) {
                } catch (Exception e6) {
                    if (!this.f23528u) {
                        return;
                    }
                    if (!q(e6)) {
                        h.this.f23512g0 = this.f23526s;
                        h.this.j0(e6);
                        return;
                    }
                }
            }
        }

        public b(int i6) {
            this.f23524q = 0;
            this.f23528u = true;
            this.f23525r = i6;
        }
    }
}