package okhttp3;

import h.i;
import h.x.d.k;
import i.d0;
import i.e0;
import i.f;
import i.h;
import i.q;
import i.t;
import java.io.Closeable;
import java.io.IOException;
import java.net.ProtocolException;
import java.util.concurrent.TimeUnit;
import okhttp3.internal.http1.HeadersReader;

/* JADX INFO: compiled from: MultipartReader.kt */
/* JADX INFO: loaded from: classes.dex */
@i(bv = {1, 0, 3}, d1 = {"\u0000X\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0010\u0002\n\u0002\b\u0002\n\u0002\u0010\t\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0010\u000e\n\u0002\b\u0003\n\u0002\u0010\u000b\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0004\n\u0002\u0010\b\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\b\u0018\u0000 &2\u00020\u0001:\u0003&'(B\u0011\b\u0016\u0012\u0006\u0010\"\u001a\u00020!¢\u0006\u0004\b#\u0010$B\u0017\u0012\u0006\u0010\u001f\u001a\u00020\u001e\u0012\u0006\u0010\r\u001a\u00020\f¢\u0006\u0004\b#\u0010%J\u000f\u0010\u0003\u001a\u00020\u0002H\u0016¢\u0006\u0004\b\u0003\u0010\u0004J\u0017\u0010\u0007\u001a\u00020\u00052\u0006\u0010\u0006\u001a\u00020\u0005H\u0002¢\u0006\u0004\b\u0007\u0010\bJ\u000f\u0010\n\u001a\u0004\u0018\u00010\t¢\u0006\u0004\b\n\u0010\u000bR\u0019\u0010\r\u001a\u00020\f8\u0007@\u0006¢\u0006\f\n\u0004\b\r\u0010\u000e\u001a\u0004\b\r\u0010\u000fR\u0016\u0010\u0011\u001a\u00020\u00108\u0002@\u0002X\u0082\u000e¢\u0006\u0006\n\u0004\b\u0011\u0010\u0012R\u0016\u0010\u0014\u001a\u00020\u00138\u0002@\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b\u0014\u0010\u0015R\u001c\u0010\u0017\u001a\b\u0018\u00010\u0016R\u00020\u00008\u0002@\u0002X\u0082\u000e¢\u0006\u0006\n\u0004\b\u0017\u0010\u0018R\u0016\u0010\u0019\u001a\u00020\u00138\u0002@\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b\u0019\u0010\u0015R\u0016\u0010\u001a\u001a\u00020\u00108\u0002@\u0002X\u0082\u000e¢\u0006\u0006\n\u0004\b\u001a\u0010\u0012R\u0016\u0010\u001c\u001a\u00020\u001b8\u0002@\u0002X\u0082\u000e¢\u0006\u0006\n\u0004\b\u001c\u0010\u001dR\u0016\u0010\u001f\u001a\u00020\u001e8\u0002@\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b\u001f\u0010 ¨\u0006)"}, d2 = {"Lokhttp3/MultipartReader;", "Ljava/io/Closeable;", "", "close", "()V", "", "maxResult", "currentPartBytesRemaining", "(J)J", "Lokhttp3/MultipartReader$Part;", "nextPart", "()Lokhttp3/MultipartReader$Part;", "", "boundary", "Ljava/lang/String;", "()Ljava/lang/String;", "", "closed", "Z", "Lokio/ByteString;", "crlfDashDashBoundary", "Lokio/ByteString;", "Lokhttp3/MultipartReader$PartSource;", "currentPart", "Lokhttp3/MultipartReader$PartSource;", "dashDashBoundary", "noMoreParts", "", "partCount", "I", "Lokio/BufferedSource;", "source", "Lokio/BufferedSource;", "Lokhttp3/ResponseBody;", "response", "<init>", "(Lokhttp3/ResponseBody;)V", "(Lokio/BufferedSource;Ljava/lang/String;)V", "Companion", "Part", "PartSource", "okhttp"}, k = 1, mv = {1, 1, 15}, pn = "", xi = 0, xs = "")
public final class MultipartReader implements Closeable {
    public static final Companion Companion = new Companion(null);
    private static final t afterBoundaryOptions = t.f1040g.d(i.i.f1027h.d("\r\n"), i.i.f1027h.d("--"), i.i.f1027h.d(" "), i.i.f1027h.d("\t"));
    private final String boundary;
    private boolean closed;
    private final i.i crlfDashDashBoundary;
    private PartSource currentPart;
    private final i.i dashDashBoundary;
    private boolean noMoreParts;
    private int partCount;
    private final h source;

