package io.netty.channel.nio;

import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
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.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 = " (expected: " + StringUtil.simpleClassName((Class<?>) ByteBuf.class) + ", " + StringUtil.simpleClassName((Class<?>) FileRegion.class) + ')';
    private Runnable flushTask;

    /* JADX INFO: Access modifiers changed from: protected */
    public class NioByteUnsafe extends AbstractNioChannel.AbstractNioUnsafe {
        protected NioByteUnsafe() {
            super();
        }

        private void closeOnRead(ChannelPipeline channelPipeline) {
            if (AbstractNioByteChannel.this.isOpen()) {
                if (!Boolean.TRUE.equals(AbstractNioByteChannel.this.config().getOption(ChannelOption.ALLOW_HALF_CLOSURE))) {
                    close(voidPromise());
                    return;
                }
                AbstractNioByteChannel.this.shutdownInput();
                SelectionKey selectionKey = AbstractNioByteChannel.this.selectionKey();
                selectionKey.interestOps(selectionKey.interestOps() & (AbstractNioByteChannel.this.readInterestOp ^ (-1)));
                channelPipeline.fireUserEventTriggered((Object) ChannelInputShutdownEvent.INSTANCE);
            }
        }

        private void handleReadException(ChannelPipeline channelPipeline, ByteBuf byteBuf, Throwable th, boolean z, 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 (z || (th instanceof IOException)) {
                closeOnRead(channelPipeline);
            }
        }

        /* JADX WARN: Code restructure failed: missing block: B:10:0x0036, code lost:
        
            r2 = true;
         */
        /* JADX WARN: Code restructure failed: missing block: B:14:0x0048, code lost:
        
            r7.readComplete();
            r3.fireChannelReadComplete();
         */
        /* JADX WARN: Code restructure failed: missing block: B:15:0x004e, code lost:
        
            if (r2 == false) goto L17;
         */
        /* JADX WARN: Code restructure failed: missing block: B:16:0x0050, code lost:
        
            closeOnRead(r3);
         */
        /* JADX WARN: Code restructure failed: missing block: B:18:0x0057, code lost:
        
            if (r9.this$0.readPending != false) goto L55;
         */
        /* JADX WARN: Code restructure failed: missing block: B:20:0x005d, code lost:
        
            if (r0.isAutoRead() != false) goto L56;
         */
        /* JADX WARN: Code restructure failed: missing block: B:22:0x0060, code lost:
        
            r1 = move-exception;
         */
        /* JADX WARN: Code restructure failed: missing block: B:23:0x0061, code lost:
        
            r5 = r1;
            r6 = r2;
         */
        /* JADX WARN: Code restructure failed: missing block: B:29:0x006d, code lost:
        
            handleReadException(r3, r4, r5, r6, r7);
         */
        /* JADX WARN: Code restructure failed: missing block: B:35:0x007f, code lost:
        
            return;
         */
        /* JADX WARN: Code restructure failed: missing block: B:36:0x0080, code lost:
        
            r1 = move-exception;
         */
        /* JADX WARN: Code restructure failed: missing block: B:38:0x0085, code lost:
        
            if (r9.this$0.readPending == false) goto L39;
         */
        /* JADX WARN: Code restructure failed: missing block: B:41:0x008d, code lost:
        
            removeReadOp();
         */
        /* JADX WARN: Code restructure failed: missing block: B:43:0x0091, code lost:
        
            throw r1;
         */
        /* JADX WARN: Code restructure failed: missing block: B:55:?, code lost:
        
            return;
         */
        /* JADX WARN: Code restructure failed: missing block: B:56:?, code lost:
        
            return;
         */
        /* JADX WARN: Code restructure failed: missing block: B:57:?, code lost:
        
            return;
         */
        /* JADX WARN: Code restructure failed: missing block: B:58:?, code lost:
        
            return;
         */
        /* JADX WARN: Code restructure failed: missing block: B:7:0x002d, code lost:
        
            r5.release();
         */
        /* JADX WARN: Code restructure failed: missing block: B:9:0x0034, code lost:
        
            if (r7.lastBytesRead() >= 0) goto L47;
         */
        @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
                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)
            L17:
                r2 = 0
                r4 = 0
                io.netty.buffer.ByteBuf r5 = r7.allocate(r1)     // Catch: java.lang.Throwable -> L69
                io.netty.channel.nio.AbstractNioByteChannel r6 = io.netty.channel.nio.AbstractNioByteChannel.this     // Catch: java.lang.Throwable -> L64
                int r6 = r6.doReadBytes(r5)     // Catch: java.lang.Throwable -> L64
                r7.lastBytesRead(r6)     // Catch: java.lang.Throwable -> L64
                int r6 = r7.lastBytesRead()     // Catch: java.lang.Throwable -> L64
                r8 = 1
                if (r6 > 0) goto L38
                r5.release()     // Catch: java.lang.Throwable -> L64
                int r1 = r7.lastBytesRead()     // Catch: java.lang.Throwable -> L69
                if (r1 >= 0) goto L48
                r2 = 1
                goto L48
            L38:
                r7.incMessagesRead(r8)     // Catch: java.lang.Throwable -> L64
                io.netty.channel.nio.AbstractNioByteChannel r6 = io.netty.channel.nio.AbstractNioByteChannel.this     // Catch: java.lang.Throwable -> L64
                r6.readPending = r2     // Catch: java.lang.Throwable -> L64
                r3.fireChannelRead(r5)     // Catch: java.lang.Throwable -> L64
                boolean r5 = r7.continueReading()     // Catch: java.lang.Throwable -> L69
                if (r5 != 0) goto L17
            L48:
                r7.readComplete()     // Catch: java.lang.Throwable -> L60
                r3.fireChannelReadComplete()     // Catch: java.lang.Throwable -> L60
                if (r2 == 0) goto L53
                r9.closeOnRead(r3)     // Catch: java.lang.Throwable -> L60
            L53:
                io.netty.channel.nio.AbstractNioByteChannel r1 = io.netty.channel.nio.AbstractNioByteChannel.this
                boolean r1 = r1.readPending
                if (r1 != 0) goto L7f
                boolean r0 = r0.isAutoRead()
                if (r0 != 0) goto L7f
                goto L7c
            L60:
                r1 = move-exception
                r5 = r1
                r6 = r2
                goto L6c
            L64:
                r1 = move-exception
                r4 = r5
                r6 = 0
                r5 = r1
                goto L6c
            L69:
                r1 = move-exception
                r5 = r1
                r6 = 0
            L6c:
                r2 = r9
                r2.handleReadException(r3, r4, r5, r6, r7)     // Catch: java.lang.Throwable -> L80
                io.netty.channel.nio.AbstractNioByteChannel r1 = io.netty.channel.nio.AbstractNioByteChannel.this
                boolean r1 = r1.readPending
                if (r1 != 0) goto L7f
                boolean r0 = r0.isAutoRead()
                if (r0 != 0) goto L7f
            L7c:
                r9.removeReadOp()
            L7f:
                return
            L80:
                r1 = move-exception
                io.netty.channel.nio.AbstractNioByteChannel r2 = io.netty.channel.nio.AbstractNioByteChannel.this
                boolean r2 = r2.readPending
                if (r2 != 0) goto L90
                boolean r0 = r0.isAutoRead()
                if (r0 != 0) goto L90
                r9.removeReadOp()
            L90:
                goto L92
            L91:
                throw r1
            L92:
                goto L91
            */
            throw new UnsupportedOperationException("Method not decompiled: io.netty.channel.nio.AbstractNioByteChannel.NioByteUnsafe.read():void");
        }
    }

    protected AbstractNioByteChannel(Channel channel, SelectableChannel selectableChannel) {
        super(channel, selectableChannel, 1);
    }

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

    protected abstract int doReadBytes(ByteBuf byteBuf) throws Exception;

    @Override // io.netty.channel.AbstractChannel
    protected void doWrite(ChannelOutboundBuffer channelOutboundBuffer) throws Exception {
        int writeSpinCount = -1;
        boolean z = false;
        while (true) {
            Object objCurrent = channelOutboundBuffer.current();
            if (objCurrent == null) {
                clearOpWrite();
                return;
            }
            long j2 = 0;
            boolean z2 = true;
            if (objCurrent instanceof ByteBuf) {
                ByteBuf byteBuf = (ByteBuf) objCurrent;
                if (byteBuf.readableBytes() != 0) {
                    if (writeSpinCount == -1) {
                        writeSpinCount = config().getWriteSpinCount();
                    }
                    int i2 = writeSpinCount - 1;
                    while (true) {
                        if (i2 < 0) {
                            break;
                        }
                        int iDoWriteBytes = doWriteBytes(byteBuf);
                        if (iDoWriteBytes == 0) {
                            z = true;
                            break;
                        }
                        j2 += (long) iDoWriteBytes;
                        if (!byteBuf.isReadable()) {
                            break;
                        } else {
                            i2--;
                        }
                    }
                    z2 = false;
                    channelOutboundBuffer.progress(j2);
                    if (!z2) {
                        break;
                    } else {
                        channelOutboundBuffer.remove();
                    }
                } else {
                    channelOutboundBuffer.remove();
                }
            } else {
                if (!(objCurrent instanceof FileRegion)) {
                    throw new Error();
                }
                FileRegion fileRegion = (FileRegion) objCurrent;
                boolean z3 = fileRegion.transferred() >= fileRegion.count();
                if (!z3) {
                    if (writeSpinCount == -1) {
                        writeSpinCount = config().getWriteSpinCount();
                    }
                    int i3 = writeSpinCount - 1;
                    long j3 = 0;
                    while (true) {
                        if (i3 < 0) {
                            z2 = z3;
                            break;
                        }
                        long jDoWriteFileRegion = doWriteFileRegion(fileRegion);
                        if (jDoWriteFileRegion == 0) {
                            z2 = z3;
                            z = true;
                            break;
                        } else {
                            j3 += jDoWriteFileRegion;
                            if (fileRegion.transferred() >= fileRegion.count()) {
                                break;
                            } else {
                                i3--;
                            }
                        }
                    }
                    channelOutboundBuffer.progress(j3);
                    z3 = z2;
                }
                if (!z3) {
                    break;
                } else {
                    channelOutboundBuffer.remove();
                }
            }
        }
        incompleteWrite(z);
    }

    protected abstract int doWriteBytes(ByteBuf byteBuf) throws Exception;

    protected abstract long doWriteFileRegion(FileRegion fileRegion) throws Exception;

    @Override // io.netty.channel.AbstractChannel
    protected 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;
        }
        throw new UnsupportedOperationException("unsupported message type: " + StringUtil.simpleClassName(obj) + EXPECTED_TYPES);
    }

    protected final void incompleteWrite(boolean z) {
        if (z) {
            setOpWrite();
            return;
        }
        Runnable runnable = this.flushTask;
        if (runnable == null) {
            runnable = new Runnable() { // from class: io.netty.channel.nio.AbstractNioByteChannel.1
                @Override // java.lang.Runnable
                public void run() {
                    AbstractNioByteChannel.this.flush();
                }
            };
            this.flushTask = runnable;
        }
        eventLoop().execute(runnable);
    }

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

    /* JADX INFO: Access modifiers changed from: protected */
    public abstract ChannelFuture shutdownInput();

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // io.netty.channel.AbstractChannel
    public AbstractNioChannel.AbstractNioUnsafe newUnsafe() {
        return new NioByteUnsafe();
    }
}
