package ᨨ;

import android.support.v4.media.session.PlaybackStateCompat;
import androidx.recyclerview.widget.RecyclerView;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import okio.ByteString;
import ඓ.K.K.K.C1293;

/* renamed from: ᨨ.㗽  reason: contains not printable characters */
/* compiled from: RealBufferedSource */
public final class C1488 implements AbstractC1496 {

    /* renamed from: ʬ  reason: contains not printable characters */
    public final AbstractC1494 f5523;

    /* renamed from: ῲ  reason: contains not printable characters */
    public final C1492 f5524 = new C1492();

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

    public C1488(AbstractC1494 r2) {
        if (r2 != null) {
            this.f5523 = r2;
            return;
        }
        throw new NullPointerException("source == null");
    }

    @Override // ᨨ.AbstractC1494, java.io.Closeable, java.lang.AutoCloseable, java.nio.channels.Channel
    public void close() throws IOException {
        if (!this.f5525) {
            this.f5525 = true;
            this.f5523.close();
            this.f5524.m3773();
        }
    }

    public boolean isOpen() {
        return !this.f5525;
    }

    @Override // ᨨ.AbstractC1494
    public long read(C1492 r7, long j) throws IOException {
        if (r7 == null) {
            throw new IllegalArgumentException("sink == null");
        } else if (j < 0) {
            throw new IllegalArgumentException(C1293.m3409("byteCount < 0: ", j));
        } else if (!this.f5525) {
            C1492 r2 = this.f5524;
            if (r2.f5529 == 0 && this.f5523.read(r2, PlaybackStateCompat.ACTION_PLAY_FROM_URI) == -1) {
                return -1;
            }
            return this.f5524.read(r7, Math.min(j, this.f5524.f5529));
        } else {
            throw new IllegalStateException("closed");
        }
    }

    @Override // ᨨ.AbstractC1496
    public byte readByte() throws IOException {
        m3759(1);
        return this.f5524.readByte();
    }

    @Override // ᨨ.AbstractC1496
    public int readInt() throws IOException {
        m3759(4);
        return this.f5524.readInt();
    }

    @Override // ᨨ.AbstractC1496
    public short readShort() throws IOException {
        m3759(2);
        return this.f5524.readShort();
    }

    @Override // ᨨ.AbstractC1496
    public void skip(long j) throws IOException {
        if (!this.f5525) {
            while (j > 0) {
                C1492 r2 = this.f5524;
                if (r2.f5529 == 0 && this.f5523.read(r2, PlaybackStateCompat.ACTION_PLAY_FROM_URI) == -1) {
                    throw new EOFException();
                }
                long min = Math.min(j, this.f5524.f5529);
                this.f5524.skip(min);
                j -= min;
            }
            return;
        }
        throw new IllegalStateException("closed");
    }

    @Override // ᨨ.AbstractC1494
    public C1477 timeout() {
        return this.f5523.timeout();
    }

    public String toString() {
        StringBuilder r0 = C1293.m3416("buffer(");
        r0.append(this.f5523);
        r0.append(")");
        return r0.toString();
    }

    @Override // ᨨ.AbstractC1496
    /* renamed from: ʬ  reason: contains not printable characters */
    public String m3743() throws IOException {
        return m3744(RecyclerView.FOREVER_NS);
    }

    @Override // ᨨ.AbstractC1496
    /* renamed from: ඓ  reason: contains not printable characters */
    public int m3745() throws IOException {
        m3759(4);
        return this.f5524.m3769();
    }

    @Override // ᨨ.AbstractC1496
    /* renamed from: ῲ  reason: contains not printable characters */
    public ByteString m3746(long j) throws IOException {
        if (m3755(j)) {
            return this.f5524.m3774(j);
        }
        throw new EOFException();
    }

    @Override // ᨨ.AbstractC1496
    /* renamed from: ⁱ  reason: contains not printable characters */
    public long m3748() throws IOException {
        m3759(1);
        int i = 0;
        while (true) {
            int i2 = i + 1;
            if (!m3755((long) i2)) {
                break;
            }
            byte r3 = this.f5524.m3777((long) i);
            if ((r3 >= 48 && r3 <= 57) || ((r3 >= 97 && r3 <= 102) || (r3 >= 65 && r3 <= 70))) {
                i = i2;
            } else if (i == 0) {
                throw new NumberFormatException(String.format("Expected leading [0-9a-fA-F] character but was %#x", Byte.valueOf(r3)));
            }
        }
        return this.f5524.m3778();
    }

