package defpackage;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.util.concurrent.atomic.AtomicInteger;

/* renamed from: ᵬᵮ, reason: contains not printable characters */
/* loaded from: classes3.dex */
public final class C4062 implements InterfaceC3280 {

    /* renamed from: ᵢˇᵭ, reason: contains not printable characters */
    public final C2117 f15226;

    /* renamed from: ᵧᵾˀˈ, reason: contains not printable characters */
    public final OutputStream f15227;

    /* renamed from: ᵿʗᵝʽ, reason: contains not printable characters */
    public final /* synthetic */ ᵮᵥٴ f15228;

    public C4062(ᵮᵥٴ r2) {
        this.f15228 = r2;
        Socket socket = (Socket) r2.ᵢˇᵭ;
        this.f15227 = socket.getOutputStream();
        this.f15226 = new C2117(socket);
    }

    @Override // defpackage.InterfaceC3280, java.io.Closeable, java.lang.AutoCloseable
    public final void close() throws IOException {
        int i;
        OutputStream outputStream = this.f15227;
        ᵮᵥٴ r1 = this.f15228;
        C2117 c2117 = this.f15226;
        c2117.m9835();
        try {
            AtomicInteger atomicInteger = (AtomicInteger) r1.ᵿʗᵝʽ;
            Socket socket = (Socket) r1.ᵢˇᵭ;
            while (true) {
                int i2 = atomicInteger.get();
                if ((i2 & 1) != 0) {
                    i = 0;
                    break;
                }
                int i3 = i2 | 1;
                if (atomicInteger.compareAndSet(i2, i3)) {
                    i = i3;
                    break;
                }
            }
            if (i != 0) {
                if (i != 3) {
                    if (!socket.isClosed() && !socket.isOutputShutdown()) {
                        outputStream.flush();
                        try {
                            socket.shutdownOutput();
                        } catch (UnsupportedOperationException unused) {
                            outputStream.close();
                        }
                    }
                    return;
                }
                socket.close();
                if (c2117.m9834()) {
                    throw c2117.mo2865(null);
                }
            }
        } catch (IOException e) {
            if (!c2117.m9834()) {
                throw e;
            }
            throw c2117.mo2865(e);
        } finally {
            c2117.m9834();
        }
    }

    @Override // defpackage.InterfaceC3280, java.io.Flushable
    public final void flush() throws IOException {
        C2117 c2117 = this.f15226;
        c2117.m9835();
        try {
            this.f15227.flush();
            if (c2117.m9834()) {
                throw c2117.mo2865(null);
            }
        } catch (IOException e) {
            if (!c2117.m9834()) {
                throw e;
            }
            throw c2117.mo2865(e);
        } finally {
            c2117.m9834();
        }
    }

    public final String toString() {
        return "sink(" + ((Socket) this.f15228.ᵢˇᵭ) + ')';
    }

    @Override // defpackage.InterfaceC3280
    /* renamed from: ᐧﾞ */
    public final void mo2377(C0963 c0963, long j) throws IOException {
        ᵩʛʽ.ᵸˑ(c0963.f4314, 0L, j);
        while (j > 0) {
            C2117 c2117 = this.f15226;
            c2117.mo8742();
            C3514 c3514 = c0963.f4315;
            int iMin = (int) Math.min(j, c3514.f13188 - c3514.f13190);
            c2117.m9835();
            try {
                try {
                    this.f15227.write(c3514.f13189, c3514.f13190, iMin);
                    if (c2117.m9834()) {
                        throw c2117.mo2865(null);
                    }
                    int i = c3514.f13190 + iMin;
                    c3514.f13190 = i;
                    long j2 = iMin;
                    j -= j2;
                    c0963.f4314 -= j2;
                    if (i == c3514.f13188) {
                        c0963.f4315 = c3514.m8039();
                        AbstractC3467.m7895(c3514);
                    }
                } catch (IOException e) {
                    if (!c2117.m9834()) {
                        throw e;
                    }
                    throw c2117.mo2865(e);
                }
            } catch (Throwable th) {
                c2117.m9834();
                throw th;
            }
        }
    }

    @Override // defpackage.InterfaceC3280
    /* renamed from: ᵝᵵᵺ */
    public final C3948 mo2378() {
        return this.f15226;
    }
}
