package okhttp3;

import d.b.a.a.a;
import g.h0.c.l;
import j.b0;
import j.c0;
import j.d;
import j.g;
import j.h;
import j.r;
import j.w;
import java.io.Closeable;
import java.io.IOException;
import java.net.ProtocolException;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import okhttp3.internal.http1.HeadersReader;

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

    /* JADX INFO: compiled from: MultipartReader.kt */
    public static final class Companion {
        private Companion() {
        }

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

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

    /* JADX INFO: compiled from: MultipartReader.kt */
    public static final class Part implements Closeable {
        private final g body;
        private final Headers headers;

        public Part(Headers headers, g gVar) {
            l.f(headers, "headers");
            l.f(gVar, "body");
            this.headers = headers;
            this.body = gVar;
        }

        public final g 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 */
    public final class PartSource implements b0 {
        private final c0 timeout = new c0();

        public PartSource() {
        }

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

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

        @Override // j.b0
        public c0 timeout() {
            return this.timeout;
        }
    }

    static {
        r.a aVar = r.f4631b;
        h.a aVar2 = h.f4612e;
        afterBoundaryOptions = aVar.c(aVar2.c("\r\n"), aVar2.c("--"), aVar2.c(" "), aVar2.c("\t"));
    }

    public MultipartReader(g gVar, String str) throws IOException {
        l.f(gVar, "source");
        l.f(str, "boundary");
        this.source = gVar;
        this.boundary = str;
        d dVar = new d();
        dVar.P("--");
        dVar.P(str);
        this.dashDashBoundary = dVar.I();
        d dVar2 = new d();
        dVar2.P("\r\n--");
        dVar2.P(str);
        this.crlfDashDashBoundary = dVar2.I();
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final long currentPartBytesRemaining(long j2) throws IOException {
        long j3;
        this.source.e0(this.crlfDashDashBoundary.c());
        d buffer = this.source.getBuffer();
        h hVar = this.crlfDashDashBoundary;
        Objects.requireNonNull(buffer);
        l.f(hVar, "bytes");
        l.f(hVar, "bytes");
        if (!(hVar.c() > 0)) {
            throw new IllegalArgumentException("bytes is empty".toString());
        }
        w wVar = buffer.f4600b;
        if (wVar != null) {
            long j4 = buffer.f4601e;
            if (j4 - 0 < 0) {
                while (j4 > 0) {
                    wVar = wVar.f4654g;
                    l.c(wVar);
                    j4 -= (long) (wVar.f4650c - wVar.f4649b);
                }
                byte[] bArrE = hVar.e();
                byte b2 = bArrE[0];
                int iC = hVar.c();
                long j5 = (buffer.f4601e - ((long) iC)) + 1;
                long j6 = j4;
                long j7 = 0;
                loop1: while (j6 < j5) {
                    byte[] bArr = wVar.a;
                    long j8 = j5;
                    int iMin = (int) Math.min(wVar.f4650c, (((long) wVar.f4649b) + j5) - j6);
                    for (int i2 = (int) ((((long) wVar.f4649b) + j7) - j6); i2 < iMin; i2++) {
                        if (bArr[i2] == b2 && j.d0.a.a(wVar, i2 + 1, bArrE, 1, iC)) {
                            j3 = ((long) (i2 - wVar.f4649b)) + j6;
                            break loop1;
                        }
                    }
                    j7 = j6 + ((long) (wVar.f4650c - wVar.f4649b));
                    wVar = wVar.f4653f;
                    l.c(wVar);
                    j6 = j7;
                    j5 = j8;
                }
                j3 = -1;
            } else {
                long j9 = 0;
                while (true) {
                    long j10 = ((long) (wVar.f4650c - wVar.f4649b)) + j9;
                    if (j10 > 0) {
                        break;
                    }
                    wVar = wVar.f4653f;
                    l.c(wVar);
                    j9 = j10;
                }
                byte[] bArrE2 = hVar.e();
                byte b3 = bArrE2[0];
                int iC2 = hVar.c();
                long j11 = (buffer.f4601e - ((long) iC2)) + 1;
                long j12 = 0;
                loop4: while (j9 < j11) {
                    byte[] bArr2 = wVar.a;
                    long j13 = j9;
                    int iMin2 = (int) Math.min(wVar.f4650c, (((long) wVar.f4649b) + j11) - j9);
                    for (int i3 = (int) ((((long) wVar.f4649b) + j12) - j13); i3 < iMin2; i3++) {
                        if (bArr2[i3] == b3 && j.d0.a.a(wVar, i3 + 1, bArrE2, 1, iC2)) {
                            j3 = ((long) (i3 - wVar.f4649b)) + j13;
                            break loop4;
                        }
                    }
                    j12 = ((long) (wVar.f4650c - wVar.f4649b)) + j13;
                    wVar = wVar.f4653f;
                    l.c(wVar);
                    j9 = j12;
                }
                j3 = -1;
            }
        } else {
            j3 = -1;
        }
        return j3 == -1 ? Math.min(j2, (this.source.getBuffer().f4601e - ((long) this.crlfDashDashBoundary.c())) + 1) : Math.min(j2, j3);
    }

    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.B(0L, this.dashDashBoundary)) {
            this.source.K(this.dashDashBoundary.c());
        } else {
            while (true) {
                long jCurrentPartBytesRemaining = currentPartBytesRemaining(8192L);
                if (jCurrentPartBytesRemaining == 0) {
                    break;
                }
                this.source.K(jCurrentPartBytesRemaining);
            }
            this.source.K(this.crlfDashDashBoundary.c());
        }
        boolean z = false;
        while (true) {
            int iK0 = this.source.k0(afterBoundaryOptions);
            if (iK0 == -1) {
                throw new ProtocolException("unexpected characters after boundary");
            }
            if (iK0 == 0) {
                this.partCount++;
                Headers headers = new HeadersReader(this.source).readHeaders();
                PartSource partSource = new PartSource();
                this.currentPart = partSource;
                return new Part(headers, f.a.y.a.t(partSource));
            }
            if (iK0 == 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 (iK0 == 2 || iK0 == 3) {
                z = true;
            }
        }
    }

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