GrpcMessageMarshaller { public I deserializeRequest(DeframedMessage message, boolean grpcWebText) throws IOException { InputStream messageStream = message.stream(); final ByteBuf buf = message.buf(); if (buf != null) { try { switch (requestType) { case PROTOBUF: final PrototypeMarshaller<I> marshaller = (PrototypeMarshaller<I>) requestMarshaller; @SuppressWarnings("unchecked") final I msg = (I) 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.parseRequest(msg); } else { return jsonMarshaller.deserializeMessage(requestMarshaller, 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 deserializeRequest_byteBuf() throws Exception { final ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(GrpcTestUtil.REQUEST_MESSAGE.getSerializedSize()); assertThat(buf.refCnt()).isEqualTo(1); buf.writeBytes(GrpcTestUtil.REQUEST_MESSAGE.toByteArray()); final SimpleRequest request = marshaller.deserializeRequest(new DeframedMessage(buf, 0), false); assertThat(request).isEqualTo(GrpcTestUtil.REQUEST_MESSAGE); assertThat(buf.refCnt()).isEqualTo(0); }
@Test public void deserializeRequest_wrappedByteBuf() throws Exception { marshaller = new GrpcMessageMarshaller<>( ByteBufAllocator.DEFAULT, GrpcSerializationFormats.PROTO, TestServiceGrpc.getUnaryCallMethod(), DEFAULT_JSON_MARSHALLER, true); final ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(GrpcTestUtil.REQUEST_MESSAGE.getSerializedSize()); assertThat(buf.refCnt()).isEqualTo(1); buf.writeBytes(GrpcTestUtil.REQUEST_MESSAGE.toByteArray()); final SimpleRequest request = marshaller.deserializeRequest(new DeframedMessage(buf, 0), false); assertThat(request).isEqualTo(GrpcTestUtil.REQUEST_MESSAGE); assertThat(buf.refCnt()).isEqualTo(1); buf.release(); }
@Test public void deserializeRequest_stream() throws Exception { final SimpleRequest request = marshaller.deserializeRequest( new DeframedMessage(new ByteArrayInputStream(GrpcTestUtil.REQUEST_MESSAGE.toByteArray()), 0), false); assertThat(request).isEqualTo(GrpcTestUtil.REQUEST_MESSAGE); }