package defpackage;

import android.util.Log;
import androidx.media3.container.NalUnitUtil;
import defpackage.g17;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentNavigableMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
import kotlin.Metadata;

/* JADX INFO: loaded from: classes3.dex */
@Metadata(d1 = {"\u0000\n\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\b\n\u0018\u00002\u00020\u0001¨\u0006\u0002"}, d2 = {"Lh17;", "Ljava/io/InputStream;", "app_sideloadRelease"}, k = 1, mv = {2, 0, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
public final class h17 extends InputStream {
    public long a;
    public final long b;
    public final /* synthetic */ g17 c;
    public final /* synthetic */ int d;

    public h17(long j, long j2, g17 g17Var, int i) {
        this.c = g17Var;
        this.d = i;
        this.a = j;
        this.b = j2;
    }

    public final boolean a() {
        g17 g17Var = this.c;
        return g17Var.h.get() || g17Var.m.get() != this.d;
    }

    @Override // java.io.InputStream
    public final int read() {
        if (this.a > this.b || a()) {
            return -1;
        }
        byte[] bArr = new byte[1];
        if (read(bArr, 0, 1) <= 0) {
            return -1;
        }
        return bArr[0] & 255;
    }

    @Override // java.io.InputStream
    public final int read(byte[] bArr, int i, int i2) throws Throwable {
        q59 q59Var;
        ReentrantLock reentrantLock;
        int i3;
        long j;
        int iMin;
        q59 q59Var2;
        long j2;
        cr4.g(bArr, "b");
        if (this.a <= this.b && !a() && (q59Var = this.c.i) != null) {
            long j3 = this.b;
            long j4 = this.a;
            long j5 = 1;
            long j6 = (j3 - j4) + 1;
            long j7 = i2;
            if (j6 > j7) {
                j6 = j7;
            }
            int i4 = (int) j6;
            if (i4 > 0) {
                ReentrantLock reentrantLock2 = q59Var.b;
                reentrantLock2.lock();
                try {
                    long jCurrentTimeMillis = System.currentTimeMillis() + 30000;
                    while (!q59Var.f && !q59Var.g) {
                        if (a()) {
                            reentrantLock2.unlock();
                            iMin = -1;
                            i3 = -1;
                        } else {
                            Map.Entry entryFloorEntry = q59Var.a.floorEntry(Long.valueOf(j4));
                            if (entryFloorEntry != null) {
                                Long l = (Long) entryFloorEntry.getKey();
                                byte[] bArr2 = (byte[]) entryFloorEntry.getValue();
                                i3 = -1;
                                j2 = j5;
                                long jLongValue = (l.longValue() + ((long) bArr2.length)) - j2;
                                if (l.longValue() <= j4 && j4 <= jLongValue) {
                                    int iLongValue = (int) (j4 - l.longValue());
                                    iMin = Math.min(bArr2.length - iLongValue, i4);
                                    System.arraycopy(bArr2, iLongValue, bArr, i, iMin);
                                    q59Var.a(j4 + ((long) iMin));
                                    reentrantLock2.unlock();
                                }
                            } else {
                                j2 = j5;
                                i3 = -1;
                            }
                            j = 0;
                            long jCurrentTimeMillis2 = jCurrentTimeMillis - System.currentTimeMillis();
                            if (jCurrentTimeMillis2 <= 0) {
                                Log.w("AssemblyBuffer", "Timeout waiting for data at position " + j4);
                                reentrantLock2.unlock();
                                break;
                            }
                            int i5 = i4;
                            reentrantLock = reentrantLock2;
                            try {
                                q59Var.c.await(Math.min(jCurrentTimeMillis2, 500L), TimeUnit.MILLISECONDS);
                                reentrantLock2 = reentrantLock;
                                i4 = i5;
                                j5 = j2;
                            } catch (Throwable th) {
                                th = th;
                                reentrantLock.unlock();
                                throw th;
                            }
                        }
                        j = 0;
                    }
                    i3 = -1;
                    j = 0;
                    reentrantLock2.unlock();
                    iMin = i3;
                    if (iMin <= 0) {
                        return i3;
                    }
                    this.a += (long) iMin;
                    if (this.c.e == g17.b.a && !a() && (q59Var2 = this.c.i) != null) {
                        ConcurrentNavigableMap concurrentNavigableMapTailMap = q59Var2.a.tailMap(Long.valueOf(this.a));
                        cr4.f(concurrentNavigableMapTailMap, "tailMap(...)");
                        Iterator it = concurrentNavigableMapTailMap.entrySet().iterator();
                        long length = j;
                        while (it.hasNext()) {
                            length += (long) ((byte[]) ((Map.Entry) it.next()).getValue()).length;
                        }
                        this.c.c.getClass();
                        if (length < 20971520 / ((long) 2)) {
                            long j8 = q59Var2.d;
                            long j9 = this.b;
                            if (j8 <= j9) {
                                this.c.h(this.d, j8, j9);
                            }
                        }
                    }
                    return iMin;
                } catch (Throwable th2) {
                    th = th2;
                    reentrantLock = reentrantLock2;
                }
            }
        }
        return -1;
    }
}
