aboutsummaryrefslogtreecommitdiff
path: root/protobuf-nano
diff options
context:
space:
mode:
authorzpencer <spencerfang@google.com>2017-09-08 16:33:27 -0700
committerGitHub <noreply@github.com>2017-09-08 16:33:27 -0700
commitb8b5f5e0469e963c3087d535f8ccd29f14023d50 (patch)
treef573f2166ea723e208595bf2acb3ed4dec47823d /protobuf-nano
parent53f56a32f146a7730f8ca9894a8feaf0815451f3 (diff)
downloadgrpc-grpc-java-b8b5f5e0469e963c3087d535f8ccd29f14023d50.tar.gz
protobuf-nano: do not use preexisting IoUtils internal class #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.
Diffstat (limited to 'protobuf-nano')
-rw-r--r--protobuf-nano/src/main/java/io/grpc/protobuf/nano/NanoUtils.java30
1 files changed, 28 insertions, 2 deletions
diff --git a/protobuf-nano/src/main/java/io/grpc/protobuf/nano/NanoUtils.java b/protobuf-nano/src/main/java/io/grpc/protobuf/nano/NanoUtils.java
index 35648c9d6..a1c10a6c7 100644
--- a/protobuf-nano/src/main/java/io/grpc/protobuf/nano/NanoUtils.java
+++ b/protobuf-nano/src/main/java/io/grpc/protobuf/nano/NanoUtils.java
@@ -16,14 +16,16 @@
package io.grpc.protobuf.nano;
+import static com.google.common.base.Preconditions.checkNotNull;
import com.google.protobuf.nano.CodedInputByteBufferNano;
import com.google.protobuf.nano.MessageNano;
import io.grpc.MethodDescriptor.Marshaller;
import io.grpc.Status;
-import io.grpc.internal.IoUtils;
+import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.io.OutputStream;
/**
* Utility methods for using nano proto with grpc.
@@ -48,7 +50,7 @@ public class NanoUtils {
try {
// TODO(simonma): Investigate whether we can do 0-copy here.
CodedInputByteBufferNano input =
- CodedInputByteBufferNano.newInstance(IoUtils.toByteArray(stream));
+ CodedInputByteBufferNano.newInstance(toByteArray(stream));
input.setSizeLimit(Integer.MAX_VALUE);
T message = factory.newInstance();
message.mergeFrom(input);
@@ -60,4 +62,28 @@ public class NanoUtils {
}
};
}
+
+ // Copied from guava com.google.common.io.ByteStreams because its API is unstable (beta)
+ private static byte[] toByteArray(InputStream in) throws IOException {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ copy(in, out);
+ return out.toByteArray();
+ }
+
+ // Copied from guava com.google.common.io.ByteStreams because its API is unstable (beta)
+ private static long copy(InputStream from, OutputStream to) throws IOException {
+ checkNotNull(from);
+ checkNotNull(to);
+ byte[] buf = new byte[BUF_SIZE];
+ long total = 0;
+ while (true) {
+ int r = from.read(buf);
+ if (r == -1) {
+ break;
+ }
+ to.write(buf, 0, r);
+ total += r;
+ }
+ return total;
+ }
}