package io.netty.channel.nio;

import g.a.a.a.a;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelConfig;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelMetadata;
import io.netty.channel.ChannelOutboundBuffer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.FileRegion;
import io.netty.channel.RecvByteBufAllocator;
import io.netty.channel.nio.AbstractNioChannel;
import io.netty.channel.socket.ChannelInputShutdownEvent;
import io.netty.channel.socket.ChannelInputShutdownReadComplete;
import io.netty.channel.socket.SocketChannelConfig;
import io.netty.util.internal.StringUtil;
import java.io.IOException;
import java.nio.channels.SelectableChannel;
import java.nio.channels.SelectionKey;

/* JADX INFO: loaded from: classes.dex */
public abstract class AbstractNioByteChannel extends AbstractNioChannel {
    private static final String EXPECTED_TYPES;
    private static final ChannelMetadata METADATA = new ChannelMetadata(false, 16);
    private final Runnable flushTask;
    private boolean inputClosedSeenErrorOnRead;

    public class NioByteUnsafe extends AbstractNioChannel.AbstractNioUnsafe {
        public NioByteUnsafe() {
            super();
        }

        private void closeOnRead(ChannelPipeline channelPipeline) {
            Object obj;
            if (AbstractNioByteChannel.this.isInputShutdown0()) {
                AbstractNioByteChannel.this.inputClosedSeenErrorOnRead = true;
                obj = ChannelInputShutdownReadComplete.INSTANCE;
            } else if (!AbstractNioByteChannel.isAllowHalfClosure(AbstractNioByteChannel.this.config())) {
                close(voidPromise());
                return;
            } else {
                AbstractNioByteChannel.this.shutdownInput();
                obj = ChannelInputShutdownEvent.INSTANCE;
            }
            channelPipeline.fireUserEventTriggered(obj);
        }

        private void handleReadException(ChannelPipeline channelPipeline, ByteBuf byteBuf, Throwable th, boolean z2, RecvByteBufAllocator.Handle handle) {
            if (byteBuf != null) {
                if (byteBuf.isReadable()) {
                    AbstractNioByteChannel.this.readPending = false;
                    channelPipeline.fireChannelRead((Object) byteBuf);
                } else {
                    byteBuf.release();
                }
            }
            handle.readComplete();
            channelPipeline.fireChannelReadComplete();
            channelPipeline.fireExceptionCaught(th);
            if (z2 || (th instanceof IOException)) {
                closeOnRead(channelPipeline);
            }
        }

