package p5;

import java.io.IOException;
import java.io.OutputStream;

/* JADX INFO: renamed from: p5.d, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes3.dex */
public final class C2734d implements N {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final /* synthetic */ int f23083a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final Object f23084b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final Object f23085c;

    public /* synthetic */ C2734d(int i6, Object obj, Object obj2) {
        this.f23083a = i6;
        this.f23084b = obj;
        this.f23085c = obj2;
    }

    @Override // p5.N, java.io.Closeable, java.lang.AutoCloseable
    public final void close() throws IOException {
        switch (this.f23083a) {
            case 0:
                C2734d c2734d = (C2734d) this.f23085c;
                O o6 = (O) this.f23084b;
                o6.h();
                try {
                    c2734d.close();
                    if (o6.i()) {
                        throw o6.k(null);
                    }
                    return;
                } catch (IOException e6) {
                    if (!o6.i()) {
                        throw e6;
                    }
                    throw o6.k(e6);
                } finally {
                    o6.i();
                }
            default:
                ((OutputStream) this.f23084b).close();
                return;
        }
    }

    @Override // p5.N, java.io.Flushable
    public final void flush() throws IOException {
        switch (this.f23083a) {
            case 0:
                C2734d c2734d = (C2734d) this.f23085c;
                O o6 = (O) this.f23084b;
                o6.h();
                try {
                    c2734d.flush();
                    if (o6.i()) {
                        throw o6.k(null);
                    }
                    return;
                } catch (IOException e6) {
                    if (!o6.i()) {
                        throw e6;
                    }
                    throw o6.k(e6);
                } finally {
                    o6.i();
                }
            default:
                ((OutputStream) this.f23084b).flush();
                return;
        }
    }

    @Override // p5.N
    public final S timeout() {
        switch (this.f23083a) {
            case 0:
                return (O) this.f23084b;
            default:
                return (S) this.f23085c;
        }
    }

    public final String toString() {
        switch (this.f23083a) {
            case 0:
                return "AsyncTimeout.sink(" + ((C2734d) this.f23085c) + ')';
            default:
                return "sink(" + ((OutputStream) this.f23084b) + ')';
        }
    }

    @Override // p5.N
    public final void write(C2740j source, long j4) throws IOException {
        switch (this.f23083a) {
            case 0:
                kotlin.jvm.internal.o.h(source, "source");
                AbstractC2732b.e(source.f23100b, 0L, j4);
                long j6 = j4;
                while (true) {
                    long j7 = 0;
                    if (j6 <= 0) {
                        return;
                    }
                    K k6 = source.f23099a;
                    kotlin.jvm.internal.o.e(k6);
                    while (true) {
                        if (j7 < 65536) {
                            j7 += (long) (k6.f23063c - k6.f23062b);
                            if (j7 >= j6) {
                                j7 = j6;
                            } else {
                                k6 = k6.f23066f;
                                kotlin.jvm.internal.o.e(k6);
                            }
                        }
                    }
                    C2734d c2734d = (C2734d) this.f23085c;
                    O o6 = (O) this.f23084b;
                    o6.h();
                    try {
                        try {
                            c2734d.write(source, j7);
                            if (o6.i()) {
                                throw o6.k(null);
                            }
                            j6 -= j7;
                        } catch (IOException e6) {
                            if (!o6.i()) {
                                throw e6;
                            }
                            throw o6.k(e6);
                        }
                    } catch (Throwable th) {
                        o6.i();
                        throw th;
                    }
                }
                break;
            default:
                kotlin.jvm.internal.o.h(source, "source");
                AbstractC2732b.e(source.f23100b, 0L, j4);
                while (j4 > 0) {
                    ((S) this.f23085c).f();
                    K k7 = source.f23099a;
                    kotlin.jvm.internal.o.e(k7);
                    int iMin = (int) Math.min(j4, k7.f23063c - k7.f23062b);
                    ((OutputStream) this.f23084b).write(k7.f23061a, k7.f23062b, iMin);
                    int i6 = k7.f23062b + iMin;
                    k7.f23062b = i6;
                    long j8 = iMin;
                    j4 -= j8;
                    source.f23100b -= j8;
                    if (i6 == k7.f23063c) {
                        source.f23099a = k7.a();
                        L.a(k7);
                    }
                }
                return;
        }
    }
}
