View Javadoc
1   /*
2    * Copyright 2013 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    *   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, 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.memcache.binary;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.channel.ChannelHandlerContext;
20  import io.netty.handler.codec.MessageToByteEncoder;
21  import io.netty.handler.codec.memcache.AbstractMemcacheObjectEncoder;
22  import io.netty.util.internal.UnstableApi;
23  
24  /**
25   * A {@link MessageToByteEncoder} that encodes binary memcache messages into bytes.
26   */
27  @UnstableApi
28  public abstract class AbstractBinaryMemcacheEncoder<M extends BinaryMemcacheMessage>
29      extends AbstractMemcacheObjectEncoder<M> {
30  
31      /**
32       * Every binary memcache message has at least a 24 bytes header.
33       */
34      private static final int MINIMUM_HEADER_SIZE = 24;
35  
36      @Override
37      protected ByteBuf encodeMessage(ChannelHandlerContext ctx, M msg) {
38          ByteBuf buf = ctx.alloc().buffer(MINIMUM_HEADER_SIZE + msg.extrasLength()
39              + msg.keyLength());
40  
41          encodeHeader(buf, msg);
42          encodeExtras(buf, msg.extras());
43          encodeKey(buf, msg.key());
44  
45          return buf;
46      }
47  
48      /**
49       * Encode the extras.
50       *
51       * @param buf    the {@link ByteBuf} to write into.
52       * @param extras the extras to encode.
53       */
54      private static void encodeExtras(ByteBuf buf, ByteBuf extras) {
55          if (extras == null || !extras.isReadable()) {
56              return;
57          }
58  
59          buf.writeBytes(extras);
60      }
61  
62      /**
63       * Encode the key.
64       *
65       * @param buf the {@link ByteBuf} to write into.
66       * @param key the key to encode.
67       */
68      private static void encodeKey(ByteBuf buf, ByteBuf key) {
69          if (key == null || !key.isReadable()) {
70              return;
71          }
72  
73          buf.writeBytes(key);
74      }
75  
76      /**
77       * Encode the header.
78       * <p/>
79       * This methods needs to be implemented by a sub class because the header is different
80       * for both requests and responses.
81       *
82       * @param buf the {@link ByteBuf} to write into.
83       * @param msg the message to encode.
84       */
85      protected abstract void encodeHeader(ByteBuf buf, M msg);
86  
87  }