Skip to content

Commit b8b5f5e

Browse files
authored
protobuf-nano: do not use preexisting IoUtils internal class grpc#3450
This reverts commit 671783f The dependency on core caused some problems with Proguard. There are android builds that should include protobuf-* but expect the rest of gRPC to be bundled with the runtime environment. In that case, when Proguard inspects the output, it will find a reference to IoUtil but fail to find the class itself. It makes the builds easier to just avoid this dependency.
1 parent 53f56a3 commit b8b5f5e

2 files changed

Lines changed: 29 additions & 3 deletions

File tree

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ subprojects {
344344
}
345345
}
346346
if (!(project.name in
347-
["grpc-stub", "grpc-protobuf", "grpc-protobuf-lite", "grpc-thrift"])) {
347+
["grpc-stub", "grpc-protobuf", "grpc-protobuf-lite", "grpc-protobuf-nano", "grpc-thrift"])) {
348348
def core = pom.dependencies.find {dep -> dep.artifactId == 'grpc-core'}
349349
if (core != null) {
350350
// Depend on specific version of grpc-core because internal package is unstable

protobuf-nano/src/main/java/io/grpc/protobuf/nano/NanoUtils.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@
1616

1717
package io.grpc.protobuf.nano;
1818

19+
import static com.google.common.base.Preconditions.checkNotNull;
1920

2021
import com.google.protobuf.nano.CodedInputByteBufferNano;
2122
import com.google.protobuf.nano.MessageNano;
2223
import io.grpc.MethodDescriptor.Marshaller;
2324
import io.grpc.Status;
24-
import io.grpc.internal.IoUtils;
25+
import java.io.ByteArrayOutputStream;
2526
import java.io.IOException;
2627
import java.io.InputStream;
28+
import java.io.OutputStream;
2729

2830
/**
2931
* Utility methods for using nano proto with grpc.
@@ -48,7 +50,7 @@ public T parse(InputStream stream) {
4850
try {
4951
// TODO(simonma): Investigate whether we can do 0-copy here.
5052
CodedInputByteBufferNano input =
51-
CodedInputByteBufferNano.newInstance(IoUtils.toByteArray(stream));
53+
CodedInputByteBufferNano.newInstance(toByteArray(stream));
5254
input.setSizeLimit(Integer.MAX_VALUE);
5355
T message = factory.newInstance();
5456
message.mergeFrom(input);
@@ -60,4 +62,28 @@ public T parse(InputStream stream) {
6062
}
6163
};
6264
}
65+
66+
// Copied from guava com.google.common.io.ByteStreams because its API is unstable (beta)
67+
private static byte[] toByteArray(InputStream in) throws IOException {
68+
ByteArrayOutputStream out = new ByteArrayOutputStream();
69+
copy(in, out);
70+
return out.toByteArray();
71+
}
72+
73+
// Copied from guava com.google.common.io.ByteStreams because its API is unstable (beta)
74+
private static long copy(InputStream from, OutputStream to) throws IOException {
75+
checkNotNull(from);
76+
checkNotNull(to);
77+
byte[] buf = new byte[BUF_SIZE];
78+
long total = 0;
79+
while (true) {
80+
int r = from.read(buf);
81+
if (r == -1) {
82+
break;
83+
}
84+
to.write(buf, 0, r);
85+
total += r;
86+
}
87+
return total;
88+
}
6389
}

0 commit comments

Comments
 (0)