package okhttp3;

import E1.d;
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.jvm.internal.g;
import kotlin.jvm.internal.k;
import kotlin.jvm.internal.l;
import okhttp3.internal.Util;
import okio.C0688f;
import okio.h;
import okio.i;
import w1.b;

/* JADX INFO: loaded from: classes2.dex */
public abstract class ResponseBody implements Closeable {
    public static final Companion Companion = new Companion(null);
    private Reader reader;

    public static final class BomAwareReader extends Reader {
        private final Charset charset;
        private boolean closed;
        private Reader delegate;
        private final h source;

        public BomAwareReader(h source, Charset charset) {
            l.e(source, "source");
            l.e(charset, "charset");
            this.source = source;
            this.charset = charset;
        }

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

        @Override // java.io.Reader
        public int read(char[] cbuf, int i2, int i3) throws IOException {
            l.e(cbuf, "cbuf");
            if (this.closed) {
                throw new IOException("Stream closed");
            }
            Reader inputStreamReader = this.delegate;
            if (inputStreamReader == null) {
                inputStreamReader = new InputStreamReader(this.source.b0(), Util.readBomAsCharset(this.source, this.charset));
                this.delegate = inputStreamReader;
            }
            return inputStreamReader.read(cbuf, i2, i3);
        }
    }

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

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

        public final ResponseBody create(String toResponseBody, MediaType mediaType) {
            l.e(toResponseBody, "$this$toResponseBody");
            Charset charset = d.f531b;
            if (mediaType != null) {
                Charset charsetCharset$default = MediaType.charset$default(mediaType, null, 1, null);
                if (charsetCharset$default == null) {
                    mediaType = MediaType.Companion.parse(mediaType + "; charset=utf-8");
                } else {
                    charset = charsetCharset$default;
                }
            }
            C0688f c0688fA0 = new C0688f().a0(toResponseBody, charset);
            return create(c0688fA0, mediaType, c0688fA0.G());
        }

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

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

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

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

        public final ResponseBody create(byte[] toResponseBody, MediaType mediaType) {
            l.e(toResponseBody, "$this$toResponseBody");
            return create(new C0688f().write(toResponseBody), mediaType, toResponseBody.length);
        }

        public final ResponseBody create(i toResponseBody, MediaType mediaType) {
            l.e(toResponseBody, "$this$toResponseBody");
            return create(new C0688f().write(toResponseBody), mediaType, toResponseBody.s());
        }

        public final ResponseBody create(final h asResponseBody, final MediaType mediaType, final long j2) {
            l.e(asResponseBody, "$this$asResponseBody");
            return new ResponseBody() { // from class: okhttp3.ResponseBody$Companion$asResponseBody$1
                @Override // okhttp3.ResponseBody
                public long contentLength() {
                    return j2;
                }

                @Override // okhttp3.ResponseBody
                public MediaType contentType() {
                    return mediaType;
                }

                @Override // okhttp3.ResponseBody
                public h source() {
                    return asResponseBody;
                }
            };
        }

        public final ResponseBody create(MediaType mediaType, String content) {
            l.e(content, "content");
            return create(content, mediaType);
        }

        public final ResponseBody create(MediaType mediaType, byte[] content) {
            l.e(content, "content");
            return create(content, mediaType);
        }

        public final ResponseBody create(MediaType mediaType, i content) {
            l.e(content, "content");
            return create(content, mediaType);
        }

        public final ResponseBody create(MediaType mediaType, long j2, h content) {
            l.e(content, "content");
            return create(content, mediaType, j2);
        }
    }

    private final Charset charset() {
        Charset charset;
        MediaType mediaTypeContentType = contentType();
        return (mediaTypeContentType == null || (charset = mediaTypeContentType.charset(d.f531b)) == null) ? d.f531b : charset;
    }

    private final <T> T consumeSource(y1.l lVar, y1.l lVar2) throws IOException {
        long jContentLength = contentLength();
        if (jContentLength > Integer.MAX_VALUE) {
            throw new IOException("Cannot buffer entire body for content length: " + jContentLength);
        }
        h hVarSource = source();
        try {
            T t2 = (T) lVar.invoke(hVarSource);
            k.b(1);
            b.a(hVarSource, null);
            k.a(1);
            int iIntValue = ((Number) lVar2.invoke(t2)).intValue();
            if (jContentLength == -1 || jContentLength == iIntValue) {
                return t2;
            }
            throw new IOException("Content-Length (" + jContentLength + ") and stream length (" + iIntValue + ") disagree");
        } finally {
        }
    }

    public static final ResponseBody create(String str, MediaType mediaType) {
        return Companion.create(str, mediaType);
    }

    public final InputStream byteStream() {
        return source().b0();
    }

    public final i byteString() throws IOException {
        long jContentLength = contentLength();
        if (jContentLength > Integer.MAX_VALUE) {
            throw new IOException("Cannot buffer entire body for content length: " + jContentLength);
        }
        h hVarSource = source();
        try {
            i iVarB = hVarSource.B();
            b.a(hVarSource, null);
            int iS = iVarB.s();
            if (jContentLength == -1 || jContentLength == iS) {
                return iVarB;
            }
            throw new IOException("Content-Length (" + jContentLength + ") and stream length (" + iS + ") disagree");
        } finally {
        }
    }

    public final byte[] bytes() throws IOException {
        long jContentLength = contentLength();
        if (jContentLength > Integer.MAX_VALUE) {
            throw new IOException("Cannot buffer entire body for content length: " + jContentLength);
        }
        h hVarSource = source();
        try {
            byte[] bArrG = hVarSource.g();
            b.a(hVarSource, null);
            int length = bArrG.length;
            if (jContentLength == -1 || jContentLength == length) {
                return bArrG;
            }
            throw new IOException("Content-Length (" + jContentLength + ") and stream length (" + length + ") disagree");
        } finally {
        }
    }

    public final Reader charStream() {
        Reader reader = this.reader;
        if (reader != null) {
            return reader;
        }
        BomAwareReader bomAwareReader = new BomAwareReader(source(), charset());
        this.reader = bomAwareReader;
        return bomAwareReader;
    }

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

    public abstract long contentLength();

    public abstract MediaType contentType();

    public abstract h source();

    public final String string() throws IOException {
        h hVarSource = source();
        try {
            String strY = hVarSource.y(Util.readBomAsCharset(hVarSource, charset()));
            b.a(hVarSource, null);
            return strY;
        } finally {
        }
    }

    public static final ResponseBody create(MediaType mediaType, long j2, h hVar) {
        return Companion.create(mediaType, j2, hVar);
    }

    public static final ResponseBody create(MediaType mediaType, String str) {
        return Companion.create(mediaType, str);
    }

    public static final ResponseBody create(MediaType mediaType, i iVar) {
        return Companion.create(mediaType, iVar);
    }

    public static final ResponseBody create(MediaType mediaType, byte[] bArr) {
        return Companion.create(mediaType, bArr);
    }

    public static final ResponseBody create(h hVar, MediaType mediaType, long j2) {
        return Companion.create(hVar, mediaType, j2);
    }

    public static final ResponseBody create(i iVar, MediaType mediaType) {
        return Companion.create(iVar, mediaType);
    }

    public static final ResponseBody create(byte[] bArr, MediaType mediaType) {
        return Companion.create(bArr, mediaType);
    }
}
