package okio;

import g.a.a.a.a;
import io.netty.handler.codec.http2.Http2CodecUtil;
import io.netty.handler.codec.rtsp.RtspHeaders;
import java.io.IOException;
import java.nio.ByteBuffer;
import kotlin.Metadata;
import kotlin.jvm.internal.o;

/* JADX INFO: renamed from: z.q, reason: from Kotlin metadata */
/* JADX INFO: loaded from: classes2.dex */
@Metadata(d1 = {"\u0000f\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0006\n\u0002\u0010\u000b\n\u0000\n\u0002\u0010\u0002\n\u0002\b\u0005\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u000e\n\u0000\n\u0002\u0010\b\n\u0000\n\u0002\u0018\u0002\n\u0002\u0010\u0012\n\u0002\b\u0002\n\u0002\u0010\t\n\u0000\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0011\n\u0002\u0018\u0002\n\u0002\b\u0006\b\u0000\u0018\u00002\u00020\u0001B\r\u0012\u0006\u0010\u0002\u001a\u00020\u0003¢\u0006\u0002\u0010\u0004J\b\u0010\u0005\u001a\u00020\u0006H\u0016J\b\u0010\u000e\u001a\u00020\u000fH\u0016J\b\u0010\u0010\u001a\u00020\u0001H\u0016J\b\u0010\u0011\u001a\u00020\u0001H\u0016J\b\u0010\u0012\u001a\u00020\u000fH\u0016J\b\u0010\u0013\u001a\u00020\rH\u0016J\b\u0010\u0014\u001a\u00020\u0015H\u0016J\b\u0010\u0016\u001a\u00020\u0017H\u0016J\b\u0010\u0018\u001a\u00020\u0019H\u0016J\u0010\u0010\u001a\u001a\u00020\u001b2\u0006\u0010\u001c\u001a\u00020\u001dH\u0016J\u0010\u0010\u001a\u001a\u00020\u00012\u0006\u0010\u001c\u001a\u00020\u001eH\u0016J \u0010\u001a\u001a\u00020\u00012\u0006\u0010\u001c\u001a\u00020\u001e2\u0006\u0010\u001f\u001a\u00020\u001b2\u0006\u0010 \u001a\u00020\u001bH\u0016J\u0018\u0010\u001a\u001a\u00020\u000f2\u0006\u0010\u001c\u001a\u00020\u00062\u0006\u0010 \u001a\u00020!H\u0016J\u0010\u0010\u001a\u001a\u00020\u00012\u0006\u0010\"\u001a\u00020#H\u0016J \u0010\u001a\u001a\u00020\u00012\u0006\u0010\"\u001a\u00020#2\u0006\u0010\u001f\u001a\u00020\u001b2\u0006\u0010 \u001a\u00020\u001bH\u0016J\u0018\u0010\u001a\u001a\u00020\u00012\u0006\u0010\u001c\u001a\u00020$2\u0006\u0010 \u001a\u00020!H\u0016J\u0010\u0010%\u001a\u00020!2\u0006\u0010\u001c\u001a\u00020$H\u0016J\u0010\u0010&\u001a\u00020\u00012\u0006\u0010'\u001a\u00020\u001bH\u0016J\u0010\u0010(\u001a\u00020\u00012\u0006\u0010)\u001a\u00020!H\u0016J\u0010\u0010*\u001a\u00020\u00012\u0006\u0010)\u001a\u00020!H\u0016J\u0010\u0010+\u001a\u00020\u00012\u0006\u0010,\u001a\u00020\u001bH\u0016J\u0010\u0010-\u001a\u00020\u00012\u0006\u0010,\u001a\u00020\u001bH\u0016J\u0010\u0010.\u001a\u00020\u00012\u0006\u0010)\u001a\u00020!H\u0016J\u0010\u0010/\u001a\u00020\u00012\u0006\u0010)\u001a\u00020!H\u0016J\u0010\u00100\u001a\u00020\u00012\u0006\u00101\u001a\u00020\u001bH\u0016J\u0010\u00102\u001a\u00020\u00012\u0006\u00101\u001a\u00020\u001bH\u0016J\u0018\u00103\u001a\u00020\u00012\u0006\u00104\u001a\u00020\u00192\u0006\u00105\u001a\u000206H\u0016J(\u00103\u001a\u00020\u00012\u0006\u00104\u001a\u00020\u00192\u0006\u00107\u001a\u00020\u001b2\u0006\u00108\u001a\u00020\u001b2\u0006\u00105\u001a\u000206H\u0016J\u0010\u00109\u001a\u00020\u00012\u0006\u00104\u001a\u00020\u0019H\u0016J \u00109\u001a\u00020\u00012\u0006\u00104\u001a\u00020\u00192\u0006\u00107\u001a\u00020\u001b2\u0006\u00108\u001a\u00020\u001bH\u0016J\u0010\u0010:\u001a\u00020\u00012\u0006\u0010;\u001a\u00020\u001bH\u0016R\u001b\u0010\u0005\u001a\u00020\u00068Ö\u0002X\u0096\u0004¢\u0006\f\u0012\u0004\b\u0007\u0010\b\u001a\u0004\b\t\u0010\nR\u0010\u0010\u000b\u001a\u00020\u00068\u0006X\u0087\u0004¢\u0006\u0002\n\u0000R\u0012\u0010\f\u001a\u00020\r8\u0006@\u0006X\u0087\u000e¢\u0006\u0002\n\u0000R\u0010\u0010\u0002\u001a\u00020\u00038\u0006X\u0087\u0004¢\u0006\u0002\n\u0000¨\u0006<"}, d2 = {"Lokio/RealBufferedSink;", "Lokio/BufferedSink;", "sink", "Lokio/Sink;", "(Lokio/Sink;)V", "buffer", "Lokio/Buffer;", "getBuffer$annotations", "()V", "getBuffer", "()Lokio/Buffer;", "bufferField", "closed", "", "close", "", "emit", "emitCompleteSegments", "flush", "isOpen", "outputStream", "Ljava/io/OutputStream;", RtspHeaders.Values.TIMEOUT, "Lokio/Timeout;", "toString", "", "write", "", "source", "Ljava/nio/ByteBuffer;", "", "offset", "byteCount", "", "byteString", "Lokio/ByteString;", "Lokio/Source;", "writeAll", "writeByte", "b", "writeDecimalLong", "v", "writeHexadecimalUnsignedLong", "writeInt", "i", "writeIntLe", "writeLong", "writeLongLe", "writeShort", "s", "writeShortLe", "writeString", "string", "charset", "Ljava/nio/charset/Charset;", "beginIndex", "endIndex", "writeUtf8", "writeUtf8CodePoint", "codePoint", "okio"}, k = 1, mv = {1, 6, 0}, xi = 48)
public final class RealBufferedSink implements BufferedSink {

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final Sink f10144f;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public final Buffer f10145m;

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    public boolean f10146n;

