package k1;

import java.io.IOException;
import java.io.OutputStream;
import l1.C0622e;

/* JADX INFO: loaded from: classes.dex */
public class Z extends OutputStream {

    /* JADX INFO: renamed from: H, reason: collision with root package name */
    private int f4755H;

    /* JADX INFO: renamed from: I, reason: collision with root package name */
    private int f4756I;

    /* JADX INFO: renamed from: L, reason: collision with root package name */
    private long f4757L;

    /* JADX INFO: renamed from: M, reason: collision with root package name */
    private byte[] f4758M;

    /* JADX INFO: renamed from: Q, reason: collision with root package name */
    private C0580Q f4759Q;

    /* JADX INFO: renamed from: Z, reason: collision with root package name */
    private C0581S f4760Z;

    /* JADX INFO: renamed from: a0, reason: collision with root package name */
    private C0579P f4761a0;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private W f4762b;

    /* JADX INFO: renamed from: b0, reason: collision with root package name */
    private C0582T f4763b0;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    private boolean f4764g;

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    private boolean f4765r;

    /* JADX INFO: renamed from: x, reason: collision with root package name */
    private int f4766x;

    /* JADX INFO: renamed from: y, reason: collision with root package name */
    private int f4767y;

    public Z(W w2) {
        this(w2, false);
    }

    void a() {
        if (this.f4762b.F()) {
            return;
        }
        this.f4762b.N(this.f4766x, this.f4755H | 2, 128, 0);
        if (this.f4764g) {
            this.f4757L = this.f4762b.I();
        }
    }

    public void b(byte[] bArr, int i2, int i3, int i4) throws IOException {
        if (i3 <= 0) {
            return;
        }
        if (this.f4758M == null) {
            throw new IOException("Bad file descriptor");
        }
        a();
        C0586d c0586d = W.f4726p0;
        if (C0622e.f5384g >= 4) {
            W.f4723m0.println("write: fid=" + this.f4762b.f4738e0 + ",off=" + i2 + ",len=" + i3);
        }
        do {
            int i5 = this.f4762b.y() == 1 ? this.f4767y : this.f4756I;
            if (i3 <= i5) {
                i5 = i3;
            }
            if (this.f4765r) {
                this.f4759Q.H(this.f4762b.f4738e0, this.f4757L, i3 - i5, bArr, i2, i5);
                if ((1 & i4) != 0) {
                    this.f4759Q.H(this.f4762b.f4738e0, this.f4757L, i3, bArr, i2, i5);
                    this.f4759Q.f4679B0 = 8;
                } else {
                    this.f4759Q.f4679B0 = 0;
                }
                this.f4762b.T(this.f4759Q, this.f4760Z);
                long j2 = this.f4757L;
                long j3 = this.f4760Z.f4687t0;
                this.f4757L = j2 + j3;
                i3 = (int) (((long) i3) - j3);
                i2 = (int) (((long) i2) + j3);
            } else {
                this.f4761a0.E(this.f4762b.f4738e0, this.f4757L, i3 - i5, bArr, i2, i5);
                long j4 = this.f4757L;
                C0582T c0582t = this.f4763b0;
                long j5 = c0582t.f4688q0;
                this.f4757L = j4 + j5;
                i3 = (int) (((long) i3) - j5);
                i2 = (int) (((long) i2) + j5);
                this.f4762b.T(this.f4761a0, c0582t);
            }
        } while (i3 > 0);
    }

    @Override // java.io.OutputStream, java.io.Closeable, java.lang.AutoCloseable
    public void close() {
        this.f4762b.d();
        this.f4758M = null;
    }

    public boolean isOpen() {
        return this.f4762b.F();
    }

    @Override // java.io.OutputStream
    public void write(int i2) throws IOException {
        byte[] bArr = this.f4758M;
        bArr[0] = (byte) i2;
        write(bArr, 0, 1);
    }

    public Z(W w2, boolean z2) {
        this(w2, z2, z2 ? 22 : 82);
    }

    Z(W w2, boolean z2, int i2) throws V {
        this.f4758M = new byte[1];
        this.f4762b = w2;
        this.f4764g = z2;
        this.f4766x = i2;
        this.f4755H = (i2 >>> 16) & 65535;
        if (z2) {
            try {
                this.f4757L = w2.I();
            } catch (C0602t e2) {
                throw e2;
            } catch (V unused) {
                this.f4757L = 0L;
            }
        }
        if ((w2 instanceof b0) && w2.f4737d0.startsWith("\\pipe\\")) {
            w2.f4737d0 = w2.f4737d0.substring(5);
            w2.T(new v0("\\pipe" + w2.f4737d0), new w0());
        }
        w2.N(i2, this.f4755H | 2, 128, 0);
        this.f4766x &= -81;
        e0 e0Var = w2.f4736c0.f4870f.f4790h;
        int i3 = e0Var.f4838n0 - 70;
        this.f4756I = i3;
        if ((e0Var.f4835k0.f4847d & 32768) == 32768) {
            this.f4767y = Math.min(U.f4709n - 70, 65465);
        } else {
            this.f4767y = i3;
        }
        boolean zU = w2.f4736c0.f4870f.f4790h.u(16);
        this.f4765r = zU;
        if (zU) {
            this.f4759Q = new C0580Q();
            this.f4760Z = new C0581S();
        } else {
            this.f4761a0 = new C0579P();
            this.f4763b0 = new C0582T();
        }
    }

    @Override // java.io.OutputStream
    public void write(byte[] bArr) throws IOException {
        write(bArr, 0, bArr.length);
    }

    @Override // java.io.OutputStream
    public void write(byte[] bArr, int i2, int i3) throws IOException {
        if (!this.f4762b.F()) {
            W w2 = this.f4762b;
            if (w2 instanceof b0) {
                w2.T(new v0("\\pipe" + this.f4762b.f4737d0), new w0());
            }
        }
        b(bArr, i2, i3, 0);
    }
}
