package okio; import java.io.IOException; /* loaded from: classes2.dex */ public final class Pipe { final long maxBufferSize; boolean sinkClosed; boolean sourceClosed; final Buffer buffer = new Buffer(); private final Sink sink = new PipeSink(); private final Source source = new PipeSource(); final class PipeSink implements Sink { final Timeout timeout = new Timeout(); PipeSink() { } @Override // okio.Sink, java.io.Closeable, java.lang.AutoCloseable public void close() throws IOException { synchronized (Pipe.this.buffer) { if (Pipe.this.sinkClosed) { return; } if (Pipe.this.sourceClosed && Pipe.this.buffer.size() > 0) { throw new IOException("source is closed"); } Pipe.this.sinkClosed = true; Pipe.this.buffer.notifyAll(); } } @Override // okio.Sink, java.io.Flushable public void flush() throws IOException { synchronized (Pipe.this.buffer) { if (Pipe.this.sinkClosed) { throw new IllegalStateException("closed"); } if (Pipe.this.sourceClosed && Pipe.this.buffer.size() > 0) { throw new IOException("source is closed"); } } } @Override // okio.Sink public Timeout timeout() { return this.timeout; } @Override // okio.Sink public void write(Buffer buffer, long j) throws IOException { synchronized (Pipe.this.buffer) { if (Pipe.this.sinkClosed) { throw new IllegalStateException("closed"); } while (j > 0) { if (Pipe.this.sourceClosed) { throw new IOException("source is closed"); } long size = Pipe.this.maxBufferSize - Pipe.this.buffer.size(); if (size == 0) { this.timeout.waitUntilNotified(Pipe.this.buffer); } else { long min = Math.min(size, j); Pipe.this.buffer.write(buffer, min); j -= min; Pipe.this.buffer.notifyAll(); } } } } } final class PipeSource implements Source { final Timeout timeout = new Timeout(); PipeSource() { } @Override // okio.Source, java.io.Closeable, java.lang.AutoCloseable public void close() throws IOException { synchronized (Pipe.this.buffer) { Pipe.this.sourceClosed = true; Pipe.this.buffer.notifyAll(); } } @Override // okio.Source public long read(Buffer buffer, long j) throws IOException { synchronized (Pipe.this.buffer) { if (Pipe.this.sourceClosed) { throw new IllegalStateException("closed"); } while (Pipe.this.buffer.size() == 0) { if (Pipe.this.sinkClosed) { return -1L; } this.timeout.waitUntilNotified(Pipe.this.buffer); } long read = Pipe.this.buffer.read(buffer, j); Pipe.this.buffer.notifyAll(); return read; } } @Override // okio.Source public Timeout timeout() { return this.timeout; } } public Pipe(long j) { if (j >= 1) { this.maxBufferSize = j; return; } throw new IllegalArgumentException("maxBufferSize < 1: " + j); } public Sink sink() { return this.sink; } public Source source() { return this.source; } }