package io.grpc.protobuf.lite; import com.google.common.base.Preconditions; import com.google.protobuf.InvalidProtocolBufferException; import io.grpc.Metadata; import io.grpc.MethodDescriptor; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.lang.ref.Reference; import o.InterfaceC16581pUl; import o.XgX; import o.ZjA; import o.cKE; /* loaded from: classes6.dex */ public final class ProtoLiteUtils { private static final int BUF_SIZE = 8192; static final int DEFAULT_MAX_MESSAGE_SIZE = 4194304; static volatile ZjA globalRegistry = ZjA.e(); public static void setExtensionRegistry(ZjA zjA) { globalRegistry = (ZjA) Preconditions.checkNotNull(zjA, "newRegistry"); } public static MethodDescriptor.Marshaller marshaller(T t) { return new MessageMarshaller(t); } public static Metadata.BinaryMarshaller metadataMarshaller(T t) { return new MetadataMarshaller(t); } /* JADX INFO: Access modifiers changed from: package-private */ public static long copy(InputStream inputStream, OutputStream outputStream) throws IOException { Preconditions.checkNotNull(inputStream, "inputStream cannot be null!"); Preconditions.checkNotNull(outputStream, "outputStream cannot be null!"); byte[] bArr = new byte[8192]; long j = 0; while (true) { int read = inputStream.read(bArr); if (read == -1) { return j; } outputStream.write(bArr, 0, read); j += read; } } private ProtoLiteUtils() { } /* loaded from: classes6.dex */ static final class MessageMarshaller implements MethodDescriptor.PrototypeMarshaller { private static final ThreadLocal> bufs = new ThreadLocal<>(); private final T defaultInstance; private final XgX parser; MessageMarshaller(T t) { this.defaultInstance = t; this.parser = (XgX) t.getParserForType(); } @Override // io.grpc.MethodDescriptor.ReflectableMarshaller public final Class getMessageClass() { return (Class) this.defaultInstance.getClass(); } @Override // io.grpc.MethodDescriptor.Marshaller public final InputStream stream(T t) { return new ProtoInputStream(t, this.parser); } /* JADX WARN: Removed duplicated region for block: B:15:0x0089 */ @Override // io.grpc.MethodDescriptor.Marshaller /* Code decompiled incorrectly, please refer to instructions dump. To view partially-correct add '--show-bad-code' argument */ public final T parse(java.io.InputStream r6) { /* r5 = this; boolean r0 = r6 instanceof io.grpc.protobuf.lite.ProtoInputStream if (r0 == 0) goto L17 r0 = r6 io.grpc.protobuf.lite.ProtoInputStream r0 = (io.grpc.protobuf.lite.ProtoInputStream) r0 o.XgX r0 = r0.parser() o.XgX r1 = r5.parser if (r0 != r1) goto L17 r0 = r6 io.grpc.protobuf.lite.ProtoInputStream r0 = (io.grpc.protobuf.lite.ProtoInputStream) r0 // Catch: java.lang.IllegalStateException -> L17 o.pUl r6 = r0.message() // Catch: java.lang.IllegalStateException -> L17 return r6 L17: boolean r0 = r6 instanceof io.grpc.KnownLength if (r0 == 0) goto L86 int r0 = r6.available() // Catch: java.io.IOException -> L7f if (r0 <= 0) goto L7a r1 = 4194304(0x400000, float:5.877472E-39) if (r0 > r1) goto L7a java.lang.ThreadLocal> r1 = io.grpc.protobuf.lite.ProtoLiteUtils.MessageMarshaller.bufs // Catch: java.io.IOException -> L7f java.lang.Object r2 = r1.get() // Catch: java.io.IOException -> L7f java.lang.ref.Reference r2 = (java.lang.ref.Reference) r2 // Catch: java.io.IOException -> L7f if (r2 == 0) goto L3a java.lang.Object r2 = r2.get() // Catch: java.io.IOException -> L7f byte[] r2 = (byte[]) r2 // Catch: java.io.IOException -> L7f if (r2 == 0) goto L3a int r3 = r2.length // Catch: java.io.IOException -> L7f if (r3 >= r0) goto L44 L3a: byte[] r2 = new byte[r0] // Catch: java.io.IOException -> L7f java.lang.ref.WeakReference r3 = new java.lang.ref.WeakReference // Catch: java.io.IOException -> L7f r3.(r2) // Catch: java.io.IOException -> L7f r1.set(r3) // Catch: java.io.IOException -> L7f L44: r1 = r0 L45: if (r1 <= 0) goto L52 int r3 = r0 - r1 int r3 = r6.read(r2, r3, r1) // Catch: java.io.IOException -> L7f r4 = -1 if (r3 == r4) goto L52 int r1 = r1 - r3 goto L45 L52: if (r1 != 0) goto L5a r1 = 0 o.cKE r0 = o.cKE.a(r2, r1, r0) // Catch: java.io.IOException -> L7f goto L87 L5a: java.lang.RuntimeException r6 = new java.lang.RuntimeException // Catch: java.io.IOException -> L7f java.lang.StringBuilder r2 = new java.lang.StringBuilder // Catch: java.io.IOException -> L7f r2.() // Catch: java.io.IOException -> L7f java.lang.String r3 = "size inaccurate: " r2.append(r3) // Catch: java.io.IOException -> L7f r2.append(r0) // Catch: java.io.IOException -> L7f java.lang.String r3 = " != " r2.append(r3) // Catch: java.io.IOException -> L7f int r0 = r0 - r1 r2.append(r0) // Catch: java.io.IOException -> L7f java.lang.String r0 = r2.toString() // Catch: java.io.IOException -> L7f r6.(r0) // Catch: java.io.IOException -> L7f throw r6 // Catch: java.io.IOException -> L7f L7a: if (r0 != 0) goto L86 T extends o.pUl r6 = r5.defaultInstance // Catch: java.io.IOException -> L7f return r6 L7f: r6 = move-exception java.lang.RuntimeException r0 = new java.lang.RuntimeException r0.(r6) throw r0 L86: r0 = 0 L87: if (r0 != 0) goto L8d o.cKE r0 = o.cKE.e(r6) L8d: int r6 = r0.d r6 = 2147483647(0x7fffffff, float:NaN) r0.d = r6 o.pUl r6 = r5.parseFrom(r0) // Catch: com.google.protobuf.InvalidProtocolBufferException -> L99 return r6 L99: r6 = move-exception io.grpc.Status r0 = io.grpc.Status.INTERNAL java.lang.String r1 = "Invalid protobuf byte sequence" io.grpc.Status r0 = r0.withDescription(r1) io.grpc.Status r6 = r0.withCause(r6) io.grpc.StatusRuntimeException r6 = r6.asRuntimeException() throw r6 */ throw new UnsupportedOperationException("Method not decompiled: io.grpc.protobuf.lite.ProtoLiteUtils.MessageMarshaller.parse(java.io.InputStream):o.pUl"); } private T parseFrom(cKE cke) throws InvalidProtocolBufferException { T d = this.parser.d(cke, ProtoLiteUtils.globalRegistry); cke.e(); return d; } @Override // io.grpc.MethodDescriptor.PrototypeMarshaller public final T getMessagePrototype() { return this.defaultInstance; } } /* loaded from: classes6.dex */ static final class MetadataMarshaller implements Metadata.BinaryMarshaller { private final T defaultInstance; MetadataMarshaller(T t) { this.defaultInstance = t; } @Override // io.grpc.Metadata.BinaryMarshaller public final byte[] toBytes(T t) { return t.toByteArray(); } @Override // io.grpc.Metadata.BinaryMarshaller public final T parseBytes(byte[] bArr) { try { return (T) this.defaultInstance.getParserForType().a(bArr, ProtoLiteUtils.globalRegistry); } catch (InvalidProtocolBufferException e) { throw new IllegalArgumentException(e); } } } }