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.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
26
27 @UnstableApi
28 public abstract class AbstractBinaryMemcacheEncoder<M extends BinaryMemcacheMessage>
29 extends AbstractMemcacheObjectEncoder<M> {
30
31
32
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
50
51
52
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
64
65
66
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
78
79
80
81
82
83
84
85 protected abstract void encodeHeader(ByteBuf buf, M msg);
86
87 }