package a.c.b.z.g;

import a.c.b.a0.c;
import a.c.b.b0.a;
import a.c.b.h;
import a.c.b.i;
import a.c.b.k;
import a.c.b.l;
import a.c.b.s;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.util.Objects;

public class f implements Runnable, Closeable {
    private static final String e = "SocketChannelReadTask";

    /* renamed from: a  reason: collision with root package name */
    private final e f92a;
    private final a b;
    private volatile boolean c = true;
    private InputStream d = null;

    public f(e eVar) {
        Objects.requireNonNull(eVar, "SocketChannel is null");
        this.f92a = eVar;
        this.b = eVar.p();
    }

    private int a(ReadableByteChannel readableByteChannel, ByteBuffer byteBuffer, int i) throws IOException {
        StringBuilder append;
        String str;
        if (byteBuffer == null || i == 0) {
            return 0;
        }
        c a2 = k.a();
        byteBuffer.limit(i);
        int read = readableByteChannel.read(byteBuffer);
        if (read == -1) {
            append = new StringBuilder().append(this.f92a.a());
            str = " read channel closed.";
        } else if (read < 0 || read > i) {
            throw new l("Read bytes too long or too short: " + read + ", expect: " + i);
        } else {
            while (read < i) {
                int read2 = readableByteChannel.read(byteBuffer);
                if (read2 == -1) {
                    append = new StringBuilder().append(this.f92a.a());
                    str = " channel read channel closed.";
                } else {
                    read += read2;
                }
            }
            if (read == i) {
                return read;
            }
            throw new l("Read bytes length less than buff length: " + read);
        }
        a2.a(e, append.append(str).toString());
        return -1;
    }

    private ByteBuffer a(int i, ByteBuffer byteBuffer) {
        if (i >= byteBuffer.capacity() || !this.f92a.v()) {
            return ByteBuffer.allocate(i);
        }
        byteBuffer.clear();
        byteBuffer.limit(i);
        return byteBuffer;
    }

    /* access modifiers changed from: private */
    public /* synthetic */ void a(c cVar, s sVar) {
        if (cVar.a()) {
            cVar.a(e, sVar.a());
        }
        if (sVar.i() == i.RES) {
            b.b().a(sVar);
            return;
        }
        this.f92a.a(sVar);
        sVar.t();
    }

    private InputStream j() {
        c a2 = k.a();
        if (!this.f92a.b()) {
            a2.b(e, "Start read thread error: socket channel is not connected");
            return null;
        }
        try {
            Socket o = this.f92a.o();
            if (o != null) {
                return o.getInputStream();
            }
            a2.b(e, "Start read thread error: socket is null");
            return null;
        } catch (IOException e2) {
            a2.b(e, "Start read thread error: " + e2.getMessage(), e2);
            return null;
        }
    }

    private void l() {
        if (this.d != null) {
            c a2 = k.a();
            try {
                int available = this.d.available();
                if (available > 0) {
                    a2.d(e, "Skip data len: " + this.d.skip((long) available));
                }
            } catch (IOException e2) {
                a2.b(e, "Skip data len error: " + e2.getMessage(), e2);
            }
        }
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable
    public synchronized void close() throws IOException {
        if (this.c) {
            InputStream inputStream = this.d;
            if (inputStream != null) {
                inputStream.close();
            }
            this.c = false;
        }
    }

    public boolean k() {
        return this.b != null;
    }

    public void run() {
        s sVar;
        s sVar2;
        c a2 = k.a();
        InputStream j = j();
        this.d = j;
        if (j != null) {
            ReadableByteChannel newChannel = Channels.newChannel(j);
            ByteBuffer allocate = ByteBuffer.allocate(20);
            h i = h.i();
            ByteBuffer allocate2 = ByteBuffer.allocate(131072);
            while (true) {
                if (!this.c) {
                    break;
                }
                boolean v = this.f92a.v();
                try {
                    allocate.clear();
                    if (a(newChannel, allocate, 20) == -1) {
                        break;
                    }
                    allocate.flip();
                    i.a(allocate.array());
                    int d2 = i.d() - 20;
                    if (d2 < 0) {
                        a2.b(e, "Read data parse not normal, find body len < 0");
                        l();
                    } else {
                        ByteBuffer a3 = a(d2, allocate2);
                        if (a(newChannel, a3, d2) == -1) {
                            break;
                        }
                        a3.flip();
                        if (v) {
                            sVar = s.s();
                            h a4 = sVar.g().a(i);
                            if (k()) {
                                ByteBuffer a5 = this.b.a(a3, false);
                                a4.c(a5.remaining());
                                sVar.a(a5, a5.remaining());
                            } else {
                                sVar.a(a3, d2);
                            }
                        } else {
                            h a6 = h.i().a(i);
                            if (k()) {
                                ByteBuffer a7 = this.b.a(a3);
                                sVar2 = new s(a6, a7, a7.remaining());
                                a6.c(a7.remaining());
                            } else {
                                sVar2 = new s(a6, a3, d2);
                            }
                            sVar = sVar2;
                        }
                        k.a(this.f92a.g()).submit(new Runnable(a2, sVar) {
                            /* class a.c.b.z.g.$$Lambda$f$UmSWMFNjkH1o6iNL2bwQJFl28M */
                            public final /* synthetic */ c f$1;
                            public final /* synthetic */ s f$2;

                            {
                                this.f$1 = r2;
                                this.f$2 = r3;
                            }

                            public final void run() {
                                f.m8lambda$UmSWMFNjkH1o6iNL2bwQJFl28M(f.this, this.f$1, this.f$2);
                            }
                        });
                    }
                } catch (IOException e2) {
                    if (this.c) {
                        a2.b(e, "Read data in channel: " + this.f92a.a() + " Error.", e2);
                    }
                } catch (l e3) {
                    a2.b(e, "parse message error: " + e3.getMessage(), e3);
                    l();
                }
            }
            if (this.c) {
                try {
                    this.f92a.close();
                } catch (IOException e4) {
                    a2.b(e, this.f92a.a() + "read channel close failed.", e4);
                }
            }
        }
    }
}
