package 㿎;

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 okio.ByteString;
import ඓ.K.K.K.C1293;
import ᨨ.AbstractC1496;
import ᨨ.C1492;
import 㿎.ሽ.C2540;

/* renamed from: 㿎.ᝁ  reason: contains not printable characters */
/* compiled from: ResponseBody */
public abstract class AbstractC2632 implements Closeable {
    public Reader reader;

    /* renamed from: 㿎.ᝁ$ῲ  reason: contains not printable characters */
    /* compiled from: ResponseBody */
    public static final class C2633 extends Reader {

        /* renamed from: ʬ  reason: contains not printable characters */
        public final Charset f8948;

        /* renamed from: ඓ  reason: contains not printable characters */
        public Reader f8949;

        /* renamed from: ῲ  reason: contains not printable characters */
        public final AbstractC1496 f8950;

        /* renamed from: 㸾  reason: contains not printable characters */
        public boolean f8951;

        public C2633(AbstractC1496 r1, Charset charset) {
            this.f8950 = r1;
            this.f8948 = charset;
        }

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

        @Override // java.io.Reader
        public int read(char[] cArr, int i, int i2) throws IOException {
            if (!this.f8951) {
                Reader reader = this.f8949;
                if (reader == null) {
                    InputStreamReader inputStreamReader = new InputStreamReader(this.f8950.m3825(), C2540.m6645(this.f8950, this.f8948));
                    this.f8949 = inputStreamReader;
                    reader = inputStreamReader;
                }
                return reader.read(cArr, i, i2);
            }
            throw new IOException("Stream closed");
        }
    }

    /* renamed from: 㿎.ᝁ$K  reason: contains not printable characters */
    /* compiled from: ResponseBody */
    public class C2634 extends AbstractC2632 {

        /* renamed from: ʬ  reason: contains not printable characters */
        public final /* synthetic */ long f8952;

        /* renamed from: ῲ  reason: contains not printable characters */
        public final /* synthetic */ C2628 f8953;

        /* renamed from: 㸾  reason: contains not printable characters */
        public final /* synthetic */ AbstractC1496 f8954;

        public C2634(C2628 r1, long j, AbstractC1496 r4) {
            this.f8953 = r1;
            this.f8952 = j;
            this.f8954 = r4;
        }

        @Override // 㿎.AbstractC2632
        public long contentLength() {
            return this.f8952;
        }

        @Override // 㿎.AbstractC2632
        public C2628 contentType() {
            return this.f8953;
        }

        @Override // 㿎.AbstractC2632
        public AbstractC1496 source() {
            return this.f8954;
        }
    }

    private Charset charset() {
        C2628 contentType = contentType();
        if (contentType == null) {
            return C2540.f8566;
        }
        Charset charset = C2540.f8566;
        try {
            String str = contentType.f8934;
            if (str != null) {
                return Charset.forName(str);
            }
            return charset;
        } catch (IllegalArgumentException unused) {
            return charset;
        }
    }

    public static AbstractC2632 create(C2628 r4, String str) {
        Charset charset = C2540.f8566;
        if (r4 != null && (charset = r4.m6938()) == null) {
            charset = C2540.f8566;
            r4 = C2628.m6936(r4 + "; charset=utf-8");
        }
        C1492 r1 = new C1492();
        r1.m3794(str, 0, str.length(), charset);
        return create(r4, r1.f5529, r1);
    }

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

    /* JADX INFO: finally extract failed */
    public final byte[] bytes() throws IOException {
        long contentLength = contentLength();
        if (contentLength <= 2147483647L) {
            AbstractC1496 source = source();
            try {
                byte[] r3 = source.m3824();
                C2540.m6651(source);
                if (contentLength == -1 || contentLength == ((long) r3.length)) {
                    return r3;
                }
                throw new IOException("Content-Length (" + contentLength + ") and stream length (" + r3.length + ") disagree");
            } catch (Throwable th) {
                C2540.m6651(source);
                throw th;
            }
        } else {
            throw new IOException(C1293.m3409("Cannot buffer entire body for content length: ", contentLength));
        }
    }

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

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

    public abstract long contentLength();

    public abstract C2628 contentType();

    public abstract AbstractC1496 source();

    public final String string() throws IOException {
        AbstractC1496 source = source();
        try {
            return source.m3818(C2540.m6645(source, charset()));
        } finally {
            C2540.m6651(source);
        }
    }

    public static AbstractC2632 create(C2628 r3, byte[] bArr) {
        C1492 r0 = new C1492();
        r0.write(bArr);
        return create(r3, (long) bArr.length, r0);
    }

    public static AbstractC2632 create(C2628 r3, ByteString byteString) {
        C1492 r0 = new C1492();
        r0.m3795(byteString);
        return create(r3, (long) byteString.size(), r0);
    }

    public static AbstractC2632 create(C2628 r1, long j, AbstractC1496 r4) {
        if (r4 != null) {
            return new C2634(r1, j, r4);
        }
        throw new NullPointerException("source == null");
    }
}
