package f;

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 m {

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

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

        /* JADX INFO: renamed from: c, reason: collision with root package name */
        final /* synthetic */ u f3096c;

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

        a(u uVar, OutputStream outputStream) {
            this.f3096c = uVar;
            this.f3097d = outputStream;
        }

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

        @Override // f.s, java.io.Flushable
        public void flush() throws IOException {
            this.f3097d.flush();
        }

        @Override // f.s
        public u timeout() {
            return this.f3096c;
        }

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

        @Override // f.s
        public void write(f.c cVar, long j) throws IOException {
            v.b(cVar.f3076e, 0L, j);
            while (j > 0) {
                this.f3096c.throwIfReached();
                p pVar = cVar.f3075d;
                int iMin = (int) Math.min(j, pVar.f3110c - pVar.f3109b);
                this.f3097d.write(pVar.f3108a, pVar.f3109b, iMin);
                int i = pVar.f3109b + iMin;
                pVar.f3109b = i;
                long j2 = iMin;
                j -= j2;
                cVar.f3076e -= j2;
                if (i == pVar.f3110c) {
                    cVar.f3075d = pVar.b();
                    q.a(pVar);
                }
            }
        }
    }

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

        /* JADX INFO: renamed from: c, reason: collision with root package name */
        final /* synthetic */ u f3098c;

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

        b(u uVar, InputStream inputStream) {
            this.f3098c = uVar;
            this.f3099d = inputStream;
        }

        @Override // f.t, java.io.Closeable, java.lang.AutoCloseable
        public void close() throws IOException {
            this.f3099d.close();
        }

        @Override // f.t
        public long read(f.c cVar, long j) throws IOException {
            if (j < 0) {
                throw new IllegalArgumentException("byteCount < 0: " + j);
            }
            if (j == 0) {
                return 0L;
            }
            try {
                this.f3098c.throwIfReached();
                p pVarD = cVar.D(1);
                int i = this.f3099d.read(pVarD.f3108a, pVarD.f3110c, (int) Math.min(j, 8192 - pVarD.f3110c));
                if (i == -1) {
                    return -1L;
                }
                pVarD.f3110c += i;
                long j2 = i;
                cVar.f3076e += j2;
                return j2;
            } catch (AssertionError e2) {
                if (m.e(e2)) {
                    throw new IOException(e2);
                }
                throw e2;
            }
        }

        @Override // f.t
        public u timeout() {
            return this.f3098c;
        }

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

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

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

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

        @Override // f.s
        public u timeout() {
            return u.NONE;
        }

        @Override // f.s
        public void write(f.c cVar, long j) throws IOException {
            cVar.skip(j);
        }
    }

    /* JADX INFO: compiled from: Okio.java */
    final class d extends f.a {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        final /* synthetic */ Socket f3100a;

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

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

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

    private m() {
    }

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

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

    public static f.d c(s sVar) {
        return new n(sVar);
    }

    public static e d(t tVar) {
        return new o(tVar);
    }

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

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

    public static s g(OutputStream outputStream) {
        return h(outputStream, new u());
    }

    private static s h(OutputStream outputStream, u uVar) {
        if (outputStream == null) {
            throw new IllegalArgumentException("out == null");
        }
        if (uVar != null) {
            return new a(uVar, outputStream);
        }
        throw new IllegalArgumentException("timeout == null");
    }

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

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

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

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

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

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