本文整理汇总了Java中io.grpc.Codec类的典型用法代码示例。如果您正苦于以下问题:Java Codec类的具体用法?Java Codec怎么用?Java Codec使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Codec类属于io.grpc包,在下文中一共展示了Codec类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getCompressedBody
import io.grpc.Codec; //导入依赖的package包/类
private ByteBufOrStream getCompressedBody() {
if (decompressor == Codec.Identity.NONE) {
throw Status.INTERNAL.withDescription(
DEBUG_STRING + ": Can't decode compressed frame as compression not configured.")
.asRuntimeException();
}
try {
// Enforce the maxMessageSizeBytes limit on the returned stream.
InputStream unlimitedStream =
decompressor.decompress(new ByteBufInputStream(nextFrame, true));
return new ByteBufOrStream(
new SizeEnforcingInputStream(unlimitedStream, maxMessageSizeBytes, DEBUG_STRING));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
示例2: prepareHeaders
import io.grpc.Codec; //导入依赖的package包/类
@VisibleForTesting
static void prepareHeaders(
Metadata headers,
DecompressorRegistry decompressorRegistry,
Compressor compressor,
boolean fullStreamDecompression) {
headers.discardAll(MESSAGE_ENCODING_KEY);
if (compressor != Codec.Identity.NONE) {
headers.put(MESSAGE_ENCODING_KEY, compressor.getMessageEncoding());
}
headers.discardAll(MESSAGE_ACCEPT_ENCODING_KEY);
byte[] advertisedEncodings =
InternalDecompressorRegistry.getRawAdvertisedMessageEncodings(decompressorRegistry);
if (advertisedEncodings.length != 0) {
headers.put(MESSAGE_ACCEPT_ENCODING_KEY, advertisedEncodings);
}
headers.discardAll(CONTENT_ENCODING_KEY);
headers.discardAll(CONTENT_ACCEPT_ENCODING_KEY);
if (fullStreamDecompression) {
headers.put(CONTENT_ACCEPT_ENCODING_KEY, FULL_STREAM_DECOMPRESSION_ENCODINGS);
}
}
示例3: getCompressedBody
import io.grpc.Codec; //导入依赖的package包/类
private InputStream getCompressedBody() {
if (decompressor == Codec.Identity.NONE) {
throw Status.INTERNAL.withDescription(
debugString + ": Can't decode compressed frame as compression not configured.")
.asRuntimeException();
}
try {
// Enforce the maxMessageSize limit on the returned stream.
InputStream unlimitedStream =
decompressor.decompress(ReadableBuffers.openStream(nextFrame, true));
return new SizeEnforcingInputStream(
unlimitedStream, maxInboundMessageSize, statsTraceCtx, debugString);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
示例4: prepareHeaders_removeReservedHeaders
import io.grpc.Codec; //导入依赖的package包/类
@Test
public void prepareHeaders_removeReservedHeaders() {
Metadata m = new Metadata();
m.put(GrpcUtil.MESSAGE_ENCODING_KEY, "gzip");
m.put(GrpcUtil.MESSAGE_ACCEPT_ENCODING_KEY, "gzip".getBytes(GrpcUtil.US_ASCII));
m.put(GrpcUtil.CONTENT_ENCODING_KEY, "gzip");
m.put(GrpcUtil.CONTENT_ACCEPT_ENCODING_KEY, "gzip".getBytes(GrpcUtil.US_ASCII));
ClientCallImpl.prepareHeaders(
m, DecompressorRegistry.emptyInstance(), Codec.Identity.NONE, false);
assertNull(m.get(GrpcUtil.MESSAGE_ENCODING_KEY));
assertNull(m.get(GrpcUtil.MESSAGE_ACCEPT_ENCODING_KEY));
assertNull(m.get(GrpcUtil.CONTENT_ENCODING_KEY));
assertNull(m.get(GrpcUtil.CONTENT_ACCEPT_ENCODING_KEY));
}
示例5: inboundHeadersReceived_disallowsContentAndMessageEncoding
import io.grpc.Codec; //导入依赖的package包/类
@Test
public void inboundHeadersReceived_disallowsContentAndMessageEncoding() {
AbstractClientStream stream =
new BaseAbstractClientStream(allocator, statsTraceCtx, transportTracer);
stream.start(mockListener);
Metadata headers = new Metadata();
headers.put(GrpcUtil.CONTENT_ENCODING_KEY, "gzip");
headers.put(GrpcUtil.MESSAGE_ENCODING_KEY, new Codec.Gzip().getMessageEncoding());
stream.setFullStreamDecompression(true);
stream.transportState().inboundHeadersReceived(headers);
verifyNoMoreInteractions(mockListener);
Throwable t = ((BaseTransportState) stream.transportState()).getDeframeFailedCause();
assertEquals(Status.INTERNAL.getCode(), Status.fromThrowable(t).getCode());
assertTrue(
"unexpected deframe failed description",
Status.fromThrowable(t)
.getDescription()
.equals("Full stream and gRPC message encoding cannot both be set"));
}
示例6: compressed
import io.grpc.Codec; //导入依赖的package包/类
@Test
public void compressed() throws Exception {
allocator = new BytesWritableBufferAllocator(100, Integer.MAX_VALUE);
// setMessageCompression should default to true
framer = new MessageFramer(sink, allocator, statsTraceCtx)
.setCompressor(new Codec.Gzip());
writeKnownLength(framer, new byte[1000]);
framer.flush();
// The GRPC header is written first as a separate frame.
// The message count is only bumped when a message is completely written.
verify(sink).deliverFrame(frameCaptor.capture(), eq(false), eq(false), eq(0));
verify(sink).deliverFrame(frameCaptor.capture(), eq(false), eq(true), eq(1));
// Check the header
ByteWritableBuffer buffer = frameCaptor.getAllValues().get(0);
assertEquals(0x1, buffer.data[0]);
ByteBuffer byteBuf = ByteBuffer.wrap(buffer.data, 1, 4);
byteBuf.order(ByteOrder.BIG_ENDIAN);
int length = byteBuf.getInt();
// compressed data should be smaller than uncompressed data.
assertTrue(length < 1000);
assertEquals(frameCaptor.getAllValues().get(1).size(), length);
checkStats(length, 1000);
}
示例7: dontCompressIfNotRequested
import io.grpc.Codec; //导入依赖的package包/类
@Test
public void dontCompressIfNotRequested() throws Exception {
allocator = new BytesWritableBufferAllocator(100, Integer.MAX_VALUE);
framer = new MessageFramer(sink, allocator, statsTraceCtx)
.setCompressor(new Codec.Gzip())
.setMessageCompression(false);
writeKnownLength(framer, new byte[1000]);
framer.flush();
// The GRPC header is written first as a separate frame
verify(sink).deliverFrame(frameCaptor.capture(), eq(false), eq(true), eq(1));
// Check the header
ByteWritableBuffer buffer = frameCaptor.getAllValues().get(0);
// We purposefully don't check the last byte of length, since that depends on how exactly it
// compressed.
assertEquals(0x0, buffer.data[0]);
ByteBuffer byteBuf = ByteBuffer.wrap(buffer.data, 1, 4);
byteBuf.order(ByteOrder.BIG_ENDIAN);
int length = byteBuf.getInt();
assertEquals(1000, length);
assertEquals(buffer.data.length - 5 , length);
checkStats(1000, 1000);
}
示例8: sendHeaders
import io.grpc.Codec; //导入依赖的package包/类
@Override
public void sendHeaders(Metadata unusedGrpcMetadata) {
checkState(!sendHeadersCalled, "sendHeaders already called");
checkState(!closeCalled, "call is closed");
HttpHeaders headers = HttpHeaders.of(HttpStatus.OK);
headers.contentType(serializationFormat.mediaType());
if (compressor == null || !messageCompression || clientAcceptEncoding == null) {
compressor = Codec.Identity.NONE;
} else {
List<String> acceptedEncodingsList =
ACCEPT_ENCODING_SPLITTER.splitToList(clientAcceptEncoding);
if (!acceptedEncodingsList.contains(compressor.getMessageEncoding())) {
// resort to using no compression.
compressor = Codec.Identity.NONE;
}
}
messageFramer.setCompressor(compressor);
// Always put compressor, even if it's identity.
headers.add(GrpcHeaderNames.GRPC_ENCODING, compressor.getMessageEncoding());
String advertisedEncodings = String.join(",", decompressorRegistry.getAdvertisedMessageEncodings());
if (!advertisedEncodings.isEmpty()) {
headers.add(GrpcHeaderNames.GRPC_ACCEPT_ENCODING, advertisedEncodings);
}
sendHeadersCalled = true;
res.write(headers);
}
示例9: uncompressedClient_compressedEndpoint
import io.grpc.Codec; //导入依赖的package包/类
@Test
public void uncompressedClient_compressedEndpoint() throws Exception {
ManagedChannel nonDecompressingChannel =
ManagedChannelBuilder.forAddress("127.0.0.1", server.httpPort())
.decompressorRegistry(
DecompressorRegistry.emptyInstance()
.with(Codec.Identity.NONE, false))
.usePlaintext(true)
.build();
UnitTestServiceBlockingStub client = UnitTestServiceGrpc.newBlockingStub(nonDecompressingChannel);
assertThat(client.staticUnaryCallSetsMessageCompression(REQUEST_MESSAGE))
.isEqualTo(RESPONSE_MESSAGE);
nonDecompressingChannel.shutdownNow();
}
示例10: sendHeaders
import io.grpc.Codec; //导入依赖的package包/类
@Override
public void sendHeaders(Metadata headers) {
checkState(!sendHeadersCalled, "sendHeaders has already been called");
checkState(!closeCalled, "call is closed");
headers.discardAll(MESSAGE_ENCODING_KEY);
if (compressor == null) {
compressor = Codec.Identity.NONE;
} else {
if (messageAcceptEncoding != null) {
// TODO(carl-mastrangelo): remove the string allocation.
if (!GrpcUtil.iterableContains(
ACCEPT_ENCODING_SPLITTER.split(new String(messageAcceptEncoding, GrpcUtil.US_ASCII)),
compressor.getMessageEncoding())) {
// resort to using no compression.
compressor = Codec.Identity.NONE;
}
} else {
compressor = Codec.Identity.NONE;
}
}
// Always put compressor, even if it's identity.
headers.put(MESSAGE_ENCODING_KEY, compressor.getMessageEncoding());
stream.setCompressor(compressor);
headers.discardAll(MESSAGE_ACCEPT_ENCODING_KEY);
byte[] advertisedEncodings =
InternalDecompressorRegistry.getRawAdvertisedMessageEncodings(decompressorRegistry);
if (advertisedEncodings.length != 0) {
headers.put(MESSAGE_ACCEPT_ENCODING_KEY, advertisedEncodings);
}
// Don't check if sendMessage has been called, since it requires that sendHeaders was already
// called.
sendHeadersCalled = true;
stream.writeHeaders(headers);
}
示例11: TransportState
import io.grpc.Codec; //导入依赖的package包/类
protected TransportState(
int maxMessageSize,
StatsTraceContext statsTraceCtx,
TransportTracer transportTracer) {
this.statsTraceCtx = checkNotNull(statsTraceCtx, "statsTraceCtx");
this.transportTracer = checkNotNull(transportTracer, "transportTracer");
deframer = new MessageDeframer(
this,
Codec.Identity.NONE,
maxMessageSize,
statsTraceCtx,
transportTracer,
getClass().getName());
}
示例12: setFullStreamDecompressor
import io.grpc.Codec; //导入依赖的package包/类
@Override
public void setFullStreamDecompressor(GzipInflatingBuffer fullStreamDecompressor) {
checkState(decompressor == Codec.Identity.NONE, "per-message decompressor already set");
checkState(this.fullStreamDecompressor == null, "full stream decompressor already set");
this.fullStreamDecompressor =
checkNotNull(fullStreamDecompressor, "Can't pass a null full stream decompressor");
unprocessed = null;
}
示例13: setStream_sendsAllMessages
import io.grpc.Codec; //导入依赖的package包/类
@Test
public void setStream_sendsAllMessages() {
stream.start(listener);
stream.setCompressor(Codec.Identity.NONE);
stream.setDecompressorRegistry(DecompressorRegistry.getDefaultInstance());
stream.setMessageCompression(true);
InputStream message = new ByteArrayInputStream(new byte[]{'a'});
stream.writeMessage(message);
stream.setMessageCompression(false);
stream.writeMessage(message);
stream.setStream(realStream);
verify(realStream).setCompressor(Codec.Identity.NONE);
verify(realStream).setDecompressorRegistry(DecompressorRegistry.getDefaultInstance());
verify(realStream).setMessageCompression(true);
verify(realStream).setMessageCompression(false);
verify(realStream, times(2)).writeMessage(message);
verify(realStream).start(listenerCaptor.capture());
stream.writeMessage(message);
verify(realStream, times(3)).writeMessage(message);
verifyNoMoreInteractions(listener);
listenerCaptor.getValue().onReady();
verify(listener).onReady();
}
示例14: prepareHeaders_userAgentIgnored
import io.grpc.Codec; //导入依赖的package包/类
@Test
public void prepareHeaders_userAgentIgnored() {
Metadata m = new Metadata();
m.put(GrpcUtil.USER_AGENT_KEY, "batmobile");
ClientCallImpl.prepareHeaders(m, decompressorRegistry, Codec.Identity.NONE, false);
// User Agent is removed and set by the transport
assertThat(m.get(GrpcUtil.USER_AGENT_KEY)).isNotNull();
}
示例15: prepareHeaders_ignoreIdentityEncoding
import io.grpc.Codec; //导入依赖的package包/类
@Test
public void prepareHeaders_ignoreIdentityEncoding() {
Metadata m = new Metadata();
ClientCallImpl.prepareHeaders(m, decompressorRegistry, Codec.Identity.NONE, false);
assertNull(m.get(GrpcUtil.MESSAGE_ENCODING_KEY));
}