    /* JADX INFO: compiled from: MultipartReader.kt */
    @i(bv = {1, 0, 3}, d1 = {"\u0000\f\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0007\b\u0080\u0003\u0018\u0000B\t\b\u0002¢\u0006\u0004\b\u0006\u0010\u0007R\u0019\u0010\u0002\u001a\u00020\u00018\u0006@\u0006¢\u0006\f\n\u0004\b\u0002\u0010\u0003\u001a\u0004\b\u0004\u0010\u0005¨\u0006\b"}, d2 = {"Lokhttp3/MultipartReader$Companion;", "Lokio/Options;", "afterBoundaryOptions", "Lokio/Options;", "getAfterBoundaryOptions", "()Lokio/Options;", "<init>", "()V", "okhttp"}, k = 1, mv = {1, 1, 15}, pn = "", xi = 0, xs = "")
    public static final class Companion {
        private Companion() {
        }

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

        public /* synthetic */ Companion(h.x.d.h hVar) {
            this();
        }
    }

    /* JADX INFO: compiled from: MultipartReader.kt */
    @i(bv = {1, 0, 3}, d1 = {"\u0000 \n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0010\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0002\b\u0006\u0018\u00002\u00020\u0001B\u0017\u0012\u0006\u0010\n\u001a\u00020\t\u0012\u0006\u0010\u0006\u001a\u00020\u0005¢\u0006\u0004\b\r\u0010\u000eJ\u0010\u0010\u0003\u001a\u00020\u0002H\u0096\u0001¢\u0006\u0004\b\u0003\u0010\u0004R\u0019\u0010\u0006\u001a\u00020\u00058\u0007@\u0006¢\u0006\f\n\u0004\b\u0006\u0010\u0007\u001a\u0004\b\u0006\u0010\bR\u0019\u0010\n\u001a\u00020\t8\u0007@\u0006¢\u0006\f\n\u0004\b\n\u0010\u000b\u001a\u0004\b\n\u0010\f¨\u0006\u000f"}, d2 = {"Lokhttp3/MultipartReader$Part;", "Ljava/io/Closeable;", "", "close", "()V", "Lokio/BufferedSource;", "body", "Lokio/BufferedSource;", "()Lokio/BufferedSource;", "Lokhttp3/Headers;", "headers", "Lokhttp3/Headers;", "()Lokhttp3/Headers;", "<init>", "(Lokhttp3/Headers;Lokio/BufferedSource;)V", "okhttp"}, k = 1, mv = {1, 1, 15}, pn = "", xi = 0, xs = "")
    public static final class Part implements Closeable {
        private final h body;
        private final Headers headers;

        public Part(Headers headers, h hVar) {
            k.d(headers, "headers");
            k.d(hVar, "body");
            this.headers = headers;
            this.body = hVar;
        }

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

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

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

    /* JADX INFO: compiled from: MultipartReader.kt */
    @i(bv = {1, 0, 3}, d1 = {"\u0000&\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0010\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\u0006\b\u0082\u0004\u0018\u00002\u00020\u0001B\u0007¢\u0006\u0004\b\u000f\u0010\u0010J\u000f\u0010\u0003\u001a\u00020\u0002H\u0016¢\u0006\u0004\b\u0003\u0010\u0004J\u001f\u0010\t\u001a\u00020\u00072\u0006\u0010\u0006\u001a\u00020\u00052\u0006\u0010\b\u001a\u00020\u0007H\u0016¢\u0006\u0004\b\t\u0010\nJ\u000f\u0010\f\u001a\u00020\u000bH\u0016¢\u0006\u0004\b\f\u0010\rR\u0016\u0010\f\u001a\u00020\u000b8\u0002@\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b\f\u0010\u000e¨\u0006\u0011"}, d2 = {"Lokhttp3/MultipartReader$PartSource;", "Li/d0;", "", "close", "()V", "Lokio/Buffer;", "sink", "", "byteCount", "read", "(Lokio/Buffer;J)J", "Lokio/Timeout;", "timeout", "()Lokio/Timeout;", "Lokio/Timeout;", "<init>", "(Lokhttp3/MultipartReader;)V", "okhttp"}, k = 1, mv = {1, 1, 15}, pn = "", xi = 0, xs = "")
    private final class PartSource implements d0 {
        private final e0 timeout = new e0();

        public PartSource() {
        }

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