    @Override // ᨨ.AbstractC1496
    /* renamed from: K  reason: contains not printable characters */
    public C1492 m3753() {
        return this.f5524;
    }

    @Override // ᨨ.AbstractC1496
    /* renamed from: 㴹  reason: contains not printable characters */
    public boolean m3757() throws IOException {
        if (!this.f5525) {
            return this.f5524.m3802() && this.f5523.read(this.f5524, PlaybackStateCompat.ACTION_PLAY_FROM_URI) == -1;
        }
        throw new IllegalStateException("closed");
    }

    @Override // ᨨ.AbstractC1496
    /* renamed from: 㸾  reason: contains not printable characters */
    public void m3759(long j) throws IOException {
        if (!m3755(j)) {
            throw new EOFException();
        }
    }

    @Override // ᨨ.AbstractC1496
    /* renamed from: 㿎  reason: contains not printable characters */
    public InputStream m3761() {
        return new C1489();
    }

    @Override // ᨨ.AbstractC1496
    /* renamed from: ʬ  reason: contains not printable characters */
    public String m3744(long j) throws IOException {
        if (j >= 0) {
            long j2 = j == RecyclerView.FOREVER_NS ? Long.MAX_VALUE : j + 1;
            long r6 = m3750((byte) 10, 0, j2);
            if (r6 != -1) {
                return this.f5524.m3807(r6);
            }
            if (j2 < RecyclerView.FOREVER_NS && m3755(j2) && this.f5524.m3777(j2 - 1) == 13 && m3755(1 + j2) && this.f5524.m3777(j2) == 10) {
                return this.f5524.m3807(j2);
            }
            C1492 r62 = new C1492();
            C1492 r0 = this.f5524;
            r0.m3796(r62, 0, Math.min(32L, r0.f5529));
            StringBuilder r1 = C1293.m3416("\\n not found: limit=");
            r1.append(Math.min(this.f5524.f5529, j));
            r1.append(" content=");
            r1.append(r62.m3772().hex());
            r1.append((char) 8230);
            throw new EOFException(r1.toString());
        }
        throw new IllegalArgumentException(C1293.m3409("limit < 0: ", j));
    }

    /* renamed from: K  reason: contains not printable characters */
    public boolean m3755(long j) throws IOException {
        C1492 r0;
        if (j < 0) {
            throw new IllegalArgumentException(C1293.m3409("byteCount < 0: ", j));
        } else if (!this.f5525) {
            do {
                r0 = this.f5524;
                if (r0.f5529 >= j) {
                    return true;
                }
            } while (this.f5523.read(r0, PlaybackStateCompat.ACTION_PLAY_FROM_URI) != -1);
            return false;
        } else {
            throw new IllegalStateException("closed");
        }
    }

    @Override // ᨨ.AbstractC1496
    /* renamed from: 㸾  reason: contains not printable characters */
    public byte[] m3760() throws IOException {
        this.f5524.m3783(this.f5523);
        return this.f5524.m3805();
    }

    @Override // ᨨ.AbstractC1496
    /* renamed from: ῲ  reason: contains not printable characters */
    public short m3747() throws IOException {
        m3759(2);
        return this.f5524.m3776();
    }

    @Override // ᨨ.AbstractC1496
    /* renamed from: 㴹  reason: contains not printable characters */
    public byte[] m3758(long j) throws IOException {
        if (m3755(j)) {
            return this.f5524.m3803(j);
        }
        throw new EOFException();
    }

    /* renamed from: ᨨ.㗽$K  reason: contains not printable characters */
    /* compiled from: RealBufferedSource */
    public class C1489 extends InputStream {
        public C1489() {
        }

        @Override // java.io.InputStream
        public int available() throws IOException {
            C1488 r0 = C1488.this;
            if (!r0.f5525) {
                return (int) Math.min(r0.f5524.f5529, 2147483647L);
            }
            throw new IOException("closed");
        }

        @Override // java.io.Closeable, java.lang.AutoCloseable, java.io.InputStream
        public void close() throws IOException {
            C1488.this.close();
        }

        @Override // java.io.InputStream
        public int read() throws IOException {
            C1488 r0 = C1488.this;
            if (!r0.f5525) {
                C1492 r1 = r0.f5524;
                if (r1.f5529 == 0 && r0.f5523.read(r1, PlaybackStateCompat.ACTION_PLAY_FROM_URI) == -1) {
                    return -1;
                }
                return C1488.this.f5524.readByte() & 255;
            }
            throw new IOException("closed");
        }

