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.channel.ChannelHandlerContext;
20 import io.netty.handler.codec.EncoderException;
21 import io.netty.handler.codec.MessageToByteEncoder;
22 import io.netty.handler.codec.memcache.AbstractMemcacheObjectEncoder;
23 import io.netty.util.internal.UnstableApi;
24
25 /**
26 * A {@link MessageToByteEncoder} that encodes binary memcache messages into bytes.
27 */
28 @UnstableApi
29 public abstract class AbstractBinaryMemcacheEncoder<M extends BinaryMemcacheMessage>
30 extends AbstractMemcacheObjectEncoder<M> {
31
32 /**
33 * Every binary memcache message has at least a 24 bytes header.
34 */
35 private static final int MINIMUM_HEADER_SIZE = 24;
36
37 @Override
38 protected ByteBuf encodeMessage(ChannelHandlerContext ctx, M msg) {
39 int extrasLength = verifyLength(msg.extras(), msg.extrasLength() & 0xFF, "extras");
40 int keyLength = verifyLength(msg.key(), msg.keyLength() & 0xFFFF, "key");
41 ByteBuf buf = ctx.alloc().buffer(MINIMUM_HEADER_SIZE + extrasLength + keyLength);
42
43 encodeHeader(buf, msg);
44 encodeExtras(buf, msg.extras());
45 encodeKey(buf, msg.key());
46
47 return buf;
48 }
49
50 /**
51 * The header is written from the lengths recorded in the message, so the readable bytes of the key and extras
52 * must still equal those recorded lengths. Otherwise the header would not describe the bytes that follow it.
53 */
54 private static int verifyLength(ByteBuf buf, int expectedLength, String name) {
55 int actualLength = buf == null ? 0 : buf.readableBytes();
56 if (actualLength != expectedLength) {
57 throw new EncoderException(name + " length: " + actualLength + " (expected: " + expectedLength + ')');
58 }
59 return actualLength;
60 }
61
62 /**
63 * Encode the extras.
64 *
65 * @param buf the {@link ByteBuf} to write into.
66 * @param extras the extras to encode.
67 */
68 private static void encodeExtras(ByteBuf buf, ByteBuf extras) {
69 if (extras == null || !extras.isReadable()) {
70 return;
71 }
72
73 buf.writeBytes(extras);
74 }
75
76 /**
77 * Encode the key.
78 *
79 * @param buf the {@link ByteBuf} to write into.
80 * @param key the key to encode.
81 */
82 private static void encodeKey(ByteBuf buf, ByteBuf key) {
83 if (key == null || !key.isReadable()) {
84 return;
85 }
86
87 buf.writeBytes(key);
88 }
89
90 /**
91 * Encode the header.
92 * <p/>
93 * This methods needs to be implemented by a sub class because the header is different
94 * for both requests and responses.
95 *
96 * @param buf the {@link ByteBuf} to write into.
97 * @param msg the message to encode.
98 */
99 protected abstract void encodeHeader(ByteBuf buf, M msg);
100
101 }