package com.mbridge.msdk.thrid.okio; import csdk.gluads.Consts; import java.io.IOException; /* loaded from: classes4.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(); public final Sink sink() { return this.sink; } public final Source source() { return this.source; } public Pipe(long j) { if (j >= 1) { this.maxBufferSize = j; return; } throw new IllegalArgumentException("maxBufferSize < 1: " + j); } public final class PipeSink implements Sink { final Timeout timeout = new Timeout(); @Override // com.mbridge.msdk.thrid.okio.Sink public Timeout timeout() { return this.timeout; } public PipeSink() { } @Override // com.mbridge.msdk.thrid.okio.Sink public void write(Buffer buffer, long j) throws IOException { synchronized (Pipe.this.buffer) { try { if (Pipe.this.sinkClosed) { throw new IllegalStateException(Consts.PLACEMENT_STATUS_CLOSED); } while (j > 0) { Pipe pipe = Pipe.this; if (pipe.sourceClosed) { throw new IOException("source is closed"); } long size = pipe.maxBufferSize - pipe.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(); } } } catch (Throwable th) { throw th; } } } @Override // com.mbridge.msdk.thrid.okio.Sink, java.io.Flushable public void flush() throws IOException { synchronized (Pipe.this.buffer) { try { Pipe pipe = Pipe.this; if (pipe.sinkClosed) { throw new IllegalStateException(Consts.PLACEMENT_STATUS_CLOSED); } if (pipe.sourceClosed && pipe.buffer.size() > 0) { throw new IOException("source is closed"); } } finally { } } } @Override // com.mbridge.msdk.thrid.okio.Sink, java.io.Closeable, java.lang.AutoCloseable public void close() throws IOException { synchronized (Pipe.this.buffer) { try { Pipe pipe = Pipe.this; if (pipe.sinkClosed) { return; } if (pipe.sourceClosed && pipe.buffer.size() > 0) { throw new IOException("source is closed"); } Pipe pipe2 = Pipe.this; pipe2.sinkClosed = true; pipe2.buffer.notifyAll(); } catch (Throwable th) { throw th; } } } } public final class PipeSource implements Source { final Timeout timeout = new Timeout(); @Override // com.mbridge.msdk.thrid.okio.Source public Timeout timeout() { return this.timeout; } public PipeSource() { } @Override // com.mbridge.msdk.thrid.okio.Source public long read(Buffer buffer, long j) throws IOException { synchronized (Pipe.this.buffer) { try { if (Pipe.this.sourceClosed) { throw new IllegalStateException(Consts.PLACEMENT_STATUS_CLOSED); } while (Pipe.this.buffer.size() == 0) { Pipe pipe = Pipe.this; if (pipe.sinkClosed) { return -1L; } this.timeout.waitUntilNotified(pipe.buffer); } long read = Pipe.this.buffer.read(buffer, j); Pipe.this.buffer.notifyAll(); return read; } catch (Throwable th) { throw th; } } } @Override // com.mbridge.msdk.thrid.okio.Source, java.io.Closeable, java.lang.AutoCloseable public void close() throws IOException { synchronized (Pipe.this.buffer) { Pipe pipe = Pipe.this; pipe.sourceClosed = true; pipe.buffer.notifyAll(); } } } }