package io.netty.handler.codec;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOutboundHandlerAdapter;
import io.netty.channel.ChannelPromise;
import io.netty.util.ReferenceCountUtil;
import io.netty.util.internal.StringUtil;
import io.netty.util.internal.TypeParameterMatcher;
import java.util.List;

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

    protected MessageToMessageEncoder() {
        this.matcher = TypeParameterMatcher.find(this, MessageToMessageEncoder.class, "I");
    }

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

    protected abstract void encode(ChannelHandlerContext channelHandlerContext, I i, List<Object> list) throws Exception;

    @Override // io.netty.channel.ChannelOutboundHandlerAdapter, io.netty.channel.ChannelOutboundHandler
    public void write(ChannelHandlerContext channelHandlerContext, Object obj, ChannelPromise channelPromise) throws Exception {
        boolean z;
        CodecOutputList codecOutputList = null;
        int i = 0;
        try {
            try {
                if (acceptOutboundMessage(obj)) {
                    try {
                        CodecOutputList codecOutputListNewInstance = CodecOutputList.newInstance();
                        try {
                            encode(channelHandlerContext, obj, codecOutputListNewInstance);
                            ReferenceCountUtil.release(obj);
                            if (codecOutputListNewInstance.isEmpty()) {
                                codecOutputListNewInstance.recycle();
                                throw new EncoderException(StringUtil.simpleClassName(this) + " must produce at least one message.");
                            }
                            codecOutputList = codecOutputListNewInstance;
                        } catch (Throwable th) {
                            ReferenceCountUtil.release(obj);
                            throw th;
                        }
                    } catch (EncoderException e2) {
                        throw e2;
                    } catch (Throwable th2) {
                        th = th2;
                        throw new EncoderException(th);
                    }
                } else {
                    channelHandlerContext.write(obj, channelPromise);
                }
                if (codecOutputList != null) {
                    int size = codecOutputList.size() - 1;
                    if (size == 0) {
                        channelHandlerContext.write(codecOutputList.get(0), channelPromise);
                    } else if (size > 0) {
                        ChannelPromise channelPromiseVoidPromise = channelHandlerContext.voidPromise();
                        z = channelPromise == channelPromiseVoidPromise;
                        while (i < size) {
                            channelHandlerContext.write(codecOutputList.getUnsafe(i), z ? channelPromiseVoidPromise : channelHandlerContext.newPromise());
                            i++;
                        }
                        channelHandlerContext.write(codecOutputList.getUnsafe(size), channelPromise);
                    }
                    codecOutputList.recycle();
                }
            } catch (Throwable th3) {
                if (0 != 0) {
                    int size2 = codecOutputList.size() - 1;
                    if (size2 == 0) {
                        channelHandlerContext.write(codecOutputList.get(0), channelPromise);
                    } else if (size2 > 0) {
                        ChannelPromise channelPromiseVoidPromise2 = channelHandlerContext.voidPromise();
                        z = channelPromise == channelPromiseVoidPromise2;
                        while (i < size2) {
                            channelHandlerContext.write(codecOutputList.getUnsafe(i), z ? channelPromiseVoidPromise2 : channelHandlerContext.newPromise());
                            i++;
                        }
                        channelHandlerContext.write(codecOutputList.getUnsafe(size2), channelPromise);
                    }
                    codecOutputList.recycle();
                }
                throw th3;
            }
        } catch (EncoderException e3) {
            throw e3;
        } catch (Throwable th4) {
            th = th4;
        }
    }

    protected MessageToMessageEncoder(Class<? extends I> cls) {
        this.matcher = TypeParameterMatcher.get(cls);
    }
}
