package defpackage;

import androidx.media3.container.NalUnitUtil;
import androidx.media3.exoplayer.upstream.CmcdData;
import java.io.Closeable;
import java.util.concurrent.locks.ReentrantLock;
import kotlin.Metadata;

/* JADX INFO: loaded from: classes3.dex */
@Metadata(d1 = {"\u0000\u0010\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0003\b&\u0018\u00002\u00060\u0001j\u0002`\u0002:\u0002\u0003\u0004¨\u0006\u0005"}, d2 = {"Ldy2;", "Ljava/io/Closeable;", "Lokio/Closeable;", CmcdData.OBJECT_TYPE_AUDIO_ONLY, "b", "okio"}, k = 1, mv = {1, 9, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
@vx8
public abstract class dy2 implements Closeable {
    public boolean a;
    public int b;
    public final ReentrantLock c = new ReentrantLock();

    @Metadata(d1 = {"\u0000\n\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\b\u0002\u0018\u00002\u00020\u0001¨\u0006\u0002"}, d2 = {"Ldy2$a;", "Lmv8;", "okio"}, k = 1, mv = {1, 9, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
    @vx8
    public static final class a implements mv8 {
        @Override // defpackage.mv8, java.io.Closeable, java.lang.AutoCloseable
        public final void close() {
        }

        @Override // defpackage.mv8, java.io.Flushable
        public final void flush() {
            throw new IllegalStateException("closed");
        }

        @Override // defpackage.mv8
        public final ko9 timeout() {
            return ko9.NONE;
        }

        @Override // defpackage.mv8
        public final void write(lg0 lg0Var, long j) {
            cr4.g(lg0Var, "source");
            throw new IllegalStateException("closed");
        }
    }

    @Metadata(d1 = {"\u0000\n\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\b\u0002\u0018\u00002\u00020\u0001¨\u0006\u0002"}, d2 = {"Ldy2$b;", "Lpx8;", "okio"}, k = 1, mv = {1, 9, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
    @vx8
    public static final class b implements px8 {
        public final dy2 a;
        public long b;
        public boolean c;

        public b(dy2 dy2Var, long j) {
            this.a = dy2Var;
            this.b = j;
        }

        @Override // java.io.Closeable, java.lang.AutoCloseable
        public final void close() {
            if (this.c) {
                return;
            }
            this.c = true;
            dy2 dy2Var = this.a;
            ReentrantLock reentrantLock = dy2Var.c;
            reentrantLock.lock();
            try {
                int i = dy2Var.b - 1;
                dy2Var.b = i;
                if (i == 0) {
                    if (dy2Var.a) {
                        reentrantLock.unlock();
                        dy2Var.a();
                    }
                }
            } finally {
                reentrantLock.unlock();
            }
        }

        @Override // defpackage.px8
        public final long read(lg0 lg0Var, long j) {
            long j2;
            long j3;
            cr4.g(lg0Var, "sink");
            if (this.c) {
                throw new IllegalStateException("closed");
            }
            long j4 = this.b;
            if (j < 0) {
                throw new IllegalArgumentException(n41.j(j, "byteCount < 0: ").toString());
            }
            long j5 = j + j4;
            long j6 = j4;
            while (true) {
                if (j6 >= j5) {
                    j2 = -1;
                    break;
                }
                me8 me8VarAf = lg0Var.af(1);
                j2 = -1;
                long j7 = j5;
                int iB = this.a.b(j6, me8VarAf.a, me8VarAf.c, (int) Math.min(j5 - j6, 8192 - r10));
                if (iB == -1) {
                    if (me8VarAf.b == me8VarAf.c) {
                        lg0Var.a = me8VarAf.a();
                        te8.a(me8VarAf);
                    }
                    if (j4 == j6) {
                        j3 = -1;
                    }
                } else {
                    me8VarAf.c += iB;
                    long j8 = iB;
                    j6 += j8;
                    lg0Var.b += j8;
                    j5 = j7;
                }
            }
            j3 = j6 - j4;
            if (j3 != j2) {
                this.b += j3;
            }
            return j3;
        }

        @Override // defpackage.px8
        public final ko9 timeout() {
            return ko9.NONE;
        }
    }

    public abstract void a();

    public abstract int b(long j, byte[] bArr, int i, int i2);

    @Override // java.io.Closeable, java.lang.AutoCloseable
    public final void close() {
        ReentrantLock reentrantLock = this.c;
        reentrantLock.lock();
        try {
            if (this.a) {
                return;
            }
            this.a = true;
            if (this.b != 0) {
                return;
            }
            reentrantLock.unlock();
            a();
        } finally {
            reentrantLock.unlock();
        }
    }

    public abstract long d();

    public final px8 h(long j) {
        ReentrantLock reentrantLock = this.c;
        reentrantLock.lock();
        try {
            if (this.a) {
                throw new IllegalStateException("closed");
            }
            this.b++;
            reentrantLock.unlock();
            return new b(this, j);
        } catch (Throwable th) {
            reentrantLock.unlock();
            throw th;
        }
    }

    public final long size() {
        ReentrantLock reentrantLock = this.c;
        reentrantLock.lock();
        try {
            if (this.a) {
                throw new IllegalStateException("closed");
            }
            reentrantLock.unlock();
            return d();
        } catch (Throwable th) {
            reentrantLock.unlock();
            throw th;
        }
    }
}
