package h;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Nullable;

/* JADX INFO: compiled from: Okio.java */
/* JADX INFO: loaded from: classes.dex */
public final class l {
    static final Logger a = Logger.getLogger(l.class.getName());

    /* JADX INFO: compiled from: Okio.java */
    final class a implements r {

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        final /* synthetic */ t f4194b;

        /* JADX INFO: renamed from: f, reason: collision with root package name */
        final /* synthetic */ OutputStream f4195f;

        a(t tVar, OutputStream outputStream) {
            this.f4194b = tVar;
            this.f4195f = outputStream;
        }

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

        @Override // h.r, java.io.Flushable
        public void flush() throws IOException {
            this.f4195f.flush();
        }

        @Override // h.r
        public t timeout() {
            return this.f4194b;
        }

        public String toString() {
            return "sink(" + this.f4195f + ")";
        }

        @Override // h.r
        public void write(h.c cVar, long j2) throws IOException {
            u.b(cVar.f4177f, 0L, j2);
            while (j2 > 0) {
                this.f4194b.throwIfReached();
                o oVar = cVar.f4176b;
                int iMin = (int) Math.min(j2, oVar.f4206c - oVar.f4205b);
                this.f4195f.write(oVar.a, oVar.f4205b, iMin);
                int i2 = oVar.f4205b + iMin;
                oVar.f4205b = i2;
                long j3 = iMin;
                j2 -= j3;
                cVar.f4177f -= j3;
                if (i2 == oVar.f4206c) {
                    cVar.f4176b = oVar.b();
                    p.a(oVar);
                }
            }
        }
    }

    /* JADX INFO: compiled from: Okio.java */
    final class b implements s {

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        final /* synthetic */ t f4196b;

        /* JADX INFO: renamed from: f, reason: collision with root package name */
        final /* synthetic */ InputStream f4197f;

        b(t tVar, InputStream inputStream) {
            this.f4196b = tVar;
            this.f4197f = inputStream;
        }

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

        @Override // h.s
        public long read(h.c cVar, long j2) throws IOException {
            if (j2 < 0) {
                throw new IllegalArgumentException("byteCount < 0: " + j2);
            }
            if (j2 == 0) {
                return 0L;
            }
            try {
                this.f4196b.throwIfReached();
                o oVarF = cVar.F(1);
                int i2 = this.f4197f.read(oVarF.a, oVarF.f4206c, (int) Math.min(j2, 8192 - oVarF.f4206c));
                if (i2 == -1) {
                    return -1L;
                }
                oVarF.f4206c += i2;
                long j3 = i2;
                cVar.f4177f += j3;
                return j3;
            } catch (AssertionError e2) {
                if (l.e(e2)) {
                    throw new IOException(e2);
                }
                throw e2;
            }
        }

        @Override // h.s
        public t timeout() {
            return this.f4196b;
        }

        public String toString() {
            return "source(" + this.f4197f + ")";
        }
    }

    /* JADX INFO: compiled from: Okio.java */
    final class c implements r {
        c() {
        }

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

        @Override // h.r, java.io.Flushable
        public void flush() throws IOException {
        }

        @Override // h.r
        public t timeout() {
            return t.NONE;
        }

        @Override // h.r
        public void write(h.c cVar, long j2) throws IOException {
            cVar.skip(j2);
        }
    }

    /* JADX INFO: compiled from: Okio.java */
    final class d extends h.a {
        final /* synthetic */ Socket a;

        d(Socket socket) {
            this.a = socket;
        }

        @Override // h.a
        protected IOException newTimeoutException(@Nullable IOException iOException) {
            SocketTimeoutException socketTimeoutException = new SocketTimeoutException("timeout");
            if (iOException != null) {
                socketTimeoutException.initCause(iOException);
            }
            return socketTimeoutException;
        }

        @Override // h.a
        protected void timedOut() {
            try {
                this.a.close();
            } catch (AssertionError e2) {
                if (!l.e(e2)) {
                    throw e2;
                }
                l.a.log(Level.WARNING, "Failed to close timed out socket " + this.a, (Throwable) e2);
            } catch (Exception e3) {
                l.a.log(Level.WARNING, "Failed to close timed out socket " + this.a, (Throwable) e3);
            }
        }
    }

    private l() {
    }

    public static r a(File file) throws FileNotFoundException {
        if (file != null) {
            return g(new FileOutputStream(file, true));
        }
        throw new IllegalArgumentException("file == null");
    }

    public static r b() {
        return new c();
    }

    public static h.d c(r rVar) {
        return new m(rVar);
    }

    public static e d(s sVar) {
        return new n(sVar);
    }

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

    public static r f(File file) throws FileNotFoundException {
        if (file != null) {
            return g(new FileOutputStream(file));
        }
        throw new IllegalArgumentException("file == null");
    }

    public static r g(OutputStream outputStream) {
        return h(outputStream, new t());
    }

    private static r h(OutputStream outputStream, t tVar) {
        if (outputStream == null) {
            throw new IllegalArgumentException("out == null");
        }
        if (tVar != null) {
            return new a(tVar, outputStream);
        }
        throw new IllegalArgumentException("timeout == null");
    }

    public static r i(Socket socket) throws IOException {
        if (socket == null) {
            throw new IllegalArgumentException("socket == null");
        }
        h.a aVarN = n(socket);
        return aVarN.sink(h(socket.getOutputStream(), aVarN));
    }

    public static s j(File file) throws FileNotFoundException {
        if (file != null) {
            return k(new FileInputStream(file));
        }
        throw new IllegalArgumentException("file == null");
    }

    public static s k(InputStream inputStream) {
        return l(inputStream, new t());
    }

    private static s l(InputStream inputStream, t tVar) {
        if (inputStream == null) {
            throw new IllegalArgumentException("in == null");
        }
        if (tVar != null) {
            return new b(tVar, inputStream);
        }
        throw new IllegalArgumentException("timeout == null");
    }

    public static s m(Socket socket) throws IOException {
        if (socket == null) {
            throw new IllegalArgumentException("socket == null");
        }
        h.a aVarN = n(socket);
        return aVarN.source(l(socket.getInputStream(), aVarN));
    }

    private static h.a n(Socket socket) {
        return new d(socket);
    }
}
