1 /*
2  * Copyright 2016 The gRPC Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package io.grpc.benchmarks;
18 
19 import io.grpc.MethodDescriptor;
20 import io.netty.buffer.ByteBuf;
21 import io.netty.buffer.EmptyByteBuf;
22 import io.netty.buffer.PooledByteBufAllocator;
23 import java.io.IOException;
24 import java.io.InputStream;
25 
26 /**
27  * Simple {@link io.grpc.MethodDescriptor.Marshaller} for Netty's {@link ByteBuf}.
28  */
29 public class ByteBufOutputMarshaller implements MethodDescriptor.Marshaller<ByteBuf> {
30 
31   public static final EmptyByteBuf EMPTY_BYTE_BUF =
32       new EmptyByteBuf(PooledByteBufAllocator.DEFAULT);
33 
34   @Override
stream(ByteBuf value)35   public InputStream stream(ByteBuf value) {
36     return new ByteBufInputStream(value);
37   }
38 
39   @Override
parse(InputStream stream)40   public ByteBuf parse(InputStream stream) {
41     try {
42       // We don't do anything with the message and it's already been read into buffers
43       // so just skip copying it.
44       stream.skip(stream.available());
45       return EMPTY_BYTE_BUF;
46     } catch (IOException ioe) {
47       throw new RuntimeException(ioe);
48     }
49   }
50 }
51