GrpcMessageMarshaller { public O deserializeResponse(DeframedMessage message, boolean grpcWebText) throws IOException { InputStream messageStream = message.stream(); final ByteBuf buf = message.buf(); if (buf != null) { try { switch (responseType) { case PROTOBUF: final PrototypeMarshaller<O> marshaller = (PrototypeMarshaller<O>) method.getResponseMarshaller(); @SuppressWarnings("unchecked") final O msg = (O) deserializeProto(marshaller, buf); return msg; default: messageStream = new ByteBufInputStream(buf.retain(), true); break; } } finally { if (!unsafeWrapDeserializedBuffer || grpcWebText) { buf.release(); } } } try (InputStream msg = messageStream) { if (isProto) { return method.parseResponse(msg); } else { return jsonMarshaller.deserializeMessage(responseMarshaller, msg); } } } GrpcMessageMarshaller(ByteBufAllocator alloc,
                                 SerializationFormat serializationFormat,
                                 MethodDescriptor<I, O> method,
                                 @Nullable GrpcJsonMarshaller jsonMarshaller,
                                 boolean unsafeWrapDeserializedBuffer); ByteBuf serializeRequest(I message); I deserializeRequest(DeframedMessage message, boolean grpcWebText); ByteBuf serializeResponse(O message); O deserializeResponse(DeframedMessage message, boolean grpcWebText);  }
@Test public void deserializeResponse_bytebuf() throws Exception { final ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(GrpcTestUtil.RESPONSE_MESSAGE.getSerializedSize()); assertThat(buf.refCnt()).isEqualTo(1); buf.writeBytes(GrpcTestUtil.RESPONSE_MESSAGE.toByteArray()); final SimpleResponse response = marshaller.deserializeResponse(new DeframedMessage(buf, 0), false); assertThat(response).isEqualTo(GrpcTestUtil.RESPONSE_MESSAGE); assertThat(buf.refCnt()).isEqualTo(0); }
@Test public void deserializeResponse_wrappedByteBuf() throws Exception { marshaller = new GrpcMessageMarshaller<>( ByteBufAllocator.DEFAULT, GrpcSerializationFormats.PROTO, TestServiceGrpc.getUnaryCallMethod(), DEFAULT_JSON_MARSHALLER, true); final ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(GrpcTestUtil.RESPONSE_MESSAGE.getSerializedSize()); assertThat(buf.refCnt()).isEqualTo(1); buf.writeBytes(GrpcTestUtil.RESPONSE_MESSAGE.toByteArray()); final SimpleResponse response = marshaller.deserializeResponse(new DeframedMessage(buf, 0), false); assertThat(response).isEqualTo(GrpcTestUtil.RESPONSE_MESSAGE); assertThat(buf.refCnt()).isEqualTo(1); buf.release(); }
@Test public void deserializeResponse_stream() throws Exception { final SimpleResponse response = marshaller.deserializeResponse( new DeframedMessage(new ByteArrayInputStream(GrpcTestUtil.RESPONSE_MESSAGE.toByteArray()), 0), false); assertThat(response).isEqualTo(GrpcTestUtil.RESPONSE_MESSAGE); }