private <T> ByteBuf serializeProto(PrototypeMarshaller<T> marshaller, Message message) throws IOException {
        if (isProto) {
            final int serializedSize = message.getSerializedSize();
            if (serializedSize == 0) {
                return Unpooled.EMPTY_BUFFER;
            }
            final ByteBuf buf = alloc.buffer(serializedSize);
            boolean success = false;
            try {
                if (useMethodMarshaller) {
                    final InputStream is = marshaller.stream((T) message);
                    try (ByteBufOutputStream os = new ByteBufOutputStream(buf)) {
                        ByteStreams.copy(is, os);
                    } finally {
                        is.close();
                    }
                } else {
                    message.writeTo(CodedOutputStream.newInstance(buf.nioBuffer(0, serializedSize)));
                    buf.writerIndex(serializedSize);
                }
                success = true;
            } finally {
                if (!success) {
                    buf.release();
                }
            }
            return buf;
        } else {
            // TODO(minwoox) Optimize this by creating buffer with the sensible initial capacity.
            final ByteBuf buf = alloc.compositeBuffer();
            boolean success = false;
            try (ByteBufOutputStream os = new ByteBufOutputStream(buf)) {
                @SuppressWarnings("unchecked")
                final T cast = (T) message;
                assert jsonMarshaller != null;
                jsonMarshaller.serializeMessage(marshaller, cast, os);
                success = true;
            } finally {
                if (!success) {
                    buf.release();
                }
            }
            return buf;
        }
    }
Marshaller for gRPC method request or response messages to and from {@link ByteBuf}. Will attempt to use
optimized code paths for known message types, and otherwise delegates to the gRPC stub.
grpc/src/main/java/com/linecorp/armeria/internal/common/grpc/GrpcMessageMarshaller.java