package okhttp3;

import androidx.media3.session.legacy.PlaybackStateCompat;
import java.io.Closeable;
import java.io.IOException;
import java.net.ProtocolException;
import java.util.concurrent.TimeUnit;
import kotlin.jvm.internal.g;
import kotlin.jvm.internal.l;
import okhttp3.internal.http1.HeadersReader;
import okio.C0688f;
import okio.E;
import okio.F;
import okio.h;
import okio.i;
import okio.q;
import okio.t;

/* JADX INFO: loaded from: classes2.dex */
public final class MultipartReader implements Closeable {
    public static final Companion Companion = new Companion(null);
    private static final t afterBoundaryOptions;
    private final String boundary;
    private boolean closed;
    private final i crlfDashDashBoundary;
    private PartSource currentPart;
    private final i dashDashBoundary;
    private boolean noMoreParts;
    private int partCount;
    private final h source;

    public static final class Companion {
        private Companion() {
        }

        public final t getAfterBoundaryOptions() {
            return MultipartReader.afterBoundaryOptions;
        }

        public /* synthetic */ Companion(g gVar) {
            this();
        }
    }

    public static final class Part implements Closeable {
        private final h body;
        private final Headers headers;

        public Part(Headers headers, h body) {
            l.e(headers, "headers");
            l.e(body, "body");
            this.headers = headers;
            this.body = body;
        }

        public final h body() {
            return this.body;
        }

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

        public final Headers headers() {
            return this.headers;
        }
    }

    private final class PartSource implements E {
        private final F timeout = new F();

        public PartSource() {
        }

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

        @Override // okio.E
        public long read(C0688f sink, long j2) {
            l.e(sink, "sink");
            if (!(j2 >= 0)) {
                throw new IllegalArgumentException(("byteCount < 0: " + j2).toString());
            }
            if (!l.a(MultipartReader.this.currentPart, this)) {
                throw new IllegalStateException("closed");
            }
            F fTimeout = MultipartReader.this.source.timeout();
            F f2 = this.timeout;
            long jTimeoutNanos = fTimeout.timeoutNanos();
            long jA = F.Companion.a(f2.timeoutNanos(), fTimeout.timeoutNanos());
            TimeUnit timeUnit = TimeUnit.NANOSECONDS;
            fTimeout.timeout(jA, timeUnit);
            if (!fTimeout.hasDeadline()) {
                if (f2.hasDeadline()) {
                    fTimeout.deadlineNanoTime(f2.deadlineNanoTime());
                }
                try {
                    long jCurrentPartBytesRemaining = MultipartReader.this.currentPartBytesRemaining(j2);
                    long j3 = jCurrentPartBytesRemaining == 0 ? -1L : MultipartReader.this.source.read(sink, jCurrentPartBytesRemaining);
                    fTimeout.timeout(jTimeoutNanos, timeUnit);
                    if (f2.hasDeadline()) {
                        fTimeout.clearDeadline();
                    }
                    return j3;
                } catch (Throwable th) {
                    fTimeout.timeout(jTimeoutNanos, TimeUnit.NANOSECONDS);
                    if (f2.hasDeadline()) {
                        fTimeout.clearDeadline();
                    }
                    throw th;
                }
            }
            long jDeadlineNanoTime = fTimeout.deadlineNanoTime();
            if (f2.hasDeadline()) {
                fTimeout.deadlineNanoTime(Math.min(fTimeout.deadlineNanoTime(), f2.deadlineNanoTime()));
            }
            try {
                long jCurrentPartBytesRemaining2 = MultipartReader.this.currentPartBytesRemaining(j2);
                long j4 = jCurrentPartBytesRemaining2 == 0 ? -1L : MultipartReader.this.source.read(sink, jCurrentPartBytesRemaining2);
                fTimeout.timeout(jTimeoutNanos, timeUnit);
                if (f2.hasDeadline()) {
                    fTimeout.deadlineNanoTime(jDeadlineNanoTime);
                }
                return j4;
            } catch (Throwable th2) {
                fTimeout.timeout(jTimeoutNanos, TimeUnit.NANOSECONDS);
                if (f2.hasDeadline()) {
                    fTimeout.deadlineNanoTime(jDeadlineNanoTime);
                }
                throw th2;
            }
        }

        @Override // okio.E
        public F timeout() {
            return this.timeout;
        }
    }

    static {
        t.a aVar = t.f6052x;
        i.a aVar2 = i.f6032y;
        afterBoundaryOptions = aVar.d(aVar2.d("\r\n"), aVar2.d("--"), aVar2.d(" "), aVar2.d("\t"));
    }

    public MultipartReader(h source, String boundary) throws IOException {
        l.e(source, "source");
        l.e(boundary, "boundary");
        this.source = source;
        this.boundary = boundary;
        this.dashDashBoundary = new C0688f().writeUtf8("--").writeUtf8(boundary).B();
        this.crlfDashDashBoundary = new C0688f().writeUtf8("\r\n--").writeUtf8(boundary).B();
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final long currentPartBytesRemaining(long j2) {
        this.source.W(this.crlfDashDashBoundary.s());
        long jM = this.source.getBuffer().m(this.crlfDashDashBoundary);
        return jM == -1 ? Math.min(j2, (this.source.getBuffer().G() - ((long) this.crlfDashDashBoundary.s())) + 1) : Math.min(j2, jM);
    }

    public final String boundary() {
        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();
    }

    public final Part nextPart() throws IOException {
        if (this.closed) {
            throw new IllegalStateException("closed");
        }
        if (this.noMoreParts) {
            return null;
        }
        if (this.partCount == 0 && this.source.x(0L, this.dashDashBoundary)) {
            this.source.skip(this.dashDashBoundary.s());
        } else {
            while (true) {
                long jCurrentPartBytesRemaining = currentPartBytesRemaining(PlaybackStateCompat.ACTION_PLAY_FROM_URI);
                if (jCurrentPartBytesRemaining == 0) {
                    break;
                }
                this.source.skip(jCurrentPartBytesRemaining);
            }
            this.source.skip(this.crlfDashDashBoundary.s());
        }
        boolean z2 = false;
        while (true) {
            int iD0 = this.source.d0(afterBoundaryOptions);
            if (iD0 == -1) {
                throw new ProtocolException("unexpected characters after boundary");
            }
            if (iD0 == 0) {
                this.partCount++;
                Headers headers = new HeadersReader(this.source).readHeaders();
                PartSource partSource = new PartSource();
                this.currentPart = partSource;
                return new Part(headers, q.d(partSource));
            }
            if (iD0 == 1) {
                if (z2) {
                    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 (iD0 == 2 || iD0 == 3) {
                z2 = true;
            }
        }
    }

    /* JADX WARN: Illegal instructions before constructor call */
    public MultipartReader(ResponseBody response) throws IOException {
        String strParameter;
        l.e(response, "response");
        h hVarSource = response.source();
        MediaType mediaTypeContentType = response.contentType();
        if (mediaTypeContentType != null && (strParameter = mediaTypeContentType.parameter("boundary")) != null) {
            this(hVarSource, strParameter);
            return;
        }
        throw new ProtocolException("expected the Content-Type to have a boundary parameter");
    }
}