    public RealBufferedSink(Sink sink) {
        o.f(sink, "sink");
        this.f10144f = sink;
        this.f10145m = new Buffer();
    }

    public BufferedSink a() {
        if (!(!this.f10146n)) {
            throw new IllegalStateException("closed".toString());
        }
        Buffer buffer = this.f10145m;
        long j2 = buffer.f10118m;
        if (j2 == 0) {
            j2 = 0;
        } else {
            Segment segment = buffer.f10117f;
            o.c(segment);
            Segment segment2 = segment.f10156g;
            o.c(segment2);
            int i2 = segment2.f10152c;
            if (i2 < 8192 && segment2.f10154e) {
                j2 -= (long) (i2 - segment2.f10151b);
            }
        }
        if (j2 > 0) {
            this.f10144f.write(this.f10145m, j2);
        }
        return this;
    }

    @Override // okio.BufferedSink
    /* JADX INFO: renamed from: buffer, reason: from getter */
    public Buffer getF10145m() {
        return this.f10145m;
    }

    @Override // okio.Sink, java.io.Closeable, java.lang.AutoCloseable, java.nio.channels.Channel
    public void close() throws Throwable {
        if (this.f10146n) {
            return;
        }
        Throwable th = null;
        try {
            Buffer buffer = this.f10145m;
            long j2 = buffer.f10118m;
            if (j2 > 0) {
                this.f10144f.write(buffer, j2);
            }
        } catch (Throwable th2) {
            th = th2;
        }
        try {
            this.f10144f.close();
        } catch (Throwable th3) {
            if (th == null) {
                th = th3;
            }
        }
        this.f10146n = true;
        if (th != null) {
            throw th;
        }
    }

