package okhttp3;

import androidx.media3.container.NalUnitUtil;
import androidx.media3.extractor.text.ttml.TtmlNode;
import androidx.media3.session.legacy.PlaybackStateCompat;
import defpackage.C0242b;
import defpackage.cr4;
import defpackage.eu6;
import defpackage.ko9;
import defpackage.kp6;
import defpackage.lg0;
import defpackage.ly6;
import defpackage.me8;
import defpackage.n41;
import defpackage.pq6;
import defpackage.px8;
import defpackage.r15;
import defpackage.sh0;
import defpackage.uz1;
import defpackage.vx8;
import defpackage.zo0;
import java.io.Closeable;
import java.io.IOException;
import java.net.ProtocolException;
import java.util.concurrent.TimeUnit;
import kotlin.Metadata;
import okhttp3.internal.http1.HeadersReader;

/* JADX INFO: loaded from: classes3.dex */
@Metadata(d1 = {"\u0000V\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u000e\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0010\t\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0005\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0010\b\n\u0002\b\u0002\n\u0002\u0010\u000b\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0002\b\u0006\u0018\u0000 &2\u00020\u0001:\u0003&'(B\u0017\u0012\u0006\u0010\u0003\u001a\u00020\u0002\u0012\u0006\u0010\u0005\u001a\u00020\u0004¢\u0006\u0004\b\u0006\u0010\u0007B\u0011\b\u0016\u0012\u0006\u0010\t\u001a\u00020\b¢\u0006\u0004\b\u0006\u0010\nJ\u0017\u0010\r\u001a\u00020\u000b2\u0006\u0010\f\u001a\u00020\u000bH\u0002¢\u0006\u0004\b\r\u0010\u000eJ\u000f\u0010\u0010\u001a\u0004\u0018\u00010\u000f¢\u0006\u0004\b\u0010\u0010\u0011J\u000f\u0010\u0013\u001a\u00020\u0012H\u0016¢\u0006\u0004\b\u0013\u0010\u0014R\u0014\u0010\u0003\u001a\u00020\u00028\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b\u0003\u0010\u0015R\u0017\u0010\u0005\u001a\u00020\u00048\u0007¢\u0006\f\n\u0004\b\u0005\u0010\u0016\u001a\u0004\b\u0005\u0010\u0017R\u0014\u0010\u0019\u001a\u00020\u00188\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b\u0019\u0010\u001aR\u0014\u0010\u001b\u001a\u00020\u00188\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b\u001b\u0010\u001aR\u0016\u0010\u001d\u001a\u00020\u001c8\u0002@\u0002X\u0082\u000e¢\u0006\u0006\n\u0004\b\u001d\u0010\u001eR\u0016\u0010 \u001a\u00020\u001f8\u0002@\u0002X\u0082\u000e¢\u0006\u0006\n\u0004\b \u0010!R\u0016\u0010\"\u001a\u00020\u001f8\u0002@\u0002X\u0082\u000e¢\u0006\u0006\n\u0004\b\"\u0010!R\u001c\u0010$\u001a\b\u0018\u00010#R\u00020\u00008\u0002@\u0002X\u0082\u000e¢\u0006\u0006\n\u0004\b$\u0010%¨\u0006)"}, d2 = {"Lokhttp3/MultipartReader;", "Ljava/io/Closeable;", "Lsh0;", "source", "", "boundary", "<init>", "(Lsh0;Ljava/lang/String;)V", "Lokhttp3/ResponseBody;", "response", "(Lokhttp3/ResponseBody;)V", "", "maxResult", "currentPartBytesRemaining", "(J)J", "Lokhttp3/MultipartReader$Part;", "nextPart", "()Lokhttp3/MultipartReader$Part;", "Le6a;", "close", "()V", "Lsh0;", "Ljava/lang/String;", "()Ljava/lang/String;", "Lzo0;", "dashDashBoundary", "Lzo0;", "crlfDashDashBoundary", "", "partCount", "I", "", "closed", "Z", "noMoreParts", "Lokhttp3/MultipartReader$PartSource;", "currentPart", "Lokhttp3/MultipartReader$PartSource;", "Companion", "Part", "PartSource", "okhttp"}, k = 1, mv = {1, 8, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
@vx8
public final class MultipartReader implements Closeable {

    /* JADX INFO: renamed from: Companion, reason: from kotlin metadata */
    @kp6
    public static final Companion INSTANCE = new Companion(null);

    @kp6
    private static final ly6 afterBoundaryOptions;

    @kp6
    private final String boundary;
    private boolean closed;

    @kp6
    private final zo0 crlfDashDashBoundary;

    @pq6
    private PartSource currentPart;

    @kp6
    private final zo0 dashDashBoundary;
    private boolean noMoreParts;
    private int partCount;

    @kp6
    private final sh0 source;

    @Metadata(d1 = {"\u0000\u0014\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0005\b\u0080\u0003\u0018\u00002\u00020\u0001B\t\b\u0002¢\u0006\u0004\b\u0002\u0010\u0003R\u0017\u0010\u0005\u001a\u00020\u00048\u0006¢\u0006\f\n\u0004\b\u0005\u0010\u0006\u001a\u0004\b\u0007\u0010\b¨\u0006\t"}, d2 = {"Lokhttp3/MultipartReader$Companion;", "", "<init>", "()V", "Lly6;", "afterBoundaryOptions", "Lly6;", "getAfterBoundaryOptions", "()Lly6;", "okhttp"}, k = 1, mv = {1, 8, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
    public static final class Companion {
        public /* synthetic */ Companion(uz1 uz1Var) {
            this();
        }

        @kp6
        public final ly6 getAfterBoundaryOptions() {
            return MultipartReader.afterBoundaryOptions;
        }

        private Companion() {
        }
    }

    @Metadata(d1 = {"\u0000\u001e\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0002\b\u0007\u0018\u00002\u00020\u0001B\u0017\u0012\u0006\u0010\u0003\u001a\u00020\u0002\u0012\u0006\u0010\u0005\u001a\u00020\u0004¢\u0006\u0004\b\u0006\u0010\u0007J\u0010\u0010\t\u001a\u00020\bH\u0096\u0001¢\u0006\u0004\b\t\u0010\nR\u0017\u0010\u0003\u001a\u00020\u00028\u0007¢\u0006\f\n\u0004\b\u0003\u0010\u000b\u001a\u0004\b\u0003\u0010\fR\u0017\u0010\u0005\u001a\u00020\u00048\u0007¢\u0006\f\n\u0004\b\u0005\u0010\r\u001a\u0004\b\u0005\u0010\u000e¨\u0006\u000f"}, d2 = {"Lokhttp3/MultipartReader$Part;", "Ljava/io/Closeable;", "Lokhttp3/Headers;", "headers", "Lsh0;", TtmlNode.TAG_BODY, "<init>", "(Lokhttp3/Headers;Lsh0;)V", "Le6a;", "close", "()V", "Lokhttp3/Headers;", "()Lokhttp3/Headers;", "Lsh0;", "()Lsh0;", "okhttp"}, k = 1, mv = {1, 8, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
    public static final class Part implements Closeable {

        @kp6
        private final sh0 body;

        @kp6
        private final Headers headers;

        public Part(@kp6 Headers headers, @kp6 sh0 sh0Var) {
            cr4.g(headers, "headers");
            cr4.g(sh0Var, TtmlNode.TAG_BODY);
            this.headers = headers;
            this.body = sh0Var;
        }

        @r15
        @kp6
        /* JADX INFO: renamed from: body, reason: from getter */
        public final sh0 getBody() {
            return this.body;
        }

        @Override // java.io.Closeable, java.lang.AutoCloseable
        public void close() throws IOException {
            this.body.close();
        }

        @r15
        @kp6
        /* JADX INFO: renamed from: headers, reason: from getter */
        public final Headers getHeaders() {
            return this.headers;
        }
    }

    @Metadata(d1 = {"\u0000*\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\t\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0002\b\u0004\b\u0082\u0004\u0018\u00002\u00020\u0001B\u0007¢\u0006\u0004\b\u0002\u0010\u0003J\u000f\u0010\u0005\u001a\u00020\u0004H\u0016¢\u0006\u0004\b\u0005\u0010\u0006J\u001f\u0010\u000b\u001a\u00020\t2\u0006\u0010\b\u001a\u00020\u00072\u0006\u0010\n\u001a\u00020\tH\u0016¢\u0006\u0004\b\u000b\u0010\fJ\u000f\u0010\u000e\u001a\u00020\rH\u0016¢\u0006\u0004\b\u000e\u0010\u000fR\u0014\u0010\u000e\u001a\u00020\r8\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b\u000e\u0010\u0010¨\u0006\u0011"}, d2 = {"Lokhttp3/MultipartReader$PartSource;", "Lpx8;", "<init>", "(Lokhttp3/MultipartReader;)V", "Le6a;", "close", "()V", "Llg0;", "sink", "", "byteCount", "read", "(Llg0;J)J", "Lko9;", "timeout", "()Lko9;", "Lko9;", "okhttp"}, k = 1, mv = {1, 8, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
    @vx8
    public final class PartSource implements px8 {

        @kp6
        private final ko9 timeout = new ko9();

        public PartSource() {
        }

        @Override // java.io.Closeable, java.lang.AutoCloseable
        public void close() {
            if (cr4.b(MultipartReader.this.currentPart, this)) {
                MultipartReader.this.currentPart = null;
            }
        }

        @Override // defpackage.px8
        public long read(@kp6 lg0 sink, long byteCount) {
            long j;
            cr4.g(sink, "sink");
            if (byteCount < 0) {
                throw new IllegalArgumentException(n41.j(byteCount, "byteCount < 0: ").toString());
            }
            if (!cr4.b(MultipartReader.this.currentPart, this)) {
                throw new IllegalStateException("closed");
            }
            ko9 timeout = MultipartReader.this.source.getTimeout();
            ko9 ko9Var = this.timeout;
            MultipartReader multipartReader = MultipartReader.this;
            long timeoutNanos = timeout.getTimeoutNanos();
            ko9.Companion companion = ko9.INSTANCE;
            long timeoutNanos2 = ko9Var.getTimeoutNanos();
            long timeoutNanos3 = timeout.getTimeoutNanos();
            companion.getClass();
            if (timeoutNanos2 == 0 || (timeoutNanos3 != 0 && timeoutNanos2 >= timeoutNanos3)) {
                timeoutNanos2 = timeoutNanos3;
            }
            TimeUnit timeUnit = TimeUnit.NANOSECONDS;
            timeout.timeout(timeoutNanos2, timeUnit);
            if (!timeout.getHasDeadline()) {
                if (ko9Var.getHasDeadline()) {
                    timeout.deadlineNanoTime(ko9Var.deadlineNanoTime());
                }
                try {
                    long jCurrentPartBytesRemaining = multipartReader.currentPartBytesRemaining(byteCount);
                    long j2 = jCurrentPartBytesRemaining == 0 ? -1L : multipartReader.source.read(sink, jCurrentPartBytesRemaining);
                    timeout.timeout(timeoutNanos, timeUnit);
                    if (ko9Var.getHasDeadline()) {
                        timeout.clearDeadline();
                    }
                    return j2;
                } catch (Throwable th) {
                    timeout.timeout(timeoutNanos, TimeUnit.NANOSECONDS);
                    if (ko9Var.getHasDeadline()) {
                        timeout.clearDeadline();
                    }
                    throw th;
                }
            }
            long jDeadlineNanoTime = timeout.deadlineNanoTime();
            if (ko9Var.getHasDeadline()) {
                j = jDeadlineNanoTime;
                timeout.deadlineNanoTime(Math.min(timeout.deadlineNanoTime(), ko9Var.deadlineNanoTime()));
            } else {
                j = jDeadlineNanoTime;
            }
            try {
                long jCurrentPartBytesRemaining2 = multipartReader.currentPartBytesRemaining(byteCount);
                long j3 = jCurrentPartBytesRemaining2 == 0 ? -1L : multipartReader.source.read(sink, jCurrentPartBytesRemaining2);
                timeout.timeout(timeoutNanos, timeUnit);
                if (ko9Var.getHasDeadline()) {
                    timeout.deadlineNanoTime(j);
                }
                return j3;
            } catch (Throwable th2) {
                long j4 = j;
                timeout.timeout(timeoutNanos, TimeUnit.NANOSECONDS);
                if (ko9Var.getHasDeadline()) {
                    timeout.deadlineNanoTime(j4);
                }
                throw th2;
            }
        }

        @Override // defpackage.px8
        @kp6
        /* JADX INFO: renamed from: timeout, reason: from getter */
        public ko9 getTimeout() {
            return this.timeout;
        }
    }

    static {
        zo0 zo0Var = zo0.d;
        afterBoundaryOptions = ly6.a.b(zo0.a.c("\r\n"), zo0.a.c("--"), zo0.a.c(" "), zo0.a.c("\t"));
    }

    public MultipartReader(@kp6 sh0 sh0Var, @kp6 String str) throws IOException {
        cr4.g(sh0Var, "source");
        cr4.g(str, "boundary");
        this.source = sh0Var;
        this.boundary = str;
        lg0 lg0Var = new lg0();
        lg0Var.av("--");
        lg0Var.av(str);
        this.dashDashBoundary = lg0Var.bb(lg0Var.b);
        lg0 lg0Var2 = new lg0();
        lg0Var2.av("\r\n--");
        lg0Var2.av(str);
        this.crlfDashDashBoundary = lg0Var2.bb(lg0Var2.b);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final long currentPartBytesRemaining(long maxResult) {
        long j;
        this.source.i(this.crlfDashDashBoundary.e());
        lg0 b = this.source.getB();
        zo0 zo0Var = this.crlfDashDashBoundary;
        b.getClass();
        cr4.g(zo0Var, "bytes");
        b.getClass();
        cr4.g(zo0Var, "bytes");
        if (zo0Var.e() <= 0) {
            throw new IllegalArgumentException("bytes is empty");
        }
        long j2 = 0;
        if (0 < 0) {
            throw new IllegalArgumentException(n41.j(0L, "fromIndex < 0: ").toString());
        }
        me8 me8Var = b.a;
        if (me8Var == null) {
            j = -1;
        } else {
            long j3 = b.b;
            if (j3 - 0 < 0) {
                while (j3 > 0) {
                    me8Var = me8Var.g;
                    cr4.d(me8Var);
                    j3 -= (long) (me8Var.c - me8Var.b);
                }
                byte[] a = zo0Var.getA();
                byte b2 = a[0];
                int iE = zo0Var.e();
                long j4 = (b.b - ((long) iE)) + 1;
                long j5 = 0;
                loop1: while (j3 < j4) {
                    byte[] bArr = me8Var.a;
                    long j6 = j5;
                    long j7 = j3;
                    int iMin = (int) Math.min(me8Var.c, (((long) me8Var.b) + j4) - j3);
                    for (int i = (int) ((((long) me8Var.b) + j6) - j7); i < iMin; i++) {
                        if (bArr[i] == b2 && C0242b.a(me8Var, i + 1, a, iE)) {
                            j = ((long) (i - me8Var.b)) + j7;
                            break loop1;
                        }
                    }
                    j3 = j7 + ((long) (me8Var.c - me8Var.b));
                    me8Var = me8Var.f;
                    cr4.d(me8Var);
                    j5 = j3;
                }
                j = -1;
            } else {
                long j8 = 0;
                while (true) {
                    long j9 = ((long) (me8Var.c - me8Var.b)) + j2;
                    if (j9 > 0) {
                        break;
                    }
                    me8Var = me8Var.f;
                    cr4.d(me8Var);
                    j2 = j9;
                }
                byte[] a2 = zo0Var.getA();
                byte b3 = a2[0];
                int iE2 = zo0Var.e();
                long j10 = (b.b - ((long) iE2)) + 1;
                loop4: while (j2 < j10) {
                    byte[] bArr2 = me8Var.a;
                    long j11 = j2;
                    int iMin2 = (int) Math.min(me8Var.c, (((long) me8Var.b) + j10) - j2);
                    for (int i2 = (int) ((((long) me8Var.b) + j8) - j11); i2 < iMin2; i2++) {
                        if (bArr2[i2] == b3 && C0242b.a(me8Var, i2 + 1, a2, iE2)) {
                            j = ((long) (i2 - me8Var.b)) + j11;
                            break loop4;
                        }
                    }
                    j2 = ((long) (me8Var.c - me8Var.b)) + j11;
                    me8Var = me8Var.f;
                    cr4.d(me8Var);
                    j8 = j2;
                }
                j = -1;
            }
        }
        return j == -1 ? Math.min(maxResult, (this.source.getB().b - ((long) this.crlfDashDashBoundary.e())) + 1) : Math.min(maxResult, j);
    }

    @r15
    @kp6
    /* JADX INFO: renamed from: boundary, reason: from getter */
    public final String getBoundary() {
        return this.boundary;
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable
    public void close() throws IOException {
        if (this.closed) {
            return;
        }
        this.closed = true;
        this.currentPart = null;
        this.source.close();
    }

    @pq6
    public final Part nextPart() throws IOException {
        if (this.closed) {
            throw new IllegalStateException("closed");
        }
        if (this.noMoreParts) {
            return null;
        }
        if (this.partCount == 0 && this.source.o(0L, this.dashDashBoundary)) {
            this.source.skip(this.dashDashBoundary.e());
        } else {
            while (true) {
                long jCurrentPartBytesRemaining = currentPartBytesRemaining(PlaybackStateCompat.ACTION_PLAY_FROM_URI);
                if (jCurrentPartBytesRemaining == 0) {
                    break;
                }
                this.source.skip(jCurrentPartBytesRemaining);
            }
            this.source.skip(this.crlfDashDashBoundary.e());
        }
        boolean z = false;
        while (true) {
            int iBi = this.source.bi(afterBoundaryOptions);
            if (iBi == -1) {
                throw new ProtocolException("unexpected characters after boundary");
            }
            if (iBi == 0) {
                this.partCount++;
                Headers headers = new HeadersReader(this.source).readHeaders();
                PartSource partSource = new PartSource();
                this.currentPart = partSource;
                return new Part(headers, eu6.d(partSource));
            }
            if (iBi == 1) {
                if (z) {
                    throw new ProtocolException("unexpected characters after boundary");
                }
                if (this.partCount == 0) {
                    throw new ProtocolException("expected at least 1 part");
                }
                this.noMoreParts = true;
                return null;
            }
            if (iBi == 2 || iBi == 3) {
                z = true;
            }
        }
    }

    /* JADX WARN: Illegal instructions before constructor call */
    public MultipartReader(@kp6 ResponseBody responseBody) throws IOException {
        String strParameter;
        cr4.g(responseBody, "response");
        sh0 bodySource = responseBody.getBodySource();
        MediaType mediaType = responseBody.get$contentType();
        if (mediaType != null && (strParameter = mediaType.parameter("boundary")) != null) {
            this(bodySource, strParameter);
            return;
        }
        throw new ProtocolException("expected the Content-Type to have a boundary parameter");
    }
}