        /* JADX WARN: Code restructure failed: missing block: B:11:0x003b, code lost:
        
            r5.release();
         */
        /* JADX WARN: Code restructure failed: missing block: B:13:0x0042, code lost:
        
            if (r7.lastBytesRead() >= 0) goto L15;
         */
        /* JADX WARN: Code restructure failed: missing block: B:15:0x0045, code lost:
        
            r8 = false;
         */
        /* JADX WARN: Code restructure failed: missing block: B:16:0x0046, code lost:
        
            if (r8 == false) goto L21;
         */
        /* JADX WARN: Code restructure failed: missing block: B:17:0x0048, code lost:
        
            r9.this$0.readPending = false;
         */
        /* JADX WARN: Code restructure failed: missing block: B:19:0x004d, code lost:
        
            r1 = move-exception;
         */
        /* JADX WARN: Code restructure failed: missing block: B:20:0x004e, code lost:
        
            r5 = r1;
            r4 = null;
            r6 = r8;
         */
        /* JADX WARN: Code restructure failed: missing block: B:21:0x0052, code lost:
        
            r4 = r8;
         */
        /* JADX WARN: Code restructure failed: missing block: B:38:0x0086, code lost:
        
            handleReadException(r3, r4, r5, r6, r7);
         */
        /* JADX WARN: Code restructure failed: missing block: B:45:0x0099, code lost:
        
            r1 = move-exception;
         */
        /* JADX WARN: Code restructure failed: missing block: B:47:0x009e, code lost:
        
            if (r9.this$0.readPending == false) goto L48;
         */
        /* JADX WARN: Code restructure failed: missing block: B:50:0x00a6, code lost:
        
            removeReadOp();
         */
        /* JADX WARN: Code restructure failed: missing block: B:51:0x00a9, code lost:
        
            throw r1;
         */
        /* JADX WARN: Code restructure failed: missing block: B:65:?, code lost:
        
            return;
         */
        /* JADX WARN: Code restructure failed: missing block: B:66:?, code lost:
        
            return;
         */
        @Override // io.netty.channel.nio.AbstractNioChannel.NioUnsafe
        /*
            Code decompiled incorrectly, please refer to instructions dump.
            To view partially-correct add '--show-bad-code' argument
        */
        public final void read() {
            /*
                r9 = this;
                io.netty.channel.nio.AbstractNioByteChannel r0 = io.netty.channel.nio.AbstractNioByteChannel.this
                io.netty.channel.ChannelConfig r0 = r0.config()
                io.netty.channel.nio.AbstractNioByteChannel r1 = io.netty.channel.nio.AbstractNioByteChannel.this
                boolean r1 = r1.shouldBreakReadReady(r0)
                if (r1 == 0) goto L14
                io.netty.channel.nio.AbstractNioByteChannel r0 = io.netty.channel.nio.AbstractNioByteChannel.this
                r0.clearReadPending()
                return
            L14:
                io.netty.channel.nio.AbstractNioByteChannel r1 = io.netty.channel.nio.AbstractNioByteChannel.this
                io.netty.channel.ChannelPipeline r3 = r1.pipeline()
                io.netty.buffer.ByteBufAllocator r1 = r0.getAllocator()
                io.netty.channel.RecvByteBufAllocator$Handle r7 = r9.recvBufAllocHandle()
                r7.reset(r0)
            L25:
                r2 = 0
                r4 = 0
                io.netty.buffer.ByteBuf r5 = r7.allocate(r1)     // Catch: java.lang.Throwable -> L81
                io.netty.channel.nio.AbstractNioByteChannel r6 = io.netty.channel.nio.AbstractNioByteChannel.this     // Catch: java.lang.Throwable -> L7c
                int r6 = r6.doReadBytes(r5)     // Catch: java.lang.Throwable -> L7c
                r7.lastBytesRead(r6)     // Catch: java.lang.Throwable -> L7c
                int r6 = r7.lastBytesRead()     // Catch: java.lang.Throwable -> L7c
                r8 = 1
                if (r6 > 0) goto L54
                r5.release()     // Catch: java.lang.Throwable -> L7c
                int r1 = r7.lastBytesRead()     // Catch: java.lang.Throwable -> L81
                if (r1 >= 0) goto L45
                goto L46
            L45:
                r8 = r4
            L46:
                if (r8 == 0) goto L52
                io.netty.channel.nio.AbstractNioByteChannel r1 = io.netty.channel.nio.AbstractNioByteChannel.this     // Catch: java.lang.Throwable -> L4d
                r1.readPending = r4     // Catch: java.lang.Throwable -> L4d
                goto L52
            L4d:
                r1 = move-exception
                r5 = r1
                r4 = r2
                r6 = r8
                goto L85
            L52:
                r4 = r8
                goto L64
            L54:
                r7.incMessagesRead(r8)     // Catch: java.lang.Throwable -> L7c
                io.netty.channel.nio.AbstractNioByteChannel r6 = io.netty.channel.nio.AbstractNioByteChannel.this     // Catch: java.lang.Throwable -> L7c
                r6.readPending = r4     // Catch: java.lang.Throwable -> L7c
                r3.fireChannelRead(r5)     // Catch: java.lang.Throwable -> L7c
                boolean r5 = r7.continueReading()     // Catch: java.lang.Throwable -> L81
                if (r5 != 0) goto L25
            L64:
                r7.readComplete()     // Catch: java.lang.Throwable -> L81
                r3.fireChannelReadComplete()     // Catch: java.lang.Throwable -> L81
                if (r4 == 0) goto L6f
                r9.closeOnRead(r3)     // Catch: java.lang.Throwable -> L81
            L6f:
                io.netty.channel.nio.AbstractNioByteChannel r1 = io.netty.channel.nio.AbstractNioByteChannel.this
                boolean r1 = r1.readPending
                if (r1 != 0) goto L98
                boolean r0 = r0.isAutoRead()
                if (r0 != 0) goto L98
                goto L95
            L7c:
                r1 = move-exception
                r6 = r4
                r4 = r5
                r5 = r1
                goto L85
            L81:
                r1 = move-exception
                r5 = r1
                r6 = r4
                r4 = r2
            L85:
                r2 = r9
                r2.handleReadException(r3, r4, r5, r6, r7)     // Catch: java.lang.Throwable -> L99
                io.netty.channel.nio.AbstractNioByteChannel r1 = io.netty.channel.nio.AbstractNioByteChannel.this
                boolean r1 = r1.readPending
                if (r1 != 0) goto L98
                boolean r0 = r0.isAutoRead()
                if (r0 != 0) goto L98
            L95:
                r9.removeReadOp()
            L98:
                return
            L99:
                r1 = move-exception
                io.netty.channel.nio.AbstractNioByteChannel r2 = io.netty.channel.nio.AbstractNioByteChannel.this
                boolean r2 = r2.readPending
                if (r2 != 0) goto La9
                boolean r0 = r0.isAutoRead()
                if (r0 != 0) goto La9
                r9.removeReadOp()
            La9:
                throw r1
            */
            throw new UnsupportedOperationException("Method not decompiled: io.netty.channel.nio.AbstractNioByteChannel.NioByteUnsafe.read():void");
        }
    }

    static {
        StringBuilder sbF = a.F(" (expected: ");
        sbF.append(StringUtil.simpleClassName((Class<?>) ByteBuf.class));
        sbF.append(", ");
        sbF.append(StringUtil.simpleClassName((Class<?>) FileRegion.class));
        sbF.append(')');
        EXPECTED_TYPES = sbF.toString();
    }