    @Override // okio.BufferedSink, okio.Sink, java.io.Flushable
    public void flush() {
        if (!(!this.f10146n)) {
            throw new IllegalStateException("closed".toString());
        }
        Buffer buffer = this.f10145m;
        long j2 = buffer.f10118m;
        if (j2 > 0) {
            this.f10144f.write(buffer, j2);
        }
        this.f10144f.flush();
    }

    @Override // okio.BufferedSink
    /* JADX INFO: renamed from: getBuffer */
    public Buffer getF10148m() {
        return this.f10145m;
    }

    @Override // java.nio.channels.Channel
    public boolean isOpen() {
        return !this.f10146n;
    }

    @Override // okio.Sink
    /* JADX INFO: renamed from: timeout */
    public Timeout getF10143m() {
        return this.f10144f.getF10143m();
    }

    public String toString() {
        StringBuilder sbF = a.F("buffer(");
        sbF.append(this.f10144f);
        sbF.append(')');
        return sbF.toString();
    }

    @Override // java.nio.channels.WritableByteChannel
    public int write(ByteBuffer source) {
        o.f(source, "source");
        if (!(!this.f10146n)) {
            throw new IllegalStateException("closed".toString());
        }
        int iWrite = this.f10145m.write(source);
        a();
        return iWrite;
    }

    @Override // okio.BufferedSink
    public BufferedSink write(ByteString byteString) {
        o.f(byteString, "byteString");
        if (!(!this.f10146n)) {
            throw new IllegalStateException("closed".toString());
        }
        this.f10145m.z(byteString);
        a();
        return this;
    }

    @Override // okio.BufferedSink
    public BufferedSink write(byte[] bArr) {
        o.f(bArr, "source");
        if (!(!this.f10146n)) {
            throw new IllegalStateException("closed".toString());
        }
        this.f10145m.A(bArr);
        a();
        return this;
    }

    @Override // okio.BufferedSink
    public BufferedSink write(byte[] bArr, int i2, int i3) {
        o.f(bArr, "source");
        if (!(!this.f10146n)) {
            throw new IllegalStateException("closed".toString());
        }
        this.f10145m.B(bArr, i2, i3);
        a();
        return this;
    }

    @Override // okio.Sink
    public void write(Buffer buffer, long j2) {
        o.f(buffer, "source");
        if (!(!this.f10146n)) {
            throw new IllegalStateException("closed".toString());
        }
        this.f10145m.write(buffer, j2);
        a();
    }

    @Override // okio.BufferedSink
    public long writeAll(Source source) throws IOException {
        o.f(source, "source");
        long j2 = 0;
        while (true) {
            long jR = ((InputStreamSource) source).r(this.f10145m, Http2CodecUtil.DEFAULT_HEADER_LIST_SIZE);
            if (jR == -1) {
                return j2;
            }
            j2 += jR;
            a();
        }
    }

    @Override // okio.BufferedSink
    public BufferedSink writeByte(int i2) {
        if (!(!this.f10146n)) {
            throw new IllegalStateException("closed".toString());
        }
        this.f10145m.C(i2);
        a();
        return this;
    }

    @Override // okio.BufferedSink
    public BufferedSink writeDecimalLong(long j2) {
        if (!(!this.f10146n)) {
            throw new IllegalStateException("closed".toString());
        }
        this.f10145m.writeDecimalLong(j2);
        a();
        return this;
    }

    @Override // okio.BufferedSink
    public BufferedSink writeHexadecimalUnsignedLong(long j2) {
        if (!(!this.f10146n)) {
            throw new IllegalStateException("closed".toString());
        }
        this.f10145m.writeHexadecimalUnsignedLong(j2);
        a();
        return this;
    }

    @Override // okio.BufferedSink
    public BufferedSink writeInt(int i2) {
        if (!(!this.f10146n)) {
            throw new IllegalStateException("closed".toString());
        }
        this.f10145m.F(i2);
        a();
        return this;
    }

    @Override // okio.BufferedSink
    public BufferedSink writeShort(int i2) {
        if (!(!this.f10146n)) {
            throw new IllegalStateException("closed".toString());
        }
        this.f10145m.G(i2);
        a();
        return this;
    }

    @Override // okio.BufferedSink
    public BufferedSink writeUtf8(String str) {
        o.f(str, "string");
        if (!(!this.f10146n)) {
            throw new IllegalStateException("closed".toString());
        }
        this.f10145m.H(str);
        a();
        return this;
    }
}
