package o;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.Charset;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/* compiled from: r8-map-id-3b7cc9102578c509342fa2f0fabed2bfc72239cd258d579fc679bfcbc198b048 */
/* renamed from: o.vZ, reason: case insensitive filesystem */
/* loaded from: classes.dex */
public final class C2720vZ implements InterfaceC1002dT {
    public static final Charset f = Charset.forName("UTF-8");
    public static final C0412Px g = new C0412Px("key", AbstractC2462sn.p(AbstractC2462sn.o(InterfaceC2435sZ.class, new O9(1))));
    public static final C0412Px h = new C0412Px("value", AbstractC2462sn.p(AbstractC2462sn.o(InterfaceC2435sZ.class, new O9(2))));
    public static final C1755lJ i = new C1755lJ(1);
    public OutputStream a;
    public final HashMap b;
    public final HashMap c;
    public final InterfaceC0906cT d;
    public final C2815wZ e = new C2815wZ(this);

    public C2720vZ(ByteArrayOutputStream byteArrayOutputStream, HashMap map, HashMap map2, InterfaceC0906cT interfaceC0906cT) {
        this.a = byteArrayOutputStream;
        this.b = map;
        this.c = map2;
        this.d = interfaceC0906cT;
    }

    public static int j(C0412Px c0412Px) {
        InterfaceC2435sZ interfaceC2435sZ = (InterfaceC2435sZ) ((Annotation) c0412Px.b.get(InterfaceC2435sZ.class));
        if (interfaceC2435sZ != null) {
            return ((O9) interfaceC2435sZ).a;
        }
        throw new C0644Yv("Field has no @Protobuf config");
    }

    @Override // o.InterfaceC1002dT
    public final InterfaceC1002dT a(C0412Px c0412Px, boolean z) {
        f(c0412Px, z ? 1 : 0, true);
        return this;
    }

    @Override // o.InterfaceC1002dT
    public final InterfaceC1002dT b(C0412Px c0412Px, int i2) {
        f(c0412Px, i2, true);
        return this;
    }

    @Override // o.InterfaceC1002dT
    public final InterfaceC1002dT c(C0412Px c0412Px, long j) throws IOException {
        if (j == 0) {
            return this;
        }
        InterfaceC2435sZ interfaceC2435sZ = (InterfaceC2435sZ) ((Annotation) c0412Px.b.get(InterfaceC2435sZ.class));
        if (interfaceC2435sZ == null) {
            throw new C0644Yv("Field has no @Protobuf config");
        }
        k(((O9) interfaceC2435sZ).a << 3);
        l(j);
        return this;
    }

    @Override // o.InterfaceC1002dT
    public final InterfaceC1002dT d(C0412Px c0412Px, double d) throws IOException {
        e(c0412Px, d, true);
        return this;
    }

    public final void e(C0412Px c0412Px, double d, boolean z) throws IOException {
        if (z && d == 0.0d) {
            return;
        }
        k((j(c0412Px) << 3) | 1);
        this.a.write(ByteBuffer.allocate(8).order(ByteOrder.LITTLE_ENDIAN).putDouble(d).array());
    }

    public final void f(C0412Px c0412Px, int i2, boolean z) {
        if (z && i2 == 0) {
            return;
        }
        InterfaceC2435sZ interfaceC2435sZ = (InterfaceC2435sZ) ((Annotation) c0412Px.b.get(InterfaceC2435sZ.class));
        if (interfaceC2435sZ == null) {
            throw new C0644Yv("Field has no @Protobuf config");
        }
        k(((O9) interfaceC2435sZ).a << 3);
        k(i2);
    }

    @Override // o.InterfaceC1002dT
    public final InterfaceC1002dT g(C0412Px c0412Px, Object obj) {
        h(c0412Px, obj, true);
        return this;
    }

