package io.netty.handler.codec.smtp;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageEncoder;
import java.util.Iterator;
import java.util.List;
import java.util.RandomAccess;

/* JADX INFO: loaded from: classes.dex */
public final class SmtpRequestEncoder extends MessageToMessageEncoder<Object> {
    private static final int CRLF_SHORT = 3338;
    private static final ByteBuf DOT_CRLF_BUFFER = Unpooled.unreleasableBuffer(Unpooled.directBuffer(3).writeByte(46).writeByte(13).writeByte(10));
    private static final byte SP = 32;
    private boolean contentExpected;

    private static void writeParameters(List<CharSequence> list, ByteBuf byteBuf) {
        if (list.isEmpty()) {
            return;
        }
        byteBuf.writeByte(32);
        if (list instanceof RandomAccess) {
            int size = list.size() - 1;
            for (int i2 = 0; i2 < size; i2++) {
                ByteBufUtil.writeAscii(byteBuf, list.get(i2));
                byteBuf.writeByte(32);
            }
            ByteBufUtil.writeAscii(byteBuf, list.get(size));
            return;
        }
        Iterator<CharSequence> it = list.iterator();
        while (true) {
            ByteBufUtil.writeAscii(byteBuf, it.next());
            if (!it.hasNext()) {
                return;
            } else {
                byteBuf.writeByte(32);
            }
        }
    }

    @Override // io.netty.handler.codec.MessageToMessageEncoder
    public boolean acceptOutboundMessage(Object obj) {
        return (obj instanceof SmtpRequest) || (obj instanceof SmtpContent);
    }

    @Override // io.netty.handler.codec.MessageToMessageEncoder
    public void encode(ChannelHandlerContext channelHandlerContext, Object obj, List<Object> list) throws Throwable {
        boolean z2 = false;
        if (obj instanceof SmtpRequest) {
            SmtpRequest smtpRequest = (SmtpRequest) obj;
            if (this.contentExpected) {
                if (!smtpRequest.command().equals(SmtpCommand.RSET)) {
                    throw new IllegalStateException("SmtpContent expected");
                }
                this.contentExpected = false;
            }
            ByteBuf byteBufBuffer = channelHandlerContext.alloc().buffer();
            try {
                smtpRequest.command().encode(byteBufBuffer);
                writeParameters(smtpRequest.parameters(), byteBufBuffer);
                ByteBufUtil.writeShortBE(byteBufBuffer, 3338);
                list.add(byteBufBuffer);
            } catch (Throwable th) {
                th = th;
                z2 = true;
            }
            try {
                if (smtpRequest.command().isContentExpected()) {
                    this.contentExpected = true;
                }
            } catch (Throwable th2) {
                th = th2;
                if (z2) {
                    byteBufBuffer.release();
                }
                throw th;
            }
        }
        if (obj instanceof SmtpContent) {
            if (!this.contentExpected) {
                throw new IllegalStateException("No SmtpContent expected");
            }
            list.add(((SmtpContent) obj).content().retain());
            if (obj instanceof LastSmtpContent) {
                list.add(DOT_CRLF_BUFFER.retainedDuplicate());
                this.contentExpected = false;
            }
        }
    }
}