        @Override // i.d0
        public long read(f fVar, long j2) {
            k.d(fVar, "sink");
            if (!(j2 >= 0)) {
                throw new IllegalArgumentException(("byteCount < 0: " + j2).toString());
            }
            if (!k.a(MultipartReader.this.currentPart, this)) {
                throw new IllegalStateException("closed".toString());
            }
            e0 e0VarTimeout = MultipartReader.this.source.timeout();
            e0 e0Var = this.timeout;
            long jTimeoutNanos = e0VarTimeout.timeoutNanos();
            e0VarTimeout.timeout(e0.Companion.a(e0Var.timeoutNanos(), e0VarTimeout.timeoutNanos()), TimeUnit.NANOSECONDS);
            if (!e0VarTimeout.hasDeadline()) {
                if (e0Var.hasDeadline()) {
                    e0VarTimeout.deadlineNanoTime(e0Var.deadlineNanoTime());
                }
                try {
                    long jCurrentPartBytesRemaining = MultipartReader.this.currentPartBytesRemaining(j2);
                    return jCurrentPartBytesRemaining == 0 ? -1L : MultipartReader.this.source.read(fVar, jCurrentPartBytesRemaining);
                } finally {
                    e0VarTimeout.timeout(jTimeoutNanos, TimeUnit.NANOSECONDS);
                    if (e0Var.hasDeadline()) {
                        e0VarTimeout.clearDeadline();
                    }
                }
            }
            long jDeadlineNanoTime = e0VarTimeout.deadlineNanoTime();
            if (e0Var.hasDeadline()) {
                e0VarTimeout.deadlineNanoTime(Math.min(e0VarTimeout.deadlineNanoTime(), e0Var.deadlineNanoTime()));
            }
            try {
                long jCurrentPartBytesRemaining2 = MultipartReader.this.currentPartBytesRemaining(j2);
                return jCurrentPartBytesRemaining2 == 0 ? -1L : MultipartReader.this.source.read(fVar, jCurrentPartBytesRemaining2);
            } finally {
                e0VarTimeout.timeout(jTimeoutNanos, TimeUnit.NANOSECONDS);
                if (e0Var.hasDeadline()) {
                    e0VarTimeout.deadlineNanoTime(jDeadlineNanoTime);
                }
            }
        }

        @Override // i.d0
        public e0 timeout() {
            return this.timeout;
        }
    }

    public MultipartReader(h hVar, String str) throws IOException {
        k.d(hVar, "source");
        k.d(str, "boundary");
        this.source = hVar;
        this.boundary = str;
        f fVar = new f();
        fVar.i0("--");
        fVar.i0(this.boundary);
        this.dashDashBoundary = fVar.g();
        f fVar2 = new f();
        fVar2.i0("\r\n--");
        fVar2.i0(this.boundary);
        this.crlfDashDashBoundary = fVar2.g();
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final long currentPartBytesRemaining(long j2) throws IOException {
        this.source.Y(this.crlfDashDashBoundary.s());
        long jQ = this.source.c().q(this.crlfDashDashBoundary);
        return jQ == -1 ? Math.min(j2, (this.source.c().J() - ((long) this.crlfDashDashBoundary.s())) + 1) : Math.min(j2, jQ);
    }

    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".toString());
        }
        if (this.noMoreParts) {
            return null;
        }
        if (this.partCount == 0 && this.source.e0(0L, this.dashDashBoundary)) {
            this.source.skip(this.dashDashBoundary.s());
        } else {
            while (true) {
                long jCurrentPartBytesRemaining = currentPartBytesRemaining(8192L);
                if (jCurrentPartBytesRemaining == 0) {
                    break;
                }
                this.source.skip(jCurrentPartBytesRemaining);
            }
            this.source.skip(this.crlfDashDashBoundary.s());
        }
        boolean z = false;
        while (true) {
            int iM0 = this.source.m0(afterBoundaryOptions);
            if (iM0 == -1) {
                throw new ProtocolException("unexpected characters after boundary");
            }
            if (iM0 == 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 (iM0 == 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 (iM0 == 2 || iM0 == 3) {
                z = true;
            }
        }
    }

    /* JADX WARN: Illegal instructions before constructor call */
    public MultipartReader(ResponseBody responseBody) throws IOException {
        String strParameter;
        k.d(responseBody, "response");
        h hVarSource = responseBody.source();
        MediaType mediaTypeContentType = responseBody.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");
    }
}