    public final void h(C0412Px c0412Px, Object obj, boolean z) {
        if (obj == null) {
            return;
        }
        if (obj instanceof CharSequence) {
            CharSequence charSequence = (CharSequence) obj;
            if (z && charSequence.length() == 0) {
                return;
            }
            k((j(c0412Px) << 3) | 2);
            byte[] bytes = charSequence.toString().getBytes(f);
            k(bytes.length);
            this.a.write(bytes);
            return;
        }
        if (obj instanceof Collection) {
            Iterator it = ((Collection) obj).iterator();
            while (it.hasNext()) {
                h(c0412Px, it.next(), false);
            }
            return;
        }
        if (obj instanceof Map) {
            Iterator it2 = ((Map) obj).entrySet().iterator();
            while (it2.hasNext()) {
                i(i, c0412Px, (Map.Entry) it2.next(), false);
            }
            return;
        }
        if (obj instanceof Double) {
            e(c0412Px, ((Double) obj).doubleValue(), z);
            return;
        }
        if (obj instanceof Float) {
            float fFloatValue = ((Float) obj).floatValue();
            if (z && fFloatValue == 0.0f) {
                return;
            }
            k((j(c0412Px) << 3) | 5);
            this.a.write(ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putFloat(fFloatValue).array());
            return;
        }
        if (obj instanceof Number) {
            long jLongValue = ((Number) obj).longValue();
            if (z && jLongValue == 0) {
                return;
            }
            InterfaceC2435sZ interfaceC2435sZ = (InterfaceC2435sZ) ((Annotation) c0412Px.b.get(InterfaceC2435sZ.class));
            if (interfaceC2435sZ == null) {
                throw new C0644Yv("Field has no @Protobuf config");
            }
            k(((O9) interfaceC2435sZ).a << 3);
            l(jLongValue);
            return;
        }
        if (obj instanceof Boolean) {
            f(c0412Px, ((Boolean) obj).booleanValue() ? 1 : 0, z);
            return;
        }
        if (obj instanceof byte[]) {
            byte[] bArr = (byte[]) obj;
            if (z && bArr.length == 0) {
                return;
            }
            k((j(c0412Px) << 3) | 2);
            k(bArr.length);
            this.a.write(bArr);
            return;
        }
        InterfaceC0906cT interfaceC0906cT = (InterfaceC0906cT) this.b.get(obj.getClass());
        if (interfaceC0906cT != null) {
            i(interfaceC0906cT, c0412Px, obj, z);
            return;
        }
        InterfaceC2928xi0 interfaceC2928xi0 = (InterfaceC2928xi0) this.c.get(obj.getClass());
        if (interfaceC2928xi0 != null) {
            C2815wZ c2815wZ = this.e;
            c2815wZ.a = false;
            c2815wZ.c = c0412Px;
            c2815wZ.b = z;
            interfaceC2928xi0.a(obj, c2815wZ);
            return;
        }
        if (obj instanceof InterfaceC2246qZ) {
            f(c0412Px, ((InterfaceC2246qZ) obj).a(), true);
        } else if (obj instanceof Enum) {
            f(c0412Px, ((Enum) obj).ordinal(), true);
        } else {
            i(this.d, c0412Px, obj, z);
        }
    }

    public final void i(InterfaceC0906cT interfaceC0906cT, C0412Px c0412Px, Object obj, boolean z) throws IOException {
        PK pk = new PK();
        pk.l = 0L;
        try {
            OutputStream outputStream = this.a;
            this.a = pk;
            try {
                interfaceC0906cT.a(obj, this);
                this.a = outputStream;
                long j = pk.l;
                pk.close();
                if (z && j == 0) {
                    return;
                }
                k((j(c0412Px) << 3) | 2);
                l(j);
                interfaceC0906cT.a(obj, this);
            } catch (Throwable th) {
                this.a = outputStream;
                throw th;
            }
        } catch (Throwable th2) {
            try {
                pk.close();
            } catch (Throwable th3) {
                th2.addSuppressed(th3);
            }
            throw th2;
        }
    }

    public final void k(int i2) throws IOException {
        while ((i2 & (-128)) != 0) {
            this.a.write((i2 & 127) | 128);
            i2 >>>= 7;
        }
        this.a.write(i2 & 127);
    }

    public final void l(long j) throws IOException {
        while (((-128) & j) != 0) {
            this.a.write((((int) j) & 127) | 128);
            j >>>= 7;
        }
        this.a.write(((int) j) & 127);
    }
}
