diff --git a/core/src/main/java/io/grpc/internal/AbstractClientStream.java b/core/src/main/java/io/grpc/internal/AbstractClientStream.java index 2d44decf13..5c5072710d 100644 --- a/core/src/main/java/io/grpc/internal/AbstractClientStream.java +++ b/core/src/main/java/io/grpc/internal/AbstractClientStream.java @@ -25,6 +25,7 @@ import static java.lang.Math.max; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Preconditions; +import com.google.common.io.ByteStreams; import io.grpc.CallOptions; import io.grpc.Codec; import io.grpc.Compressor; @@ -478,11 +479,12 @@ public abstract class AbstractClientStream extends AbstractStream this.statsTraceCtx = checkNotNull(statsTraceCtx, "statsTraceCtx"); } + @SuppressWarnings("BetaApi") // ByteStreams is not Beta in v27 @Override public void writePayload(InputStream message) { checkState(payload == null, "writePayload should not be called multiple times"); try { - payload = IoUtils.toByteArray(message); + payload = ByteStreams.toByteArray(message); } catch (java.io.IOException ex) { throw new RuntimeException(ex); } diff --git a/core/src/main/java/io/grpc/internal/IoUtils.java b/core/src/main/java/io/grpc/internal/IoUtils.java deleted file mode 100644 index f66b3bb04a..0000000000 --- a/core/src/main/java/io/grpc/internal/IoUtils.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright 2016 The gRPC Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.grpc.internal; - -import static com.google.common.base.Preconditions.checkNotNull; - -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; - -/** Common IoUtils for thrift and nanopb to convert inputstream to bytes. */ -public final class IoUtils { - - /** maximum buffer to be read is 16 KB. */ - private static final int MAX_BUFFER_LENGTH = 16384; - - /** Returns the byte array. */ - public static byte[] toByteArray(InputStream in) throws IOException { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - copy(in, out); - return out.toByteArray(); - } - - /** Copies the data from input stream to output stream. */ - public static long copy(InputStream from, OutputStream to) throws IOException { - // Copied from guava com.google.common.io.ByteStreams because its API is unstable (beta) - checkNotNull(from); - checkNotNull(to); - byte[] buf = new byte[MAX_BUFFER_LENGTH]; - long total = 0; - while (true) { - int r = from.read(buf); - if (r == -1) { - break; - } - to.write(buf, 0, r); - total += r; - } - return total; - } -} diff --git a/core/src/main/java/io/grpc/internal/MessageFramer.java b/core/src/main/java/io/grpc/internal/MessageFramer.java index dfac24e79e..ad86d450fa 100644 --- a/core/src/main/java/io/grpc/internal/MessageFramer.java +++ b/core/src/main/java/io/grpc/internal/MessageFramer.java @@ -21,6 +21,7 @@ import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkState; import static java.lang.Math.min; +import com.google.common.io.ByteStreams; import io.grpc.Codec; import io.grpc.Compressor; import io.grpc.Drainable; @@ -269,7 +270,8 @@ public class MessageFramer implements Framer { } else { // This makes an unnecessary copy of the bytes when bytebuf supports array(). However, we // expect performance-critical code to support flushTo(). - long written = IoUtils.copy(message, outputStream); + @SuppressWarnings("BetaApi") // ByteStreams is not Beta in v27 + long written = ByteStreams.copy(message, outputStream); checkArgument(written <= Integer.MAX_VALUE, "Message size overflow: %s", written); return (int) written; } diff --git a/core/src/test/java/io/grpc/internal/AbstractTransportTest.java b/core/src/test/java/io/grpc/internal/AbstractTransportTest.java index 448a6c4327..b3cf144a70 100644 --- a/core/src/test/java/io/grpc/internal/AbstractTransportTest.java +++ b/core/src/test/java/io/grpc/internal/AbstractTransportTest.java @@ -42,6 +42,7 @@ import static org.mockito.Mockito.verify; import com.google.common.base.Objects; import com.google.common.collect.Iterables; import com.google.common.collect.Lists; +import com.google.common.io.ByteStreams; import com.google.common.util.concurrent.MoreExecutors; import com.google.common.util.concurrent.SettableFuture; import io.grpc.Attributes; @@ -2293,7 +2294,7 @@ public abstract class AbstractTransportTest { @Override public String parse(InputStream stream) { try { - return new String(IoUtils.toByteArray(stream), UTF_8); + return new String(ByteStreams.toByteArray(stream), UTF_8); } catch (IOException ex) { throw new RuntimeException(ex); } diff --git a/core/src/test/java/io/grpc/internal/IoUtilsTest.java b/core/src/test/java/io/grpc/internal/IoUtilsTest.java deleted file mode 100644 index fc51985237..0000000000 --- a/core/src/test/java/io/grpc/internal/IoUtilsTest.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright 2016 The gRPC Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.grpc.internal; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotSame; - -import java.io.ByteArrayInputStream; -import java.io.InputStream; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -/** Unit test for IoUtils. */ -@RunWith(JUnit4.class) -public class IoUtilsTest { - - @Test - public void testRoundTrip() throws Exception { - byte[] bytes = { 1, 2, 3, -127, 100, 127}; - InputStream is = new ByteArrayInputStream(bytes); - byte[] bytes2 = IoUtils.toByteArray(is); - - assertNotSame(bytes2, bytes); - assertEquals(bytes.length, bytes2.length); - for (int i = 0; i < bytes.length; ++i) { - assertEquals(bytes[i], bytes2[i]); - } - } - - @Test - public void testEmpty() throws Exception { - InputStream is = new ByteArrayInputStream(new byte[0]); - byte[] bytes = IoUtils.toByteArray(is); - - assertEquals(0, bytes.length); - } -} \ No newline at end of file diff --git a/services/src/test/java/io/grpc/services/BinaryLogProviderTest.java b/services/src/test/java/io/grpc/services/BinaryLogProviderTest.java index 454ec6d76d..b6dcbab6e9 100644 --- a/services/src/test/java/io/grpc/services/BinaryLogProviderTest.java +++ b/services/src/test/java/io/grpc/services/BinaryLogProviderTest.java @@ -40,7 +40,6 @@ import io.grpc.ServerCall.Listener; import io.grpc.ServerCallHandler; import io.grpc.ServerInterceptor; import io.grpc.ServerMethodDefinition; -import io.grpc.internal.IoUtils; import io.grpc.internal.NoopClientCall; import io.grpc.internal.NoopServerCall; import java.io.ByteArrayInputStream; @@ -318,7 +317,7 @@ public class BinaryLogProviderTest { public void onMessage(RespT message) { assertTrue(message instanceof InputStream); try { - byte[] bytes = IoUtils.toByteArray((InputStream) message); + byte[] bytes = ByteStreams.toByteArray((InputStream) message); binlogResp.add(bytes); ByteArrayInputStream input = new ByteArrayInputStream(bytes); RespT dup = method.parseResponse(input); @@ -371,7 +370,7 @@ public class BinaryLogProviderTest { public void onMessage(ReqT message) { assertTrue(message instanceof InputStream); try { - byte[] bytes = IoUtils.toByteArray((InputStream) message); + byte[] bytes = ByteStreams.toByteArray((InputStream) message); binlogReq.add(bytes); ByteArrayInputStream input = new ByteArrayInputStream(bytes); ReqT dup = call.getMethodDescriptor().parseRequest(input);