查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
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    *   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.memcache;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.buffer.Unpooled;
20  import io.netty.channel.ChannelHandlerContext;
21  import io.netty.channel.FileRegion;
22  import io.netty.handler.codec.MessageToMessageEncoder;
23  import io.netty.util.internal.StringUtil;
24  import io.netty.util.internal.UnstableApi;
25  
26  import java.util.List;
27  
28  /**
29   * A general purpose {@link AbstractMemcacheObjectEncoder} that encodes {@link MemcacheMessage}s.
30   * <p/>
31   * <p>Note that this class is designed to be extended, especially because both the binary and ascii protocol
32   * require different treatment of their messages. Since the content chunk writing is the same for both, the encoder
33   * abstracts this right away.</p>
34   */
35  @UnstableApi
36  public abstract class AbstractMemcacheObjectEncoder<M extends MemcacheMessage> extends MessageToMessageEncoder<Object> {
37  
38      private boolean expectingMoreContent;
39  
40      @Override
41      protected void encode(ChannelHandlerContext ctx, Object msg, List<Object> out) throws Exception {
42          if (msg instanceof MemcacheMessage) {
43              if (expectingMoreContent) {
44                  throw new IllegalStateException("unexpected message type: " + StringUtil.simpleClassName(msg));
45              }
46  
47              @SuppressWarnings({ "unchecked", "CastConflictsWithInstanceof" })
48              final M m = (M) msg;
49              out.add(encodeMessage(ctx, m));
50          }
51  
52          if (msg instanceof MemcacheContent || msg instanceof ByteBuf || msg instanceof FileRegion) {
53              int contentLength = contentLength(msg);
54              if (contentLength > 0) {
55                  out.add(encodeAndRetain(msg));
56              } else {
57                  out.add(Unpooled.EMPTY_BUFFER);
58              }
59  
60              expectingMoreContent = !(msg instanceof LastMemcacheContent);
61          }
62      }
63  
64      @Override
65      public boolean acceptOutboundMessage(Object msg) throws Exception {
66          return msg instanceof MemcacheObject || msg instanceof ByteBuf || msg instanceof FileRegion;
67      }
68  
69      /**
70       * Take the given {@link MemcacheMessage} and encode it into a writable {@link ByteBuf}.
71       *
72       * @param ctx the channel handler context.
73       * @param msg the message to encode.
74       * @return the {@link ByteBuf} representation of the message.
75       */
76      protected abstract ByteBuf encodeMessage(ChannelHandlerContext ctx, M msg);
77  
78      /**
79       * Determine the content length of the given object.
80       *
81       * @param msg the object to determine the length of.
82       * @return the determined content length.
83       */
84      private static int contentLength(Object msg) {
85          if (msg instanceof MemcacheContent) {
86              return ((MemcacheContent) msg).content().readableBytes();
87          }
88          if (msg instanceof ByteBuf) {
89              return ((ByteBuf) msg).readableBytes();
90          }
91          if (msg instanceof FileRegion) {
92              return (int) ((FileRegion) msg).count();
93          }
94          throw new IllegalStateException("unexpected message type: " + StringUtil.simpleClassName(msg));
95      }
96  
97      /**
98       * Encode the content, depending on the object type.
99       *
100      * @param msg the object to encode.
101      * @return the encoded object.
102      */
103     private static Object encodeAndRetain(Object msg) {
104         if (msg instanceof ByteBuf) {
105             return ((ByteBuf) msg).retain();
106         }
107         if (msg instanceof MemcacheContent) {
108             return ((MemcacheContent) msg).content().retain();
109         }
110         if (msg instanceof FileRegion) {
111             return ((FileRegion) msg).retain();
112         }
113         throw new IllegalStateException("unexpected message type: " + StringUtil.simpleClassName(msg));
114     }
115 
116 }