package io.netty.handler.codec;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOutboundHandlerAdapter;
import io.netty.channel.ChannelPromise;
import io.netty.util.ReferenceCountUtil;
import io.netty.util.ReferenceCounted;
import io.netty.util.internal.TypeParameterMatcher;

/* JADX INFO: loaded from: classes.dex */
public abstract class MessageToByteEncoder<I> extends ChannelOutboundHandlerAdapter {
    private final TypeParameterMatcher matcher;
    private final boolean preferDirect;

    public MessageToByteEncoder() {
        this(true);
    }

    public MessageToByteEncoder(Class<? extends I> cls) {
        this(cls, true);
    }

    public MessageToByteEncoder(Class<? extends I> cls, boolean z2) {
        this.matcher = TypeParameterMatcher.get(cls);
        this.preferDirect = z2;
    }

    public MessageToByteEncoder(boolean z2) {
        this.matcher = TypeParameterMatcher.find(this, MessageToByteEncoder.class, "I");
        this.preferDirect = z2;
    }

    public boolean acceptOutboundMessage(Object obj) {
        return this.matcher.match(obj);
    }

    public ByteBuf allocateBuffer(ChannelHandlerContext channelHandlerContext, I i2, boolean z2) {
        ByteBufAllocator byteBufAllocatorAlloc = channelHandlerContext.alloc();
        return z2 ? byteBufAllocatorAlloc.ioBuffer() : byteBufAllocatorAlloc.heapBuffer();
    }

    public abstract void encode(ChannelHandlerContext channelHandlerContext, I i2, ByteBuf byteBuf);

    public boolean isPreferDirect() {
        return this.preferDirect;
    }

    /* JADX WARN: Multi-variable type inference failed */
    @Override // io.netty.channel.ChannelOutboundHandlerAdapter, io.netty.channel.ChannelOutboundHandler
    public void write(ChannelHandlerContext channelHandlerContext, Object obj, ChannelPromise channelPromise) {
        ReferenceCounted referenceCounted = null;
        try {
            try {
                try {
                    boolean zAcceptOutboundMessage = acceptOutboundMessage(obj);
                    Object obj2 = obj;
                    if (zAcceptOutboundMessage) {
                        ByteBuf byteBufAllocateBuffer = allocateBuffer(channelHandlerContext, obj, this.preferDirect);
                        try {
                            encode(channelHandlerContext, obj, byteBufAllocateBuffer);
                            ReferenceCountUtil.release(obj);
                            if (byteBufAllocateBuffer.isReadable()) {
                                channelHandlerContext.write(byteBufAllocateBuffer, channelPromise);
                                return;
                            } else {
                                byteBufAllocateBuffer.release();
                                obj2 = (I) Unpooled.EMPTY_BUFFER;
                            }
                        } catch (Throwable th) {
                            ReferenceCountUtil.release(obj);
                            throw th;
                        }
                    }
                    channelHandlerContext.write(obj2, channelPromise);
                } catch (Throwable th2) {
                    if (0 != 0) {
                        referenceCounted.release();
                    }
                    throw th2;
                }
            } catch (Throwable th3) {
                throw new EncoderException(th3);
            }
        } catch (EncoderException e2) {
            throw e2;
        }
    }
}
