package com.czhj.wire.okio; import android.os.Build; 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.nio.file.Files; import java.nio.file.OpenOption; import java.nio.file.Path; import java.util.logging.Level; import java.util.logging.Logger; public final class Okio { static final Logger a = Logger.getLogger(Okio.class.getName()); private Okio() { } private static AsyncTimeout a(final Socket socket) { return new AsyncTimeout() { /* class com.czhj.wire.okio.Okio.AnonymousClass3 */ /* access modifiers changed from: protected */ @Override // com.czhj.wire.okio.AsyncTimeout public IOException newTimeoutException(IOException iOException) { SocketTimeoutException socketTimeoutException = new SocketTimeoutException("timeout"); if (iOException != null) { socketTimeoutException.initCause(iOException); } return socketTimeoutException; } /* JADX DEBUG: Multi-variable search result rejected for r1v2, resolved type: java.lang.AssertionError */ /* JADX WARN: Multi-variable type inference failed */ /* access modifiers changed from: protected */ @Override // com.czhj.wire.okio.AsyncTimeout public void timedOut() { StringBuilder sb; Level level; Logger logger; Exception exc; try { socket.close(); return; } catch (Exception e) { Logger logger2 = Okio.a; level = Level.WARNING; sb = new StringBuilder(); exc = e; logger = logger2; } catch (AssertionError e2) { if (Okio.a(e2)) { Logger logger3 = Okio.a; level = Level.WARNING; sb = new StringBuilder(); exc = e2; logger = logger3; } else { throw e2; } } sb.append("Failed to close timed out socket "); sb.append(socket); logger.log(level, sb.toString(), (Throwable) exc); } }; } private static Sink a(final OutputStream outputStream, final Timeout timeout) { if (outputStream == null) { throw new IllegalArgumentException("out == null"); } else if (timeout != null) { return new Sink() { /* class com.czhj.wire.okio.Okio.AnonymousClass1 */ @Override // com.czhj.wire.okio.Sink, java.io.Closeable, java.lang.AutoCloseable public void close() throws IOException { outputStream.close(); } @Override // com.czhj.wire.okio.Sink, java.io.Flushable public void flush() throws IOException { outputStream.flush(); } @Override // com.czhj.wire.okio.Sink public Timeout timeout() { return Timeout.this; } public String toString() { return "sink(" + outputStream + ")"; } @Override // com.czhj.wire.okio.Sink public void write(Buffer buffer, long j) throws IOException { Util.checkOffsetAndCount(buffer.c, 0, j); while (j > 0) { Timeout.this.throwIfReached(); Segment segment = buffer.b; int min = (int) Math.min(j, (long) (segment.e - segment.d)); outputStream.write(segment.c, segment.d, min); segment.d += min; long j2 = (long) min; j -= j2; buffer.c -= j2; if (segment.d == segment.e) { buffer.b = segment.pop(); SegmentPool.a(segment); } } } }; } else { throw new IllegalArgumentException("timeout == null"); } } private static Source a(final InputStream inputStream, final Timeout timeout) { if (inputStream == null) { throw new IllegalArgumentException("in == null"); } else if (timeout != null) { return new Source() { /* class com.czhj.wire.okio.Okio.AnonymousClass2 */ @Override // java.io.Closeable, com.czhj.wire.okio.Source, java.lang.AutoCloseable public void close() throws IOException { inputStream.close(); } @Override // com.czhj.wire.okio.Source public long read(Buffer buffer, long j) throws IOException { int i = (j > 0 ? 1 : (j == 0 ? 0 : -1)); if (i < 0) { throw new IllegalArgumentException("byteCount < 0: " + j); } else if (i == 0) { return 0; } else { try { Timeout.this.throwIfReached(); Segment a2 = buffer.a(1); int read = inputStream.read(a2.c, a2.e, (int) Math.min(j, (long) (8192 - a2.e))); if (read == -1) { return -1; } a2.e += read; long j2 = (long) read; buffer.c += j2; return j2; } catch (AssertionError e) { if (Okio.a(e)) { throw new IOException(e); } throw e; } } } @Override // com.czhj.wire.okio.Source public Timeout timeout() { return Timeout.this; } public String toString() { return "source(" + inputStream + ")"; } }; } else { throw new IllegalArgumentException("timeout == null"); } } static boolean a(AssertionError assertionError) { return (assertionError.getCause() == null || assertionError.getMessage() == null || !assertionError.getMessage().contains("getsockname failed")) ? false : true; } public static Sink appendingSink(File file) throws FileNotFoundException { if (file != null) { return sink(new FileOutputStream(file, true)); } throw new IllegalArgumentException("file == null"); } public static BufferedSink buffer(Sink sink) { if (sink != null) { return new RealBufferedSink(sink); } throw new IllegalArgumentException("sink == null"); } public static BufferedSource buffer(Source source) { if (source != null) { return new RealBufferedSource(source); } throw new IllegalArgumentException("source == null"); } public static Sink sink(File file) throws FileNotFoundException { if (file != null) { return sink(new FileOutputStream(file)); } throw new IllegalArgumentException("file == null"); } public static Sink sink(OutputStream outputStream) { return a(outputStream, new Timeout()); } public static Sink sink(Socket socket) throws IOException { if (socket != null) { AsyncTimeout a2 = a(socket); return a2.sink(a(socket.getOutputStream(), a2)); } throw new IllegalArgumentException("socket == null"); } public static Sink sink(Path path, OpenOption... openOptionArr) throws IOException { if (path != null) { try { if (Build.VERSION.SDK_INT >= 26) { return sink(Files.newOutputStream(path, openOptionArr)); } throw new IOException("no support os version"); } catch (Throwable th) { throw new IOException(th.getMessage()); } } else { throw new IllegalArgumentException("path == null"); } } public static Source source(File file) throws FileNotFoundException { if (file != null) { return source(new FileInputStream(file)); } throw new IllegalArgumentException("file == null"); } public static Source source(InputStream inputStream) { return a(inputStream, new Timeout()); } public static Source source(Socket socket) throws IOException { if (socket != null) { AsyncTimeout a2 = a(socket); return a2.source(a(socket.getInputStream(), a2)); } throw new IllegalArgumentException("socket == null"); } public static Source source(Path path, OpenOption... openOptionArr) throws IOException { if (path != null) { try { if (Build.VERSION.SDK_INT >= 26) { return source(Files.newInputStream(path, openOptionArr)); } throw new IOException("no support os version"); } catch (Throwable th) { throw new IOException(th.getMessage()); } } else { throw new IllegalArgumentException("path == null"); } } }