core,services: cleanup io.grpc.internal.IoUtils

This commit is contained in:
ZHANG Dapeng 2019-06-05 17:31:46 -07:00 committed by GitHub
parent 7657523b28
commit 6aadaf0a64
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 10 additions and 114 deletions

View File

@ -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);
}

View File

@ -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;
}
}

View File

@ -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;
}

View File

@ -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);
}

View File

@ -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);
}
}

View File

@ -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);