1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.handler.codec.memcache.binary;
17
18 import io.netty.buffer.ByteBuf;
19 import io.netty.handler.codec.memcache.AbstractMemcacheObject;
20 import io.netty.util.internal.UnstableApi;
21
22
23
24
25 @UnstableApi
26 public abstract class AbstractBinaryMemcacheMessage
27 extends AbstractMemcacheObject
28 implements BinaryMemcacheMessage {
29
30
31
32
33 private ByteBuf key;
34
35
36
37
38 private ByteBuf extras;
39
40 private byte magic;
41 private byte opcode;
42 private short keyLength;
43 private byte extrasLength;
44 private byte dataType;
45 private int totalBodyLength;
46 private int opaque;
47 private long cas;
48
49
50
51
52
53
54
55 protected AbstractBinaryMemcacheMessage(ByteBuf key, ByteBuf extras) {
56 this.key = key;
57 keyLength = key == null ? 0 : (short) key.readableBytes();
58 this.extras = extras;
59 extrasLength = extras == null ? 0 : (byte) extras.readableBytes();
60 totalBodyLength = keyLength + extrasLength;
61 }
62
63 @Override
64 public ByteBuf key() {
65 return key;
66 }
67
68 @Override
69 public ByteBuf extras() {
70 return extras;
71 }
72
73 @Override
74 public BinaryMemcacheMessage setKey(ByteBuf key) {
75 if (this.key != null) {
76 this.key.release();
77 }
78 this.key = key;
79 short oldKeyLength = keyLength;
80 keyLength = key == null ? 0 : (short) key.readableBytes();
81 totalBodyLength = totalBodyLength + keyLength - oldKeyLength;
82 return this;
83 }
84
85 @Override
86 public BinaryMemcacheMessage setExtras(ByteBuf extras) {
87 if (this.extras != null) {
88 this.extras.release();
89 }
90 this.extras = extras;
91 short oldExtrasLength = extrasLength;
92 extrasLength = extras == null ? 0 : (byte) extras.readableBytes();
93 totalBodyLength = totalBodyLength + extrasLength - oldExtrasLength;
94 return this;
95 }
96
97 @Override
98 public byte magic() {
99 return magic;
100 }
101
102 @Override
103 public BinaryMemcacheMessage setMagic(byte magic) {
104 this.magic = magic;
105 return this;
106 }
107
108 @Override
109 public long cas() {
110 return cas;
111 }
112
113 @Override
114 public BinaryMemcacheMessage setCas(long cas) {
115 this.cas = cas;
116 return this;
117 }
118
119 @Override
120 public int opaque() {
121 return opaque;
122 }
123
124 @Override
125 public BinaryMemcacheMessage setOpaque(int opaque) {
126 this.opaque = opaque;
127 return this;
128 }
129
130 @Override
131 public int totalBodyLength() {
132 return totalBodyLength;
133 }
134
135 @Override
136 public BinaryMemcacheMessage setTotalBodyLength(int totalBodyLength) {
137 this.totalBodyLength = totalBodyLength;
138 return this;
139 }
140
141 @Override
142 public byte dataType() {
143 return dataType;
144 }
145
146 @Override
147 public BinaryMemcacheMessage setDataType(byte dataType) {
148 this.dataType = dataType;
149 return this;
150 }
151
152 @Override
153 public byte extrasLength() {
154 return extrasLength;
155 }
156
157
158
159
160
161
162
163
164 BinaryMemcacheMessage setExtrasLength(byte extrasLength) {
165 this.extrasLength = extrasLength;
166 return this;
167 }
168
169 @Override
170 public short keyLength() {
171 return keyLength;
172 }
173
174
175
176
177
178
179
180
181 BinaryMemcacheMessage setKeyLength(short keyLength) {
182 this.keyLength = keyLength;
183 return this;
184 }
185
186 @Override
187 public byte opcode() {
188 return opcode;
189 }
190
191 @Override
192 public BinaryMemcacheMessage setOpcode(byte opcode) {
193 this.opcode = opcode;
194 return this;
195 }
196
197 @Override
198 public BinaryMemcacheMessage retain() {
199 super.retain();
200 return this;
201 }
202
203 @Override
204 public BinaryMemcacheMessage retain(int increment) {
205 super.retain(increment);
206 return this;
207 }
208
209 @Override
210 protected void deallocate() {
211 if (key != null) {
212 key.release();
213 }
214 if (extras != null) {
215 extras.release();
216 }
217 }
218
219 @Override
220 public BinaryMemcacheMessage touch() {
221 super.touch();
222 return this;
223 }
224
225 @Override
226 public BinaryMemcacheMessage touch(Object hint) {
227 if (key != null) {
228 key.touch(hint);
229 }
230 if (extras != null) {
231 extras.touch(hint);
232 }
233 return this;
234 }
235
236
237
238
239
240
241 void copyMeta(AbstractBinaryMemcacheMessage dst) {
242 dst.magic = magic;
243 dst.opcode = opcode;
244 dst.keyLength = keyLength;
245 dst.extrasLength = extrasLength;
246 dst.dataType = dataType;
247 dst.totalBodyLength = totalBodyLength;
248 dst.opaque = opaque;
249 dst.cas = cas;
250 }
251 }