package okio;

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

/* JADX INFO: loaded from: classes2.dex */
@Metadata(d1 = {"\u0000-\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u000e\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\t\n\u0000*\u0001\u0000\b\n\u0018\u00002\u00020\u0001J\b\u0010\u0002\u001a\u00020\u0003H\u0016J\b\u0010\u0004\u001a\u00020\u0003H\u0016J\b\u0010\u0005\u001a\u00020\u0006H\u0016J\b\u0010\u0007\u001a\u00020\bH\u0016J\u0018\u0010\t\u001a\u00020\u00032\u0006\u0010\n\u001a\u00020\u000b2\u0006\u0010\f\u001a\u00020\rH\u0016¨\u0006\u000e"}, d2 = {"okio/AsyncTimeout$sink$1", "Lokio/Sink;", "close", "", "flush", RtspHeaders.Values.TIMEOUT, "Lokio/AsyncTimeout;", "toString", "", "write", "source", "Lokio/Buffer;", "byteCount", "", "okio"}, k = 1, mv = {1, 6, 0}, xi = 48)
public final class b implements Sink {

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final /* synthetic */ AsyncTimeout f10113f;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public final /* synthetic */ Sink f10114m;

    public b(AsyncTimeout asyncTimeout, Sink sink) {
        this.f10113f = asyncTimeout;
        this.f10114m = sink;
    }

    @Override // okio.Sink, java.io.Closeable, java.lang.AutoCloseable, java.nio.channels.Channel
    public void close() throws IOException {
        AsyncTimeout asyncTimeout = this.f10113f;
        Sink sink = this.f10114m;
        asyncTimeout.h();
        try {
            sink.close();
            if (asyncTimeout.i()) {
                throw asyncTimeout.j(null);
            }
        } catch (IOException e2) {
            if (!asyncTimeout.i()) {
                throw e2;
            }
            throw asyncTimeout.j(e2);
        } finally {
            asyncTimeout.i();
        }
    }

    @Override // okio.Sink, java.io.Flushable
    public void flush() throws IOException {
        AsyncTimeout asyncTimeout = this.f10113f;
        Sink sink = this.f10114m;
        asyncTimeout.h();
        try {
            sink.flush();
            if (asyncTimeout.i()) {
                throw asyncTimeout.j(null);
            }
        } catch (IOException e2) {
            if (!asyncTimeout.i()) {
                throw e2;
            }
            throw asyncTimeout.j(e2);
        } finally {
            asyncTimeout.i();
        }
    }

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

    public String toString() {
        StringBuilder sbF = a.F("AsyncTimeout.sink(");
        sbF.append(this.f10114m);
        sbF.append(')');
        return sbF.toString();
    }

    @Override // okio.Sink
    public void write(Buffer buffer, long j2) throws IOException {
        o.f(buffer, "source");
        a0.b(buffer.f10118m, 0L, j2);
        while (true) {
            long j3 = 0;
            if (j2 <= 0) {
                return;
            }
            Segment segment = buffer.f10117f;
            while (true) {
                o.c(segment);
                if (j3 >= 65536) {
                    break;
                }
                j3 += (long) (segment.f10152c - segment.f10151b);
                if (j3 >= j2) {
                    j3 = j2;
                    break;
                }
                segment = segment.f10155f;
            }
            AsyncTimeout asyncTimeout = this.f10113f;
            Sink sink = this.f10114m;
            asyncTimeout.h();
            try {
                sink.write(buffer, j3);
                if (asyncTimeout.i()) {
                    throw asyncTimeout.j(null);
                }
                j2 -= j3;
            } catch (IOException e2) {
                if (!asyncTimeout.i()) {
                    throw e2;
                }
                throw asyncTimeout.j(e2);
            } finally {
                asyncTimeout.i();
            }
        }
    }
}
