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