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    *   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.binary;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.buffer.Unpooled;
20  import io.netty.util.internal.ObjectUtil;
21  import io.netty.util.internal.UnstableApi;
22  
23  /**
24   * The default implementation of a {@link FullBinaryMemcacheResponse}.
25   */
26  @UnstableApi
27  public class DefaultFullBinaryMemcacheResponse extends DefaultBinaryMemcacheResponse
28      implements FullBinaryMemcacheResponse {
29  
30      private final ByteBuf content;
31  
32      /**
33       * Create a new {@link DefaultFullBinaryMemcacheResponse} with the header, key and extras.
34       *
35       * @param key    the key to use.
36       * @param extras the extras to use.
37       */
38      public DefaultFullBinaryMemcacheResponse(ByteBuf key, ByteBuf extras) {
39          this(key, extras, Unpooled.buffer(0));
40      }
41  
42      /**
43       * Create a new {@link DefaultFullBinaryMemcacheResponse} with the header, key, extras and content.
44       *
45       * @param key     the key to use.
46       * @param extras  the extras to use.
47       * @param content the content of the full request.
48       */
49      public DefaultFullBinaryMemcacheResponse(ByteBuf key, ByteBuf extras,
50          ByteBuf content) {
51          super(key, extras);
52          this.content = ObjectUtil.checkNotNull(content, "content");
53          setTotalBodyLength(keyLength() + extrasLength() + content.readableBytes());
54      }
55  
56      @Override
57      public ByteBuf content() {
58          return content;
59      }
60  
61      @Override
62      public FullBinaryMemcacheResponse retain() {
63          super.retain();
64          return this;
65      }
66  
67      @Override
68      public FullBinaryMemcacheResponse retain(int increment) {
69          super.retain(increment);
70          return this;
71      }
72  
73      @Override
74      public FullBinaryMemcacheResponse touch() {
75          super.touch();
76          return this;
77      }
78  
79      @Override
80      public FullBinaryMemcacheResponse touch(Object hint) {
81          super.touch(hint);
82          content.touch(hint);
83          return this;
84      }
85  
86      @Override
87      protected void deallocate() {
88          super.deallocate();
89          content.release();
90      }
91  
92      @Override
93      public FullBinaryMemcacheResponse copy() {
94          ByteBuf key = key();
95          if (key != null) {
96              key = key.copy();
97          }
98          ByteBuf extras = extras();
99          if (extras != null) {
100             extras = extras.copy();
101         }
102         return newInstance(key, extras, content().copy());
103     }
104 
105     @Override
106     public FullBinaryMemcacheResponse duplicate() {
107         ByteBuf key = key();
108         if (key != null) {
109             key = key.duplicate();
110         }
111         ByteBuf extras = extras();
112         if (extras != null) {
113             extras = extras.duplicate();
114         }
115         return newInstance(key, extras, content().duplicate());
116     }
117 
118     @Override
119     public FullBinaryMemcacheResponse retainedDuplicate() {
120         return replace(content().retainedDuplicate());
121     }
122 
123     @Override
124     public FullBinaryMemcacheResponse replace(ByteBuf content) {
125         ByteBuf key = key();
126         if (key != null) {
127             key = key.retainedDuplicate();
128         }
129         ByteBuf extras = extras();
130         if (extras != null) {
131             extras = extras.retainedDuplicate();
132         }
133         return newInstance(key, extras, content);
134     }
135 
136     private FullBinaryMemcacheResponse newInstance(ByteBuf key, ByteBuf extras, ByteBuf content) {
137         DefaultFullBinaryMemcacheResponse newInstance = new DefaultFullBinaryMemcacheResponse(key, extras, content);
138         copyMeta(newInstance);
139         return newInstance;
140     }
141 }