package h;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.logging.Logger;

/* JADX INFO: loaded from: classes.dex */
public final class p {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public static final Logger f6564a = Logger.getLogger(p.class.getName());

    public class a implements x {

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        public final /* synthetic */ y f6565b;

        /* JADX INFO: renamed from: c, reason: collision with root package name */
        public final /* synthetic */ InputStream f6566c;

        public a(y yVar, InputStream inputStream) {
            this.f6565b = yVar;
            this.f6566c = inputStream;
        }

        @Override // h.x, java.io.Closeable, java.lang.AutoCloseable
        public void close() throws IOException {
            this.f6566c.close();
        }

        @Override // h.x
        public y e() {
            return this.f6565b;
        }

        public String toString() {
            StringBuilder sbC = a.b.c.a.a.C("source(");
            sbC.append(this.f6566c);
            sbC.append(")");
            return sbC.toString();
        }

        @Override // h.x
        public long v(f fVar, long j2) throws IOException {
            if (j2 < 0) {
                throw new IllegalArgumentException("byteCount < 0: " + j2);
            }
            if (j2 == 0) {
                return 0L;
            }
            try {
                this.f6565b.f();
                t tVarV = fVar.V(1);
                int i2 = this.f6566c.read(tVarV.f6574a, tVarV.f6576c, (int) Math.min(j2, 8192 - tVarV.f6576c));
                if (i2 == -1) {
                    return -1L;
                }
                tVarV.f6576c += i2;
                long j3 = i2;
                fVar.f6543d += j3;
                return j3;
            } catch (AssertionError e2) {
                if (p.a(e2)) {
                    throw new IOException(e2);
                }
                throw e2;
            }
        }
    }

    public static boolean a(AssertionError assertionError) {
        return (assertionError.getCause() == null || assertionError.getMessage() == null || !assertionError.getMessage().contains("getsockname failed")) ? false : true;
    }

    public static w b(Socket socket) throws IOException {
        if (socket == null) {
            throw new IllegalArgumentException("socket == null");
        }
        if (socket.getOutputStream() == null) {
            throw new IOException("socket's output stream == null");
        }
        q qVar = new q(socket);
        OutputStream outputStream = socket.getOutputStream();
        if (outputStream != null) {
            return new h.a(qVar, new o(qVar, outputStream));
        }
        throw new IllegalArgumentException("out == null");
    }

    public static x c(InputStream inputStream, y yVar) {
        if (inputStream != null) {
            return new a(yVar, inputStream);
        }
        throw new IllegalArgumentException("in == null");
    }

    public static x d(Socket socket) throws IOException {
        if (socket == null) {
            throw new IllegalArgumentException("socket == null");
        }
        if (socket.getInputStream() == null) {
            throw new IOException("socket's input stream == null");
        }
        q qVar = new q(socket);
        return new b(qVar, c(socket.getInputStream(), qVar));
    }
}
