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.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));
                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:7:0x002d, code lost:
        
            r5.release();
         */
        /* JADX WARN: Code restructure failed: missing block: B:9:0x0034, code lost:
        
            if (r7.lastBytesRead() >= 0) goto L14;
         */
        @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 -> L65
                io.netty.channel.nio.AbstractNioByteChannel r6 = io.netty.channel.nio.AbstractNioByteChannel.this     // Catch: java.lang.Throwable -> L60
                int r6 = r6.doReadBytes(r5)     // Catch: java.lang.Throwable -> L60
                r7.lastBytesRead(r6)     // Catch: java.lang.Throwable -> L60
                int r6 = r7.lastBytesRead()     // Catch: java.lang.Throwable -> L60
                r8 = 1
                if (r6 > 0) goto L38
                r5.release()     // Catch: java.lang.Throwable -> L60
                int r1 = r7.lastBytesRead()     // Catch: java.lang.Throwable -> L65
                if (r1 >= 0) goto L48
                r2 = r8
                goto L48
            L38:
                r7.incMessagesRead(r8)     // Catch: java.lang.Throwable -> L60
                io.netty.channel.nio.AbstractNioByteChannel r6 = io.netty.channel.nio.AbstractNioByteChannel.this     // Catch: java.lang.Throwable -> L60
                r6.readPending = r2     // Catch: java.lang.Throwable -> L60
                r3.fireChannelRead(r5)     // Catch: java.lang.Throwable -> L60
                boolean r5 = r7.continueReading()     // Catch: java.lang.Throwable -> L65
                if (r5 != 0) goto L17
            L48:
                r7.readComplete()     // Catch: java.lang.Throwable -> L65
                r3.fireChannelReadComplete()     // Catch: java.lang.Throwable -> L65
                if (r2 == 0) goto L53
                r9.closeOnRead(r3)     // Catch: java.lang.Throwable -> L65
            L53:
                io.netty.channel.nio.AbstractNioByteChannel r1 = io.netty.channel.nio.AbstractNioByteChannel.this
                boolean r1 = r1.readPending
                if (r1 != 0) goto L7b
                boolean r0 = r0.isAutoRead()
                if (r0 != 0) goto L7b
                goto L78
            L60:
                r1 = move-exception
                r6 = r2
                r4 = r5
                r5 = r1
                goto L68
            L65:
                r1 = move-exception
                r5 = r1
                r6 = r2
            L68:
                r2 = r9
                r2.handleReadException(r3, r4, r5, r6, r7)     // Catch: java.lang.Throwable -> L7c
                io.netty.channel.nio.AbstractNioByteChannel r1 = io.netty.channel.nio.AbstractNioByteChannel.this
                boolean r1 = r1.readPending
                if (r1 != 0) goto L7b
                boolean r0 = r0.isAutoRead()
                if (r0 != 0) goto L7b
            L78:
                r9.removeReadOp()
            L7b:
                return
            L7c:
                r1 = move-exception
                io.netty.channel.nio.AbstractNioByteChannel r2 = io.netty.channel.nio.AbstractNioByteChannel.this
                boolean r2 = r2.readPending
                if (r2 != 0) goto L8c
                boolean r0 = r0.isAutoRead()
                if (r0 != 0) goto L8c
                r9.removeReadOp()
            L8c:
                throw r1
            */
            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;

    /* JADX WARN: Code restructure failed: missing block: B:45:0x0092, code lost:
    
        r10 = r7;
     */
    @Override // io.netty.channel.AbstractChannel
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    protected void doWrite(io.netty.channel.ChannelOutboundBuffer r19) throws java.lang.Exception {
        /*
            r18 = this;
            r0 = r18
            r1 = r19
            r2 = 0
            r3 = -1
            r5 = r2
            r4 = r3
        L8:
            java.lang.Object r6 = r19.current()
            if (r6 != 0) goto L12
            r18.clearOpWrite()
            return
        L12:
            boolean r7 = r6 instanceof io.netty.buffer.ByteBuf
            r8 = 0
            r10 = 1
            if (r7 == 0) goto L51
            io.netty.buffer.ByteBuf r6 = (io.netty.buffer.ByteBuf) r6
            int r7 = r6.readableBytes()
            if (r7 != 0) goto L25
            r19.remove()
            goto L8
        L25:
            if (r4 != r3) goto L2f
            io.netty.channel.ChannelConfig r4 = r18.config()
            int r4 = r4.getWriteSpinCount()
        L2f:
            int r7 = r4 + (-1)
        L31:
            if (r7 < 0) goto L47
            int r11 = r0.doWriteBytes(r6)
            if (r11 != 0) goto L3b
            r5 = r10
            goto L47
        L3b:
            long r11 = (long) r11
            long r8 = r8 + r11
            boolean r11 = r6.isReadable()
            if (r11 != 0) goto L44
            goto L48
        L44:
            int r7 = r7 + (-1)
            goto L31
        L47:
            r10 = r2
        L48:
            r1.progress(r8)
            if (r10 == 0) goto L9e
            r19.remove()
            goto L8
        L51:
            boolean r7 = r6 instanceof io.netty.channel.FileRegion
            if (r7 == 0) goto La2
            io.netty.channel.FileRegion r6 = (io.netty.channel.FileRegion) r6
            long r11 = r6.transferred()
            long r13 = r6.count()
            int r7 = (r11 > r13 ? 1 : (r11 == r13 ? 0 : -1))
            if (r7 < 0) goto L65
            r7 = r10
            goto L66
        L65:
            r7 = r2
        L66:
            if (r7 != 0) goto L97
            if (r4 != r3) goto L72
            io.netty.channel.ChannelConfig r4 = r18.config()
            int r4 = r4.getWriteSpinCount()
        L72:
            int r11 = r4 + (-1)
            r12 = r8
        L75:
            if (r11 < 0) goto L92
            long r14 = r0.doWriteFileRegion(r6)
            int r16 = (r14 > r8 ? 1 : (r14 == r8 ? 0 : -1))
            if (r16 != 0) goto L81
            r5 = r10
            goto L92
        L81:
            long r12 = r12 + r14
            long r14 = r6.transferred()
            long r16 = r6.count()
            int r14 = (r14 > r16 ? 1 : (r14 == r16 ? 0 : -1))
            if (r14 < 0) goto L8f
            goto L93
        L8f:
            int r11 = r11 + (-1)
            goto L75
        L92:
            r10 = r7
        L93:
            r1.progress(r12)
            r7 = r10
        L97:
            if (r7 == 0) goto L9e
            r19.remove()
            goto L8
        L9e:
            r0.incompleteWrite(r5)
            return
        La2:
            java.lang.Error r1 = new java.lang.Error
            r1.<init>()
            throw r1
        */
        throw new UnsupportedOperationException("Method not decompiled: io.netty.channel.nio.AbstractNioByteChannel.doWrite(io.netty.channel.ChannelOutboundBuffer):void");
    }

    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();
    }
}
