package okhttp3;

import g.h0.c.l;
import g.m0.a;
import j.d;
import j.g;
import j.h;
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 okhttp3.internal.Util;

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

    /* JADX INFO: compiled from: ResponseBody.kt */
    public static final class BomAwareReader extends Reader {
        private final Charset charset;
        private boolean closed;
        private Reader delegate;
        private final g source;

        public BomAwareReader(g gVar, Charset charset) {
            l.f(gVar, "source");
            l.f(charset, "charset");
            this.source = gVar;
            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[] cArr, int i2, int i3) throws IOException {
            l.f(cArr, "cbuf");
            if (this.closed) {
                throw new IOException("Stream closed");
            }
            Reader inputStreamReader = this.delegate;
            if (inputStreamReader == null) {
                inputStreamReader = new InputStreamReader(this.source.j0(), Util.readBomAsCharset(this.source, this.charset));
                this.delegate = inputStreamReader;
            }
            return inputStreamReader.read(cArr, i2, i3);
        }
    }

    /* JADX INFO: compiled from: ResponseBody.kt */
    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 str, MediaType mediaType) {
            l.f(str, "$this$toResponseBody");
            Charset charset = a.f4156b;
            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;
                }
            }
            d dVar = new d();
            l.f(str, "string");
            l.f(charset, "charset");
            d dVarO = dVar.O(str, 0, str.length(), charset);
            return create(dVarO, mediaType, dVarO.f4601e);
        }

        public /* synthetic */ Companion(g.h0.c.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, h hVar, MediaType mediaType, int i2, Object obj) {
            if ((i2 & 1) != 0) {
                mediaType = null;
            }
            return companion.create(hVar, mediaType);
        }

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

        public final ResponseBody create(byte[] bArr, MediaType mediaType) {
            l.f(bArr, "$this$toResponseBody");
            d dVar = new d();
            dVar.z(bArr);
            return create(dVar, mediaType, bArr.length);
        }

        public final ResponseBody create(h hVar, MediaType mediaType) {
            l.f(hVar, "$this$toResponseBody");
            d dVar = new d();
            dVar.y(hVar);
            return create(dVar, mediaType, hVar.c());
        }

        public final ResponseBody create(final g gVar, final MediaType mediaType, final long j2) {
            l.f(gVar, "$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 g source() {
                    return gVar;
                }
            };
        }

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

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

        public final ResponseBody create(MediaType mediaType, h hVar) {
            l.f(hVar, "content");
            return create(hVar, mediaType);
        }

        public final ResponseBody create(MediaType mediaType, long j2, g gVar) {
            l.f(gVar, "content");
            return create(gVar, mediaType, j2);
        }
    }

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

    /* JADX WARN: Type inference failed for: r5v3, types: [T, java.lang.Object] */
    private final <T> T consumeSource(g.h0.b.l<? super g, ? extends T> lVar, g.h0.b.l<? super T, Integer> lVar2) throws IOException {
        long jContentLength = contentLength();
        if (jContentLength > Integer.MAX_VALUE) {
            throw new IOException(d.b.a.a.a.l("Cannot buffer entire body for content length: ", jContentLength));
        }
        g gVarSource = source();
        try {
            T tInvoke = lVar.invoke(gVarSource);
            f.a.y.a.B(gVarSource, null);
            int iIntValue = lVar2.invoke(tInvoke).intValue();
            if (jContentLength == -1 || jContentLength == iIntValue) {
                return tInvoke;
            }
            throw new IOException("Content-Length (" + jContentLength + ") and stream length (" + iIntValue + ") disagree");
        } finally {
        }
    }

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

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

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

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

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

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

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

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

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

    public final h byteString() throws IOException {
        long jContentLength = contentLength();
        if (jContentLength > Integer.MAX_VALUE) {
            throw new IOException(d.b.a.a.a.l("Cannot buffer entire body for content length: ", jContentLength));
        }
        g gVarSource = source();
        try {
            h hVarI = gVarSource.I();
            f.a.y.a.B(gVarSource, null);
            int iC = hVarI.c();
            if (jContentLength == -1 || jContentLength == iC) {
                return hVarI;
            }
            throw new IOException("Content-Length (" + jContentLength + ") and stream length (" + iC + ") disagree");
        } finally {
        }
    }

    public final byte[] bytes() throws IOException {
        long jContentLength = contentLength();
        if (jContentLength > Integer.MAX_VALUE) {
            throw new IOException(d.b.a.a.a.l("Cannot buffer entire body for content length: ", jContentLength));
        }
        g gVarSource = source();
        try {
            byte[] bArrK = gVarSource.k();
            f.a.y.a.B(gVarSource, null);
            int length = bArrK.length;
            if (jContentLength == -1 || jContentLength == length) {
                return bArrK;
            }
            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 g source();

    public final String string() throws IOException {
        g gVarSource = source();
        try {
            String strC = gVarSource.C(Util.readBomAsCharset(gVarSource, charset()));
            f.a.y.a.B(gVarSource, null);
            return strC;
        } finally {
        }
    }
}
