1 /*
2 * Copyright 2015 The Netty Project
3 *
4 * The Netty Project licenses this file to you under the Apache License,
5 * version 2.0 (the "License"); you may not use this file except in compliance
6 * with the License. You may obtain a copy of the License at:
7 *
8 * https://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, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations
14 * under the License.
15 */
16 package io.netty.handler.codec.protobuf;
17
18 import com.google.protobuf.nano.MessageNano;
19
20 import java.util.List;
21
22 import io.netty.buffer.ByteBuf;
23 import io.netty.buffer.ByteBufUtil;
24 import io.netty.channel.ChannelHandler.Sharable;
25 import io.netty.channel.ChannelHandlerContext;
26 import io.netty.channel.ChannelPipeline;
27 import io.netty.handler.codec.ByteToMessageDecoder;
28 import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
29 import io.netty.handler.codec.MessageToMessageDecoder;
30 import io.netty.util.internal.ObjectUtil;
31
32 /**
33 * Decodes a received {@link ByteBuf} into a
34 * <a href="https://github.com/google/protobuf">Google Protocol Buffers</a>
35 * {@link MessageNano}. Please note that this decoder must
36 * be used with a proper {@link ByteToMessageDecoder} such as {@link LengthFieldBasedFrameDecoder}
37 * if you are using a stream-based transport such as TCP/IP. A typical setup for TCP/IP would be:
38 * <pre>
39 * {@link ChannelPipeline} pipeline = ...;
40 *
41 * // Decoders
42 * pipeline.addLast("frameDecoder",
43 * new {@link LengthFieldBasedFrameDecoder}(1048576, 0, 4, 0, 4));
44 * pipeline.addLast("protobufDecoder",
45 * new {@link ProtobufDecoderNano}(MyMessage.getDefaultInstance()));
46 *
47 * // Encoder
48 * pipeline.addLast("frameEncoder", new {@link io.netty.handler.codec.LengthFieldPrepender}(4));
49 * pipeline.addLast("protobufEncoder", new {@link ProtobufEncoderNano}());
50 * </pre>
51 * and then you can use a {@code MyMessage} instead of a {@link ByteBuf}
52 * as a message:
53 * <pre>
54 * void channelRead({@link ChannelHandlerContext} ctx, Object msg) {
55 * MyMessage req = (MyMessage) msg;
56 * MyMessage res = MyMessage.newBuilder().setText(
57 * "Did you say '" + req.getText() + "'?").build();
58 * ch.write(res);
59 * }
60 * </pre>
61 */
62 @Sharable
63 public class ProtobufDecoderNano extends MessageToMessageDecoder<ByteBuf> {
64 private final Class<? extends MessageNano> clazz;
65 /**
66 * Creates a new instance.
67 */
68 public ProtobufDecoderNano(Class<? extends MessageNano> clazz) {
69 this.clazz = ObjectUtil.checkNotNull(clazz, "You must provide a Class");
70 }
71
72 @Override
73 protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out)
74 throws Exception {
75 final byte[] array;
76 final int offset;
77 final int length = msg.readableBytes();
78 if (msg.hasArray()) {
79 array = msg.array();
80 offset = msg.arrayOffset() + msg.readerIndex();
81 } else {
82 array = ByteBufUtil.getBytes(msg, msg.readerIndex(), length, false);
83 offset = 0;
84 }
85 MessageNano prototype = clazz.getConstructor().newInstance();
86 out.add(MessageNano.mergeFrom(prototype, array, offset, length));
87 }
88 }