package io.grpc.protobuf.lite; import com.google.protobuf.CodedOutputStream; import io.grpc.Drainable; import io.grpc.KnownLength; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import o.InterfaceC16581pUl; import o.XgX; /* loaded from: classes6.dex */ final class ProtoInputStream extends InputStream implements Drainable, KnownLength { private InterfaceC16581pUl message; private final XgX parser; private ByteArrayInputStream partial; /* JADX INFO: Access modifiers changed from: package-private */ public ProtoInputStream(InterfaceC16581pUl interfaceC16581pUl, XgX xgX) { this.message = interfaceC16581pUl; this.parser = xgX; } @Override // io.grpc.Drainable public final int drainTo(OutputStream outputStream) throws IOException { InterfaceC16581pUl interfaceC16581pUl = this.message; if (interfaceC16581pUl != null) { int serializedSize = interfaceC16581pUl.getSerializedSize(); this.message.writeTo(outputStream); this.message = null; return serializedSize; } ByteArrayInputStream byteArrayInputStream = this.partial; if (byteArrayInputStream == null) { return 0; } int copy = (int) ProtoLiteUtils.copy(byteArrayInputStream, outputStream); this.partial = null; return copy; } @Override // java.io.InputStream public final int read() { if (this.message != null) { this.partial = new ByteArrayInputStream(this.message.toByteArray()); this.message = null; } ByteArrayInputStream byteArrayInputStream = this.partial; if (byteArrayInputStream != null) { return byteArrayInputStream.read(); } return -1; } @Override // java.io.InputStream public final int read(byte[] bArr, int i, int i2) throws IOException { InterfaceC16581pUl interfaceC16581pUl = this.message; if (interfaceC16581pUl != null) { int serializedSize = interfaceC16581pUl.getSerializedSize(); if (serializedSize == 0) { this.message = null; this.partial = null; return -1; } if (i2 >= serializedSize) { CodedOutputStream c = CodedOutputStream.c(bArr, i, serializedSize); this.message.writeTo(c); c.f(); if (c.i() == 0) { this.message = null; this.partial = null; return serializedSize; } throw new IllegalStateException("Did not write as much data as expected."); } this.partial = new ByteArrayInputStream(this.message.toByteArray()); this.message = null; } ByteArrayInputStream byteArrayInputStream = this.partial; if (byteArrayInputStream != null) { return byteArrayInputStream.read(bArr, i, i2); } return -1; } @Override // java.io.InputStream, io.grpc.KnownLength public final int available() { InterfaceC16581pUl interfaceC16581pUl = this.message; if (interfaceC16581pUl != null) { return interfaceC16581pUl.getSerializedSize(); } ByteArrayInputStream byteArrayInputStream = this.partial; if (byteArrayInputStream != null) { return byteArrayInputStream.available(); } return 0; } /* JADX INFO: Access modifiers changed from: package-private */ public final InterfaceC16581pUl message() { InterfaceC16581pUl interfaceC16581pUl = this.message; if (interfaceC16581pUl != null) { return interfaceC16581pUl; } throw new IllegalStateException("message not available"); } /* JADX INFO: Access modifiers changed from: package-private */ public final XgX parser() { return this.parser; } }