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;

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

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

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

    public abstract void encode(ChannelHandlerContext channelHandlerContext, I i2, List<Object> list);

    @Override // io.netty.channel.ChannelOutboundHandlerAdapter, io.netty.channel.ChannelOutboundHandler
    public void write(ChannelHandlerContext channelHandlerContext, Object obj, ChannelPromise channelPromise) {
        Object unsafe;
        boolean z2;
        Object unsafe2;
        CodecOutputList codecOutputList = null;
        int i2 = 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) {
                        if (size > 0) {
                            ChannelPromise channelPromiseVoidPromise = channelHandlerContext.voidPromise();
                            z2 = channelPromise == channelPromiseVoidPromise;
                            while (i2 < size) {
                                channelHandlerContext.write(codecOutputList.getUnsafe(i2), z2 ? channelPromiseVoidPromise : channelHandlerContext.newPromise());
                                i2++;
                            }
                            unsafe2 = codecOutputList.getUnsafe(size);
                        }
                        codecOutputList.recycle();
                    }
                    unsafe2 = codecOutputList.get(0);
                    channelHandlerContext.write(unsafe2, channelPromise);
                    codecOutputList.recycle();
                }
            } catch (Throwable th3) {
                if (0 != 0) {
                    int size2 = codecOutputList.size() - 1;
                    if (size2 != 0) {
                        if (size2 > 0) {
                            ChannelPromise channelPromiseVoidPromise2 = channelHandlerContext.voidPromise();
                            z2 = channelPromise == channelPromiseVoidPromise2;
                            while (i2 < size2) {
                                channelHandlerContext.write(codecOutputList.getUnsafe(i2), z2 ? channelPromiseVoidPromise2 : channelHandlerContext.newPromise());
                                i2++;
                            }
                            unsafe = codecOutputList.getUnsafe(size2);
                        }
                        codecOutputList.recycle();
                    } else {
                        unsafe = codecOutputList.get(0);
                    }
                    channelHandlerContext.write(unsafe, channelPromise);
                    codecOutputList.recycle();
                }
                throw th3;
            }
        } catch (EncoderException e3) {
            throw e3;
        } catch (Throwable th4) {
            th = th4;
        }
    }
}
