Home / Class/ AbstractMemcacheObjectEncoder Class — netty Architecture

AbstractMemcacheObjectEncoder Class — netty Architecture

Architecture documentation for the AbstractMemcacheObjectEncoder class in AbstractMemcacheObjectEncoder.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  2483babf_38af_78d3_a690_99fb02b9904b["AbstractMemcacheObjectEncoder"]
  c14527ec_9fc2_1058_5452_2b061165385e["AbstractMemcacheObjectEncoder.java"]
  2483babf_38af_78d3_a690_99fb02b9904b -->|defined in| c14527ec_9fc2_1058_5452_2b061165385e
  dcb144a3_3f27_e86f_a5ae_e25c340ab842["AbstractMemcacheObjectEncoder()"]
  2483babf_38af_78d3_a690_99fb02b9904b -->|method| dcb144a3_3f27_e86f_a5ae_e25c340ab842
  917cd8db_0206_e46f_4936_482a44106ce3["encode()"]
  2483babf_38af_78d3_a690_99fb02b9904b -->|method| 917cd8db_0206_e46f_4936_482a44106ce3
  f1c2ae53_adb0_78c3_b51d_68de551045ed["acceptOutboundMessage()"]
  2483babf_38af_78d3_a690_99fb02b9904b -->|method| f1c2ae53_adb0_78c3_b51d_68de551045ed
  9722b18f_0dc1_aafd_82a0_54ca2f3663b2["ByteBuf()"]
  2483babf_38af_78d3_a690_99fb02b9904b -->|method| 9722b18f_0dc1_aafd_82a0_54ca2f3663b2
  6c95b6ea_792a_d20a_be2e_c1e99bb857a8["contentLength()"]
  2483babf_38af_78d3_a690_99fb02b9904b -->|method| 6c95b6ea_792a_d20a_be2e_c1e99bb857a8
  235983dc_4462_6225_53dd_dc3b5781d1e5["Object()"]
  2483babf_38af_78d3_a690_99fb02b9904b -->|method| 235983dc_4462_6225_53dd_dc3b5781d1e5

Relationship Graph

Source Code

codec-memcache/src/main/java/io/netty/handler/codec/memcache/AbstractMemcacheObjectEncoder.java lines 35–120

@UnstableApi
public abstract class AbstractMemcacheObjectEncoder<M extends MemcacheMessage> extends MessageToMessageEncoder<Object> {

    private boolean expectingMoreContent;

    public AbstractMemcacheObjectEncoder() {
        super(Object.class);
    }

    @Override
    protected void encode(ChannelHandlerContext ctx, Object msg, List<Object> out) throws Exception {
        if (msg instanceof MemcacheMessage) {
            if (expectingMoreContent) {
                throw new IllegalStateException("unexpected message type: " + StringUtil.simpleClassName(msg));
            }

            @SuppressWarnings({ "unchecked", "CastConflictsWithInstanceof" })
            final M m = (M) msg;
            out.add(encodeMessage(ctx, m));
        }

        if (msg instanceof MemcacheContent || msg instanceof ByteBuf || msg instanceof FileRegion) {
            int contentLength = contentLength(msg);
            if (contentLength > 0) {
                out.add(encodeAndRetain(msg));
            } else {
                out.add(Unpooled.EMPTY_BUFFER);
            }

            expectingMoreContent = !(msg instanceof LastMemcacheContent);
        }
    }

    @Override
    public boolean acceptOutboundMessage(Object msg) throws Exception {
        return msg instanceof MemcacheObject || msg instanceof ByteBuf || msg instanceof FileRegion;
    }

    /**
     * Take the given {@link MemcacheMessage} and encode it into a writable {@link ByteBuf}.
     *
     * @param ctx the channel handler context.
     * @param msg the message to encode.
     * @return the {@link ByteBuf} representation of the message.
     */
    protected abstract ByteBuf encodeMessage(ChannelHandlerContext ctx, M msg);

    /**
     * Determine the content length of the given object.
     *
     * @param msg the object to determine the length of.
     * @return the determined content length.
     */
    private static int contentLength(Object msg) {
        if (msg instanceof MemcacheContent) {
            return ((MemcacheContent) msg).content().readableBytes();
        }
        if (msg instanceof ByteBuf) {
            return ((ByteBuf) msg).readableBytes();
        }
        if (msg instanceof FileRegion) {
            return (int) ((FileRegion) msg).count();
        }
        throw new IllegalStateException("unexpected message type: " + StringUtil.simpleClassName(msg));
    }

    /**
     * Encode the content, depending on the object type.
     *
     * @param msg the object to encode.
     * @return the encoded object.
     */
    private static Object encodeAndRetain(Object msg) {
        if (msg instanceof ByteBuf) {
            return ((ByteBuf) msg).retain();
        }
        if (msg instanceof MemcacheContent) {
            return ((MemcacheContent) msg).content().retain();
        }
        if (msg instanceof FileRegion) {
            return ((FileRegion) msg).retain();

Frequently Asked Questions

What is the AbstractMemcacheObjectEncoder class?
AbstractMemcacheObjectEncoder is a class in the netty codebase, defined in codec-memcache/src/main/java/io/netty/handler/codec/memcache/AbstractMemcacheObjectEncoder.java.
Where is AbstractMemcacheObjectEncoder defined?
AbstractMemcacheObjectEncoder is defined in codec-memcache/src/main/java/io/netty/handler/codec/memcache/AbstractMemcacheObjectEncoder.java at line 35.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free