    public AbstractNioByteChannel(Channel channel, SelectableChannel selectableChannel) {
        super(channel, selectableChannel, 1);
        this.flushTask = new Runnable() { // from class: io.netty.channel.nio.AbstractNioByteChannel.1
            @Override // java.lang.Runnable
            public void run() {
                ((AbstractNioChannel.AbstractNioUnsafe) AbstractNioByteChannel.this.unsafe()).flush0();
            }
        };
    }

    private int doWriteInternal(ChannelOutboundBuffer channelOutboundBuffer, Object obj) {
        if (obj instanceof ByteBuf) {
            ByteBuf byteBuf = (ByteBuf) obj;
            if (!byteBuf.isReadable()) {
                channelOutboundBuffer.remove();
                return 0;
            }
            int iDoWriteBytes = doWriteBytes(byteBuf);
            if (iDoWriteBytes <= 0) {
                return Integer.MAX_VALUE;
            }
            channelOutboundBuffer.progress(iDoWriteBytes);
            if (!byteBuf.isReadable()) {
                channelOutboundBuffer.remove();
            }
            return 1;
        }
        if (!(obj instanceof FileRegion)) {
            throw new Error();
        }
        FileRegion fileRegion = (FileRegion) obj;
        if (fileRegion.transferred() >= fileRegion.count()) {
            channelOutboundBuffer.remove();
            return 0;
        }
        long jDoWriteFileRegion = doWriteFileRegion(fileRegion);
        if (jDoWriteFileRegion <= 0) {
            return Integer.MAX_VALUE;
        }
        channelOutboundBuffer.progress(jDoWriteFileRegion);
        if (fileRegion.transferred() >= fileRegion.count()) {
            channelOutboundBuffer.remove();
        }
        return 1;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static boolean isAllowHalfClosure(ChannelConfig channelConfig) {
        return (channelConfig instanceof SocketChannelConfig) && ((SocketChannelConfig) channelConfig).isAllowHalfClosure();
    }

    public final void clearOpWrite() {
        SelectionKey selectionKey = selectionKey();
        if (selectionKey.isValid()) {
            int iInterestOps = selectionKey.interestOps();
            if ((iInterestOps & 4) != 0) {
                selectionKey.interestOps(iInterestOps & (-5));
            }
        }
    }

    public abstract int doReadBytes(ByteBuf byteBuf);

    @Override // io.netty.channel.AbstractChannel
    public void doWrite(ChannelOutboundBuffer channelOutboundBuffer) {
        int writeSpinCount = config().getWriteSpinCount();
        do {
            Object objCurrent = channelOutboundBuffer.current();
            if (objCurrent == null) {
                clearOpWrite();
                return;
            }
            writeSpinCount -= doWriteInternal(channelOutboundBuffer, objCurrent);
        } while (writeSpinCount > 0);
        incompleteWrite(writeSpinCount < 0);
    }

    public final int doWrite0(ChannelOutboundBuffer channelOutboundBuffer) {
        if (channelOutboundBuffer.current() == null) {
            return 0;
        }
        return doWriteInternal(channelOutboundBuffer, channelOutboundBuffer.current());
    }

    public abstract int doWriteBytes(ByteBuf byteBuf);

    public abstract long doWriteFileRegion(FileRegion fileRegion);

    @Override // io.netty.channel.AbstractChannel
    public final Object filterOutboundMessage(Object obj) {
        if (obj instanceof ByteBuf) {
            ByteBuf byteBuf = (ByteBuf) obj;
            return byteBuf.isDirect() ? obj : newDirectBuffer(byteBuf);
        }
        if (obj instanceof FileRegion) {
            return obj;
        }
        StringBuilder sbF = a.F("unsupported message type: ");
        sbF.append(StringUtil.simpleClassName(obj));
        sbF.append(EXPECTED_TYPES);
        throw new UnsupportedOperationException(sbF.toString());
    }

    public final void incompleteWrite(boolean z2) {
        if (z2) {
            setOpWrite();
        } else {
            clearOpWrite();
            eventLoop().execute(this.flushTask);
        }
    }

    public boolean isInputShutdown0() {
        return false;
    }

    @Override // io.netty.channel.Channel
    public ChannelMetadata metadata() {
        return METADATA;
    }

    @Override // io.netty.channel.AbstractChannel
    public AbstractNioChannel.AbstractNioUnsafe newUnsafe() {
        return new NioByteUnsafe();
    }

    public final void setOpWrite() {
        SelectionKey selectionKey = selectionKey();
        if (selectionKey.isValid()) {
            int iInterestOps = selectionKey.interestOps();
            if ((iInterestOps & 4) == 0) {
                selectionKey.interestOps(iInterestOps | 4);
            }
        }
    }

    public final boolean shouldBreakReadReady(ChannelConfig channelConfig) {
        return isInputShutdown0() && (this.inputClosedSeenErrorOnRead || !isAllowHalfClosure(channelConfig));
    }

    public abstract ChannelFuture shutdownInput();
}
