- 28,932 files - Full Java source code - Smali files - Resources Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
312 lines
12 KiB
Java
312 lines
12 KiB
Java
package com.google.firebase.encoders.proto;
|
|
|
|
import androidx.work.WorkInfo;
|
|
import com.google.firebase.encoders.EncodingException;
|
|
import com.google.firebase.encoders.FieldDescriptor;
|
|
import com.google.firebase.encoders.ObjectEncoder;
|
|
import com.google.firebase.encoders.ObjectEncoderContext;
|
|
import com.google.firebase.encoders.ValueEncoder;
|
|
import com.google.firebase.encoders.proto.Protobuf;
|
|
import java.io.OutputStream;
|
|
import java.nio.ByteBuffer;
|
|
import java.nio.ByteOrder;
|
|
import java.nio.charset.Charset;
|
|
import java.util.Collection;
|
|
import java.util.Iterator;
|
|
import java.util.Map;
|
|
|
|
/* loaded from: classes3.dex */
|
|
public final class ProtobufDataEncoderContext implements ObjectEncoderContext {
|
|
public final ObjectEncoder fallbackEncoder;
|
|
public final Map objectEncoders;
|
|
public OutputStream output;
|
|
public final ProtobufValueEncoderContext valueEncoderContext = new ProtobufValueEncoderContext(this);
|
|
public final Map valueEncoders;
|
|
public static final Charset UTF_8 = Charset.forName("UTF-8");
|
|
public static final FieldDescriptor MAP_KEY_DESC = FieldDescriptor.builder("key").withProperty(AtProtobuf.builder().tag(1).build()).build();
|
|
public static final FieldDescriptor MAP_VALUE_DESC = FieldDescriptor.builder("value").withProperty(AtProtobuf.builder().tag(2).build()).build();
|
|
public static final ObjectEncoder DEFAULT_MAP_ENCODER = new ObjectEncoder() { // from class: com.google.firebase.encoders.proto.ProtobufDataEncoderContext$$ExternalSyntheticLambda0
|
|
@Override // com.google.firebase.encoders.ObjectEncoder
|
|
public final void encode(Object obj, Object obj2) {
|
|
ProtobufDataEncoderContext.lambda$static$0((Map.Entry) obj, (ObjectEncoderContext) obj2);
|
|
}
|
|
};
|
|
|
|
public static /* synthetic */ void lambda$static$0(Map.Entry entry, ObjectEncoderContext objectEncoderContext) {
|
|
objectEncoderContext.add(MAP_KEY_DESC, entry.getKey());
|
|
objectEncoderContext.add(MAP_VALUE_DESC, entry.getValue());
|
|
}
|
|
|
|
public ProtobufDataEncoderContext(OutputStream outputStream, Map map, Map map2, ObjectEncoder objectEncoder) {
|
|
this.output = outputStream;
|
|
this.objectEncoders = map;
|
|
this.valueEncoders = map2;
|
|
this.fallbackEncoder = objectEncoder;
|
|
}
|
|
|
|
@Override // com.google.firebase.encoders.ObjectEncoderContext
|
|
public ObjectEncoderContext add(FieldDescriptor fieldDescriptor, Object obj) {
|
|
return add(fieldDescriptor, obj, true);
|
|
}
|
|
|
|
public ObjectEncoderContext add(FieldDescriptor fieldDescriptor, Object obj, boolean z) {
|
|
if (obj == null) {
|
|
return this;
|
|
}
|
|
if (obj instanceof CharSequence) {
|
|
CharSequence charSequence = (CharSequence) obj;
|
|
if (z && charSequence.length() == 0) {
|
|
return this;
|
|
}
|
|
writeVarInt32((getTag(fieldDescriptor) << 3) | 2);
|
|
byte[] bytes = charSequence.toString().getBytes(UTF_8);
|
|
writeVarInt32(bytes.length);
|
|
this.output.write(bytes);
|
|
return this;
|
|
}
|
|
if (obj instanceof Collection) {
|
|
Iterator it = ((Collection) obj).iterator();
|
|
while (it.hasNext()) {
|
|
add(fieldDescriptor, it.next(), false);
|
|
}
|
|
return this;
|
|
}
|
|
if (obj instanceof Map) {
|
|
Iterator it2 = ((Map) obj).entrySet().iterator();
|
|
while (it2.hasNext()) {
|
|
doEncode(DEFAULT_MAP_ENCODER, fieldDescriptor, it2.next(), false);
|
|
}
|
|
return this;
|
|
}
|
|
if (obj instanceof Double) {
|
|
return add(fieldDescriptor, ((Double) obj).doubleValue(), z);
|
|
}
|
|
if (obj instanceof Float) {
|
|
return add(fieldDescriptor, ((Float) obj).floatValue(), z);
|
|
}
|
|
if (obj instanceof Number) {
|
|
return add(fieldDescriptor, ((Number) obj).longValue(), z);
|
|
}
|
|
if (obj instanceof Boolean) {
|
|
return add(fieldDescriptor, ((Boolean) obj).booleanValue(), z);
|
|
}
|
|
if (obj instanceof byte[]) {
|
|
byte[] bArr = (byte[]) obj;
|
|
if (z && bArr.length == 0) {
|
|
return this;
|
|
}
|
|
writeVarInt32((getTag(fieldDescriptor) << 3) | 2);
|
|
writeVarInt32(bArr.length);
|
|
this.output.write(bArr);
|
|
return this;
|
|
}
|
|
ObjectEncoder objectEncoder = (ObjectEncoder) this.objectEncoders.get(obj.getClass());
|
|
if (objectEncoder != null) {
|
|
return doEncode(objectEncoder, fieldDescriptor, obj, z);
|
|
}
|
|
ValueEncoder valueEncoder = (ValueEncoder) this.valueEncoders.get(obj.getClass());
|
|
if (valueEncoder != null) {
|
|
return doEncode(valueEncoder, fieldDescriptor, obj, z);
|
|
}
|
|
if (obj instanceof ProtoEnum) {
|
|
return add(fieldDescriptor, ((ProtoEnum) obj).getNumber());
|
|
}
|
|
if (obj instanceof Enum) {
|
|
return add(fieldDescriptor, ((Enum) obj).ordinal());
|
|
}
|
|
return doEncode(this.fallbackEncoder, fieldDescriptor, obj, z);
|
|
}
|
|
|
|
@Override // com.google.firebase.encoders.ObjectEncoderContext
|
|
public ObjectEncoderContext add(FieldDescriptor fieldDescriptor, double d) {
|
|
return add(fieldDescriptor, d, true);
|
|
}
|
|
|
|
public ObjectEncoderContext add(FieldDescriptor fieldDescriptor, double d, boolean z) {
|
|
if (z && d == 0.0d) {
|
|
return this;
|
|
}
|
|
writeVarInt32((getTag(fieldDescriptor) << 3) | 1);
|
|
this.output.write(allocateBuffer(8).putDouble(d).array());
|
|
return this;
|
|
}
|
|
|
|
public ObjectEncoderContext add(FieldDescriptor fieldDescriptor, float f, boolean z) {
|
|
if (z && f == 0.0f) {
|
|
return this;
|
|
}
|
|
writeVarInt32((getTag(fieldDescriptor) << 3) | 5);
|
|
this.output.write(allocateBuffer(4).putFloat(f).array());
|
|
return this;
|
|
}
|
|
|
|
@Override // com.google.firebase.encoders.ObjectEncoderContext
|
|
public ProtobufDataEncoderContext add(FieldDescriptor fieldDescriptor, int i) {
|
|
return add(fieldDescriptor, i, true);
|
|
}
|
|
|
|
/* renamed from: com.google.firebase.encoders.proto.ProtobufDataEncoderContext$1, reason: invalid class name */
|
|
public static /* synthetic */ class AnonymousClass1 {
|
|
public static final /* synthetic */ int[] $SwitchMap$com$google$firebase$encoders$proto$Protobuf$IntEncoding;
|
|
|
|
static {
|
|
int[] iArr = new int[Protobuf.IntEncoding.values().length];
|
|
$SwitchMap$com$google$firebase$encoders$proto$Protobuf$IntEncoding = iArr;
|
|
try {
|
|
iArr[Protobuf.IntEncoding.DEFAULT.ordinal()] = 1;
|
|
} catch (NoSuchFieldError unused) {
|
|
}
|
|
try {
|
|
$SwitchMap$com$google$firebase$encoders$proto$Protobuf$IntEncoding[Protobuf.IntEncoding.SIGNED.ordinal()] = 2;
|
|
} catch (NoSuchFieldError unused2) {
|
|
}
|
|
try {
|
|
$SwitchMap$com$google$firebase$encoders$proto$Protobuf$IntEncoding[Protobuf.IntEncoding.FIXED.ordinal()] = 3;
|
|
} catch (NoSuchFieldError unused3) {
|
|
}
|
|
}
|
|
}
|
|
|
|
public ProtobufDataEncoderContext add(FieldDescriptor fieldDescriptor, int i, boolean z) {
|
|
if (z && i == 0) {
|
|
return this;
|
|
}
|
|
Protobuf protobuf = getProtobuf(fieldDescriptor);
|
|
int i2 = AnonymousClass1.$SwitchMap$com$google$firebase$encoders$proto$Protobuf$IntEncoding[protobuf.intEncoding().ordinal()];
|
|
if (i2 == 1) {
|
|
writeVarInt32(protobuf.tag() << 3);
|
|
writeVarInt32(i);
|
|
} else if (i2 == 2) {
|
|
writeVarInt32(protobuf.tag() << 3);
|
|
writeVarInt32((i << 1) ^ (i >> 31));
|
|
} else if (i2 == 3) {
|
|
writeVarInt32((protobuf.tag() << 3) | 5);
|
|
this.output.write(allocateBuffer(4).putInt(i).array());
|
|
}
|
|
return this;
|
|
}
|
|
|
|
@Override // com.google.firebase.encoders.ObjectEncoderContext
|
|
public ProtobufDataEncoderContext add(FieldDescriptor fieldDescriptor, long j) {
|
|
return add(fieldDescriptor, j, true);
|
|
}
|
|
|
|
public ProtobufDataEncoderContext add(FieldDescriptor fieldDescriptor, long j, boolean z) {
|
|
if (z && j == 0) {
|
|
return this;
|
|
}
|
|
Protobuf protobuf = getProtobuf(fieldDescriptor);
|
|
int i = AnonymousClass1.$SwitchMap$com$google$firebase$encoders$proto$Protobuf$IntEncoding[protobuf.intEncoding().ordinal()];
|
|
if (i == 1) {
|
|
writeVarInt32(protobuf.tag() << 3);
|
|
writeVarInt64(j);
|
|
} else if (i == 2) {
|
|
writeVarInt32(protobuf.tag() << 3);
|
|
writeVarInt64((j >> 63) ^ (j << 1));
|
|
} else if (i == 3) {
|
|
writeVarInt32((protobuf.tag() << 3) | 1);
|
|
this.output.write(allocateBuffer(8).putLong(j).array());
|
|
}
|
|
return this;
|
|
}
|
|
|
|
@Override // com.google.firebase.encoders.ObjectEncoderContext
|
|
public ProtobufDataEncoderContext add(FieldDescriptor fieldDescriptor, boolean z) {
|
|
return add(fieldDescriptor, z, true);
|
|
}
|
|
|
|
public ProtobufDataEncoderContext add(FieldDescriptor fieldDescriptor, boolean z, boolean z2) {
|
|
return add(fieldDescriptor, z ? 1 : 0, z2);
|
|
}
|
|
|
|
public ProtobufDataEncoderContext encode(Object obj) {
|
|
if (obj == null) {
|
|
return this;
|
|
}
|
|
ObjectEncoder objectEncoder = (ObjectEncoder) this.objectEncoders.get(obj.getClass());
|
|
if (objectEncoder != null) {
|
|
objectEncoder.encode(obj, this);
|
|
return this;
|
|
}
|
|
throw new EncodingException("No encoder for " + obj.getClass());
|
|
}
|
|
|
|
public final ProtobufDataEncoderContext doEncode(ObjectEncoder objectEncoder, FieldDescriptor fieldDescriptor, Object obj, boolean z) {
|
|
long determineSize = determineSize(objectEncoder, obj);
|
|
if (z && determineSize == 0) {
|
|
return this;
|
|
}
|
|
writeVarInt32((getTag(fieldDescriptor) << 3) | 2);
|
|
writeVarInt64(determineSize);
|
|
objectEncoder.encode(obj, this);
|
|
return this;
|
|
}
|
|
|
|
public final long determineSize(ObjectEncoder objectEncoder, Object obj) {
|
|
LengthCountingOutputStream lengthCountingOutputStream = new LengthCountingOutputStream();
|
|
try {
|
|
OutputStream outputStream = this.output;
|
|
this.output = lengthCountingOutputStream;
|
|
try {
|
|
objectEncoder.encode(obj, this);
|
|
this.output = outputStream;
|
|
long length = lengthCountingOutputStream.getLength();
|
|
lengthCountingOutputStream.close();
|
|
return length;
|
|
} catch (Throwable th) {
|
|
this.output = outputStream;
|
|
throw th;
|
|
}
|
|
} catch (Throwable th2) {
|
|
try {
|
|
lengthCountingOutputStream.close();
|
|
} catch (Throwable th3) {
|
|
th2.addSuppressed(th3);
|
|
}
|
|
throw th2;
|
|
}
|
|
}
|
|
|
|
public final ProtobufDataEncoderContext doEncode(ValueEncoder valueEncoder, FieldDescriptor fieldDescriptor, Object obj, boolean z) {
|
|
this.valueEncoderContext.resetContext(fieldDescriptor, z);
|
|
valueEncoder.encode(obj, this.valueEncoderContext);
|
|
return this;
|
|
}
|
|
|
|
public static ByteBuffer allocateBuffer(int i) {
|
|
return ByteBuffer.allocate(i).order(ByteOrder.LITTLE_ENDIAN);
|
|
}
|
|
|
|
public static int getTag(FieldDescriptor fieldDescriptor) {
|
|
Protobuf protobuf = (Protobuf) fieldDescriptor.getProperty(Protobuf.class);
|
|
if (protobuf == null) {
|
|
throw new EncodingException("Field has no @Protobuf config");
|
|
}
|
|
return protobuf.tag();
|
|
}
|
|
|
|
public static Protobuf getProtobuf(FieldDescriptor fieldDescriptor) {
|
|
Protobuf protobuf = (Protobuf) fieldDescriptor.getProperty(Protobuf.class);
|
|
if (protobuf != null) {
|
|
return protobuf;
|
|
}
|
|
throw new EncodingException("Field has no @Protobuf config");
|
|
}
|
|
|
|
public final void writeVarInt32(int i) {
|
|
while ((i & WorkInfo.STOP_REASON_FOREGROUND_SERVICE_TIMEOUT) != 0) {
|
|
this.output.write((i & 127) | 128);
|
|
i >>>= 7;
|
|
}
|
|
this.output.write(i & 127);
|
|
}
|
|
|
|
public final void writeVarInt64(long j) {
|
|
while (((-128) & j) != 0) {
|
|
this.output.write((((int) j) & 127) | 128);
|
|
j >>>= 7;
|
|
}
|
|
this.output.write(((int) j) & 127);
|
|
}
|
|
}
|