        public String toString() {
            return C1488.this + ".inputStream()";
        }

        @Override // java.io.InputStream
        public int read(byte[] bArr, int i, int i2) throws IOException {
            if (!C1488.this.f5525) {
                C1491.m3764((long) bArr.length, (long) i, (long) i2);
                C1488 r0 = C1488.this;
                C1492 r1 = r0.f5524;
                if (r1.f5529 == 0 && r0.f5523.read(r1, PlaybackStateCompat.ACTION_PLAY_FROM_URI) == -1) {
                    return -1;
                }
                return C1488.this.f5524.m3779(bArr, i, i2);
            }
            throw new IOException("closed");
        }
    }

    /* renamed from: K  reason: contains not printable characters */
    public void m3754(byte[] bArr) throws IOException {
        try {
            m3759((long) bArr.length);
            this.f5524.m3798(bArr);
        } catch (EOFException e) {
            int i = 0;
            while (true) {
                C1492 r2 = this.f5524;
                long j = r2.f5529;
                if (j > 0) {
                    int r22 = r2.m3779(bArr, i, (int) j);
                    if (r22 != -1) {
                        i += r22;
                    } else {
                        throw new AssertionError();
                    }
                } else {
                    throw e;
                }
            }
        }
    }

    @Override // java.nio.channels.ReadableByteChannel
    public int read(ByteBuffer byteBuffer) throws IOException {
        C1492 r0 = this.f5524;
        if (r0.f5529 == 0 && this.f5523.read(r0, PlaybackStateCompat.ACTION_PLAY_FROM_URI) == -1) {
            return -1;
        }
        return this.f5524.read(byteBuffer);
    }

    @Override // ᨨ.AbstractC1496
    /* renamed from: K  reason: contains not printable characters */
    public long m3751(AbstractC1473 r10) throws IOException {
        if (r10 != null) {
            long j = 0;
            while (this.f5523.read(this.f5524, PlaybackStateCompat.ACTION_PLAY_FROM_URI) != -1) {
                long r4 = this.f5524.m3801();
                if (r4 > 0) {
                    j += r4;
                    r10.m3692(this.f5524, r4);
                }
            }
            C1492 r42 = this.f5524;
            long j2 = r42.f5529;
            if (j2 <= 0) {
                return j;
            }
            long j3 = j + j2;
            r10.m3692(r42, j2);
            return j3;
        }
        throw new IllegalArgumentException("sink == null");
    }

    @Override // ᨨ.AbstractC1496
    /* renamed from: K  reason: contains not printable characters */
    public String m3752(Charset charset) throws IOException {
        if (charset != null) {
            this.f5524.m3783(this.f5523);
            return this.f5524.m3785(charset);
        }
        throw new IllegalArgumentException("charset == null");
    }

    @Override // ᨨ.AbstractC1496
    /* renamed from: K  reason: contains not printable characters */
    public long m3749(byte b) throws IOException {
        return m3750(b, 0, RecyclerView.FOREVER_NS);
    }

    /* renamed from: K  reason: contains not printable characters */
    public long m3750(byte b, long j, long j2) throws IOException {
        if (this.f5525) {
            throw new IllegalStateException("closed");
        } else if (j < 0 || j2 < j) {
            throw new IllegalArgumentException(String.format("fromIndex=%s toIndex=%s", Long.valueOf(j), Long.valueOf(j2)));
        } else {
            while (j < j2) {
                long r0 = this.f5524.m3781(b, j, j2);
                if (r0 == -1) {
                    C1492 r02 = this.f5524;
                    long j3 = r02.f5529;
                    if (j3 >= j2 || this.f5523.read(r02, PlaybackStateCompat.ACTION_PLAY_FROM_URI) == -1) {
                        break;
                    }
                    j = Math.max(j, j3);
                } else {
                    return r0;
                }
            }
            return -1;
        }
    }

    @Override // ᨨ.AbstractC1496
    /* renamed from: K  reason: contains not printable characters */
    public boolean m3756(long j, ByteString byteString) throws IOException {
        int size = byteString.size();
        if (this.f5525) {
            throw new IllegalStateException("closed");
        } else if (j < 0 || size < 0 || byteString.size() - 0 < size) {
            return false;
        } else {
            for (int i = 0; i < size; i++) {
                long j2 = ((long) i) + j;
                if (!(m3755(1 + j2) && this.f5524.m3777(j2) == byteString.getByte(0 + i))) {
                    return false;
                }
            }
            return true;
        }
    }
}
