package okhttp3;

import androidx.media3.container.NalUnitUtil;
import defpackage.bz0;
import defpackage.cr4;
import defpackage.e52;
import defpackage.e6a;
import defpackage.f21;
import defpackage.kp6;
import defpackage.lg0;
import defpackage.n41;
import defpackage.pq6;
import defpackage.r15;
import defpackage.sh0;
import defpackage.sq3;
import defpackage.uz1;
import defpackage.vx8;
import defpackage.x15;
import defpackage.zo0;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.Charset;
import kotlin.Metadata;
import okhttp3.internal.Util;

/* JADX INFO: loaded from: classes3.dex */
@Metadata(d1 = {"\u0000l\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0010\u0000\n\u0000\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\b\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0010\t\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0004\n\u0002\u0010\u0012\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0010\u000e\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0006\b&\u0018\u0000 +2\u00020\u0001:\u0002,+B\u0007¢\u0006\u0004\b\u0002\u0010\u0003JB\u0010\u000b\u001a\u00028\u0000\"\b\b\u0000\u0010\u0005*\u00020\u00042\u0012\u0010\b\u001a\u000e\u0012\u0004\u0012\u00020\u0007\u0012\u0004\u0012\u00028\u00000\u00062\u0012\u0010\n\u001a\u000e\u0012\u0004\u0012\u00028\u0000\u0012\u0004\u0012\u00020\t0\u0006H\u0082\b¢\u0006\u0004\b\u000b\u0010\fJ\u000f\u0010\u000e\u001a\u00020\rH\u0002¢\u0006\u0004\b\u000e\u0010\u000fJ\u0011\u0010\u0011\u001a\u0004\u0018\u00010\u0010H&¢\u0006\u0004\b\u0011\u0010\u0012J\u000f\u0010\u0014\u001a\u00020\u0013H&¢\u0006\u0004\b\u0014\u0010\u0015J\r\u0010\u0017\u001a\u00020\u0016¢\u0006\u0004\b\u0017\u0010\u0018J\u000f\u0010\u0019\u001a\u00020\u0007H&¢\u0006\u0004\b\u0019\u0010\u001aJ\r\u0010\u001c\u001a\u00020\u001b¢\u0006\u0004\b\u001c\u0010\u001dJ\r\u0010\u001f\u001a\u00020\u001e¢\u0006\u0004\b\u001f\u0010 J\r\u0010\"\u001a\u00020!¢\u0006\u0004\b\"\u0010#J\r\u0010%\u001a\u00020$¢\u0006\u0004\b%\u0010&J\u000f\u0010(\u001a\u00020'H\u0016¢\u0006\u0004\b(\u0010\u0003R\u0018\u0010)\u001a\u0004\u0018\u00010!8\u0002@\u0002X\u0082\u000e¢\u0006\u0006\n\u0004\b)\u0010*¨\u0006-"}, d2 = {"Lokhttp3/ResponseBody;", "Ljava/io/Closeable;", "<init>", "()V", "", "T", "Lkotlin/Function1;", "Lsh0;", "consumer", "", "sizeMapper", "consumeSource", "(Lsq3;Lsq3;)Ljava/lang/Object;", "Ljava/nio/charset/Charset;", "charset", "()Ljava/nio/charset/Charset;", "Lokhttp3/MediaType;", "contentType", "()Lokhttp3/MediaType;", "", "contentLength", "()J", "Ljava/io/InputStream;", "byteStream", "()Ljava/io/InputStream;", "source", "()Lsh0;", "", "bytes", "()[B", "Lzo0;", "byteString", "()Lzo0;", "Ljava/io/Reader;", "charStream", "()Ljava/io/Reader;", "", "string", "()Ljava/lang/String;", "Le6a;", "close", "reader", "Ljava/io/Reader;", "Companion", "BomAwareReader", "okhttp"}, k = 1, mv = {1, 8, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
@vx8
public abstract class ResponseBody implements Closeable {

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

    @pq6
    private Reader reader;

    @Metadata(d1 = {"\u00004\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0010\u0019\n\u0000\n\u0002\u0010\b\n\u0002\b\u0004\n\u0002\u0018\u0002\n\u0002\b\u0004\n\u0002\u0010\u000b\n\u0002\b\u0005\b\u0000\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\r\u001a\u00020\n2\u0006\u0010\t\u001a\u00020\b2\u0006\u0010\u000b\u001a\u00020\n2\u0006\u0010\f\u001a\u00020\nH\u0016¢\u0006\u0004\b\r\u0010\u000eJ\u000f\u0010\u0010\u001a\u00020\u000fH\u0016¢\u0006\u0004\b\u0010\u0010\u0011R\u0014\u0010\u0003\u001a\u00020\u00028\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b\u0003\u0010\u0012R\u0014\u0010\u0005\u001a\u00020\u00048\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b\u0005\u0010\u0013R\u0016\u0010\u0015\u001a\u00020\u00148\u0002@\u0002X\u0082\u000e¢\u0006\u0006\n\u0004\b\u0015\u0010\u0016R\u0018\u0010\u0017\u001a\u0004\u0018\u00010\u00018\u0002@\u0002X\u0082\u000e¢\u0006\u0006\n\u0004\b\u0017\u0010\u0018¨\u0006\u0019"}, d2 = {"Lokhttp3/ResponseBody$BomAwareReader;", "Ljava/io/Reader;", "Lsh0;", "source", "Ljava/nio/charset/Charset;", "charset", "<init>", "(Lsh0;Ljava/nio/charset/Charset;)V", "", "cbuf", "", "off", "len", "read", "([CII)I", "Le6a;", "close", "()V", "Lsh0;", "Ljava/nio/charset/Charset;", "", "closed", "Z", "delegate", "Ljava/io/Reader;", "okhttp"}, k = 1, mv = {1, 8, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
    @vx8
    public static final class BomAwareReader extends Reader {

        @kp6
        private final Charset charset;
        private boolean closed;

        @pq6
        private Reader delegate;

        @kp6
        private final sh0 source;

        public BomAwareReader(@kp6 sh0 sh0Var, @kp6 Charset charset) {
            cr4.g(sh0Var, "source");
            cr4.g(charset, "charset");
            this.source = sh0Var;
            this.charset = charset;
        }

        @Override // java.io.Reader, java.io.Closeable, java.lang.AutoCloseable
        public void close() throws IOException {
            e6a e6aVar;
            this.closed = true;
            Reader reader = this.delegate;
            if (reader != null) {
                reader.close();
                e6aVar = e6a.a;
            } else {
                e6aVar = null;
            }
            if (e6aVar == null) {
                this.source.close();
            }
        }

        @Override // java.io.Reader
        public int read(@kp6 char[] cbuf, int off, int len) throws IOException {
            cr4.g(cbuf, "cbuf");
            if (this.closed) {
                throw new IOException("Stream closed");
            }
            Reader inputStreamReader = this.delegate;
            if (inputStreamReader == null) {
                inputStreamReader = new InputStreamReader(this.source.cg(), Util.readBomAsCharset(this.source, this.charset));
                this.delegate = inputStreamReader;
            }
            return inputStreamReader.read(cbuf, off, len);
        }
    }

    @Metadata(d1 = {"\u00006\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\b\u0002\n\u0002\u0010\u000e\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0010\u0012\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\u0010\t\n\u0002\b\t\b\u0086\u0003\u0018\u00002\u00020\u0001B\t\b\u0002¢\u0006\u0004\b\u0002\u0010\u0003J\u001f\u0010\n\u001a\u00020\u0007*\u00020\u00042\n\b\u0002\u0010\u0006\u001a\u0004\u0018\u00010\u0005H\u0007¢\u0006\u0004\b\b\u0010\tJ\u001f\u0010\n\u001a\u00020\u0007*\u00020\u000b2\n\b\u0002\u0010\u0006\u001a\u0004\u0018\u00010\u0005H\u0007¢\u0006\u0004\b\b\u0010\fJ\u001f\u0010\n\u001a\u00020\u0007*\u00020\r2\n\b\u0002\u0010\u0006\u001a\u0004\u0018\u00010\u0005H\u0007¢\u0006\u0004\b\b\u0010\u000eJ)\u0010\u0013\u001a\u00020\u0007*\u00020\u000f2\n\b\u0002\u0010\u0006\u001a\u0004\u0018\u00010\u00052\b\b\u0002\u0010\u0011\u001a\u00020\u0010H\u0007¢\u0006\u0004\b\b\u0010\u0012J!\u0010\b\u001a\u00020\u00072\b\u0010\u0006\u001a\u0004\u0018\u00010\u00052\u0006\u0010\u0014\u001a\u00020\u0004H\u0007¢\u0006\u0004\b\b\u0010\u0015J!\u0010\b\u001a\u00020\u00072\b\u0010\u0006\u001a\u0004\u0018\u00010\u00052\u0006\u0010\u0014\u001a\u00020\u000bH\u0007¢\u0006\u0004\b\b\u0010\u0016J!\u0010\b\u001a\u00020\u00072\b\u0010\u0006\u001a\u0004\u0018\u00010\u00052\u0006\u0010\u0014\u001a\u00020\rH\u0007¢\u0006\u0004\b\b\u0010\u0017J)\u0010\b\u001a\u00020\u00072\b\u0010\u0006\u001a\u0004\u0018\u00010\u00052\u0006\u0010\u0011\u001a\u00020\u00102\u0006\u0010\u0014\u001a\u00020\u000fH\u0007¢\u0006\u0004\b\b\u0010\u0018¨\u0006\u0019"}, d2 = {"Lokhttp3/ResponseBody$Companion;", "", "<init>", "()V", "", "Lokhttp3/MediaType;", "contentType", "Lokhttp3/ResponseBody;", "create", "(Ljava/lang/String;Lokhttp3/MediaType;)Lokhttp3/ResponseBody;", "toResponseBody", "", "([BLokhttp3/MediaType;)Lokhttp3/ResponseBody;", "Lzo0;", "(Lzo0;Lokhttp3/MediaType;)Lokhttp3/ResponseBody;", "Lsh0;", "", "contentLength", "(Lsh0;Lokhttp3/MediaType;J)Lokhttp3/ResponseBody;", "asResponseBody", "content", "(Lokhttp3/MediaType;Ljava/lang/String;)Lokhttp3/ResponseBody;", "(Lokhttp3/MediaType;[B)Lokhttp3/ResponseBody;", "(Lokhttp3/MediaType;Lzo0;)Lokhttp3/ResponseBody;", "(Lokhttp3/MediaType;JLsh0;)Lokhttp3/ResponseBody;", "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();
        }

        public static /* synthetic */ ResponseBody create$default(Companion companion, String str, MediaType mediaType, int i, Object obj) {
            if ((i & 1) != 0) {
                mediaType = null;
            }
            return companion.create(str, mediaType);
        }

        @x15
        @r15
        @kp6
        public final ResponseBody create(@kp6 String str, @pq6 MediaType mediaType) {
            cr4.g(str, "<this>");
            Charset charset = bz0.a;
            if (mediaType != null) {
                Charset charsetCharset$default = MediaType.charset$default(mediaType, null, 1, null);
                if (charsetCharset$default == null) {
                    mediaType = MediaType.INSTANCE.parse(mediaType + "; charset=utf-8");
                } else {
                    charset = charsetCharset$default;
                }
            }
            lg0 lg0Var = new lg0();
            cr4.g(charset, "charset");
            lg0Var.as(str, 0, str.length(), charset);
            return create(lg0Var, mediaType, lg0Var.b);
        }

        private Companion() {
        }

        public static /* synthetic */ ResponseBody create$default(Companion companion, byte[] bArr, MediaType mediaType, int i, Object obj) {
            if ((i & 1) != 0) {
                mediaType = null;
            }
            return companion.create(bArr, mediaType);
        }

        public static /* synthetic */ ResponseBody create$default(Companion companion, zo0 zo0Var, MediaType mediaType, int i, Object obj) {
            if ((i & 1) != 0) {
                mediaType = null;
            }
            return companion.create(zo0Var, mediaType);
        }

        public static /* synthetic */ ResponseBody create$default(Companion companion, sh0 sh0Var, MediaType mediaType, long j, int i, Object obj) {
            if ((i & 1) != 0) {
                mediaType = null;
            }
            if ((i & 2) != 0) {
                j = -1;
            }
            return companion.create(sh0Var, mediaType, j);
        }

        @x15
        @r15
        @kp6
        public final ResponseBody create(@kp6 byte[] bArr, @pq6 MediaType mediaType) {
            cr4.g(bArr, "<this>");
            lg0 lg0Var = new lg0();
            lg0Var.m177write(bArr);
            return create(lg0Var, mediaType, bArr.length);
        }

        @x15
        @r15
        @kp6
        public final ResponseBody create(@kp6 zo0 zo0Var, @pq6 MediaType mediaType) {
            cr4.g(zo0Var, "<this>");
            lg0 lg0Var = new lg0();
            lg0Var.ah(zo0Var);
            return create(lg0Var, mediaType, zo0Var.e());
        }

        @x15
        @r15
        @kp6
        public final ResponseBody create(@kp6 final sh0 sh0Var, @pq6 final MediaType mediaType, final long j) {
            cr4.g(sh0Var, "<this>");
            return new ResponseBody() { // from class: okhttp3.ResponseBody$Companion$asResponseBody$1
                @Override // okhttp3.ResponseBody
                /* JADX INFO: renamed from: contentLength, reason: from getter */
                public long get$contentLength() {
                    return j;
                }

                @Override // okhttp3.ResponseBody
                @pq6
                /* JADX INFO: renamed from: contentType, reason: from getter */
                public MediaType get$contentType() {
                    return mediaType;
                }

                @Override // okhttp3.ResponseBody
                @kp6
                /* JADX INFO: renamed from: source, reason: from getter */
                public sh0 get$this_asResponseBody() {
                    return sh0Var;
                }
            };
        }

        @e52
        @x15
        @kp6
        public final ResponseBody create(@pq6 MediaType contentType, @kp6 String content) {
            cr4.g(content, "content");
            return create(content, contentType);
        }

        @e52
        @x15
        @kp6
        public final ResponseBody create(@pq6 MediaType contentType, @kp6 byte[] content) {
            cr4.g(content, "content");
            return create(content, contentType);
        }

        @e52
        @x15
        @kp6
        public final ResponseBody create(@pq6 MediaType contentType, @kp6 zo0 content) {
            cr4.g(content, "content");
            return create(content, contentType);
        }

        @e52
        @x15
        @kp6
        public final ResponseBody create(@pq6 MediaType contentType, long contentLength, @kp6 sh0 content) {
            cr4.g(content, "content");
            return create(content, contentType, contentLength);
        }
    }

    private final Charset charset() {
        Charset charset;
        MediaType mediaTypeContentType = get$contentType();
        return (mediaTypeContentType == null || (charset = mediaTypeContentType.charset(bz0.a)) == null) ? bz0.a : charset;
    }

    private final <T> T consumeSource(sq3<? super sh0, ? extends T> consumer, sq3<? super T, Integer> sizeMapper) throws IOException {
        long jContentLength = get$contentLength();
        if (jContentLength > 2147483647L) {
            throw new IOException(n41.j(jContentLength, "Cannot buffer entire body for content length: "));
        }
        sh0 sh0VarSource = get$this_asResponseBody();
        try {
            T t = (T) consumer.invoke(sh0VarSource);
            f21.a(sh0VarSource, null);
            int iIntValue = ((Number) sizeMapper.invoke(t)).intValue();
            if (jContentLength == -1 || jContentLength == iIntValue) {
                return t;
            }
            throw new IOException("Content-Length (" + jContentLength + ") and stream length (" + iIntValue + ") disagree");
        } finally {
        }
    }

    @x15
    @r15
    @kp6
    public static final ResponseBody create(@kp6 sh0 sh0Var, @pq6 MediaType mediaType, long j) {
        return INSTANCE.create(sh0Var, mediaType, j);
    }

    @kp6
    public final InputStream byteStream() {
        return get$this_asResponseBody().cg();
    }

    @kp6
    public final zo0 byteString() throws IOException {
        long jContentLength = get$contentLength();
        if (jContentLength > 2147483647L) {
            throw new IOException(n41.j(jContentLength, "Cannot buffer entire body for content length: "));
        }
        sh0 sh0VarSource = get$this_asResponseBody();
        try {
            zo0 zo0VarBt = sh0VarSource.bt();
            sh0VarSource.close();
            int iE = zo0VarBt.e();
            if (jContentLength == -1 || jContentLength == iE) {
                return zo0VarBt;
            }
            throw new IOException("Content-Length (" + jContentLength + ") and stream length (" + iE + ") disagree");
        } finally {
        }
    }

    @kp6
    public final byte[] bytes() throws IOException {
        long jContentLength = get$contentLength();
        if (jContentLength > 2147483647L) {
            throw new IOException(n41.j(jContentLength, "Cannot buffer entire body for content length: "));
        }
        sh0 sh0VarSource = get$this_asResponseBody();
        try {
            byte[] bArrBf = sh0VarSource.bf();
            sh0VarSource.close();
            int length = bArrBf.length;
            if (jContentLength == -1 || jContentLength == length) {
                return bArrBf;
            }
            throw new IOException("Content-Length (" + jContentLength + ") and stream length (" + length + ") disagree");
        } finally {
        }
    }

    @kp6
    public final Reader charStream() {
        Reader reader = this.reader;
        if (reader != null) {
            return reader;
        }
        BomAwareReader bomAwareReader = new BomAwareReader(get$this_asResponseBody(), charset());
        this.reader = bomAwareReader;
        return bomAwareReader;
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable
    public void close() {
        Util.closeQuietly(get$this_asResponseBody());
    }

    /* JADX INFO: renamed from: contentLength */
    public abstract long get$contentLength();

    @pq6
    /* JADX INFO: renamed from: contentType */
    public abstract MediaType get$contentType();

    @kp6
    /* JADX INFO: renamed from: source */
    public abstract sh0 get$this_asResponseBody();

    @kp6
    public final String string() throws IOException {
        sh0 sh0VarSource = get$this_asResponseBody();
        try {
            String strBn = sh0VarSource.bn(Util.readBomAsCharset(sh0VarSource, charset()));
            sh0VarSource.close();
            return strBn;
        } finally {
        }
    }

    @x15
    @r15
    @kp6
    public static final ResponseBody create(@kp6 zo0 zo0Var, @pq6 MediaType mediaType) {
        return INSTANCE.create(zo0Var, mediaType);
    }

    @x15
    @r15
    @kp6
    public static final ResponseBody create(@kp6 String str, @pq6 MediaType mediaType) {
        return INSTANCE.create(str, mediaType);
    }

    @e52
    @x15
    @kp6
    public static final ResponseBody create(@pq6 MediaType mediaType, long j, @kp6 sh0 sh0Var) {
        return INSTANCE.create(mediaType, j, sh0Var);
    }

    @e52
    @x15
    @kp6
    public static final ResponseBody create(@pq6 MediaType mediaType, @kp6 zo0 zo0Var) {
        return INSTANCE.create(mediaType, zo0Var);
    }

    @e52
    @x15
    @kp6
    public static final ResponseBody create(@pq6 MediaType mediaType, @kp6 String str) {
        return INSTANCE.create(mediaType, str);
    }

    @e52
    @x15
    @kp6
    public static final ResponseBody create(@pq6 MediaType mediaType, @kp6 byte[] bArr) {
        return INSTANCE.create(mediaType, bArr);
    }

    @x15
    @r15
    @kp6
    public static final ResponseBody create(@kp6 byte[] bArr, @pq6 MediaType mediaType) {
        return INSTANCE.create(bArr, mediaType);
    }
}
