package okhttp3.repackaged.internal.framed; import java.io.EOFException; import java.io.IOException; import java.io.InterruptedIOException; import java.net.SocketTimeoutException; import java.util.ArrayList; import java.util.List; import o.C15140giG; import o.InterfaceC15142giR; import o.giM; import o.giX; import o.giY; import o.giZ; /* loaded from: classes6.dex */ public final class FramedStream { static final boolean $assertionsDisabled = false; private final FramedConnection ajZ; private final List
aka; private List
akb; private final b akc; final a akd; long bytesLeftInWriteWindow; private final int id; long unacknowledgedBytesRead = 0; private final c ake = new c(this); private final c akf = new c(this); private ErrorCode akg = null; /* JADX INFO: Access modifiers changed from: package-private */ public FramedStream(int i, FramedConnection framedConnection, boolean z, boolean z2, List
list) { if (framedConnection == null) { throw new NullPointerException("connection == null"); } if (list == null) { throw new NullPointerException("requestHeaders == null"); } this.id = i; this.ajZ = framedConnection; this.bytesLeftInWriteWindow = framedConnection.ajK.M(65536); b bVar = new b(framedConnection.ajI.M(65536)); this.akc = bVar; a aVar = new a(this); this.akd = aVar; bVar.finished = z2; aVar.finished = z; this.aka = list; } public final boolean isOpen() { synchronized (this) { if (this.akg != null) { return false; } if ((this.akc.finished || this.akc.closed) && (this.akd.finished || this.akd.closed)) { if (this.akb != null) { return false; } } return true; } } public final boolean isLocallyInitiated() { return this.ajZ.client == ((this.id & 1) == 1); } public final List
getResponseHeaders() throws IOException { List
list; synchronized (this) { this.ake.enter(); while (this.akb == null && this.akg == null) { try { waitForIo(); } catch (Throwable th) { this.ake.exitAndThrowIfTimedOut(); throw th; } } this.ake.exitAndThrowIfTimedOut(); list = this.akb; if (list == null) { StringBuilder sb = new StringBuilder(); sb.append("stream was reset: "); sb.append(this.akg); throw new IOException(sb.toString()); } } return list; } public final ErrorCode getErrorCode() { ErrorCode errorCode; synchronized (this) { errorCode = this.akg; } return errorCode; } public final void reply(List
list, boolean z) throws IOException { boolean z2; synchronized (this) { if (list == null) { throw new NullPointerException("responseHeaders == null"); } if (this.akb != null) { throw new IllegalStateException("reply already sent"); } this.akb = list; if (z) { z2 = false; } else { z2 = true; this.akd.finished = true; } } this.ajZ.writeSynReply(this.id, z2, list); if (z2) { this.ajZ.flush(); } } public final giX getSink() { synchronized (this) { if (this.akb == null && !isLocallyInitiated()) { throw new IllegalStateException("reply before requesting the sink"); } } return this.akd; } public final void close(ErrorCode errorCode) throws IOException { if (a(errorCode)) { this.ajZ.b(this.id, errorCode); } } public final void closeLater(ErrorCode errorCode) { if (a(errorCode)) { this.ajZ.a(this.id, errorCode); } } private boolean a(ErrorCode errorCode) { synchronized (this) { if (this.akg != null) { return false; } if (this.akc.finished && this.akd.finished) { return false; } this.akg = errorCode; notifyAll(); this.ajZ.v(this.id); return true; } } /* JADX INFO: Access modifiers changed from: package-private */ public final void a(List
list, HeadersMode headersMode) { ErrorCode errorCode; boolean z; synchronized (this) { errorCode = null; z = true; if (this.akb == null) { if (headersMode.failIfHeadersAbsent()) { errorCode = ErrorCode.PROTOCOL_ERROR; } else { this.akb = list; z = isOpen(); notifyAll(); } } else if (headersMode.failIfHeadersPresent()) { errorCode = ErrorCode.STREAM_IN_USE; } else { ArrayList arrayList = new ArrayList(); arrayList.addAll(this.akb); arrayList.addAll(list); this.akb = arrayList; } } if (errorCode != null) { closeLater(errorCode); } else { if (z) { return; } this.ajZ.v(this.id); } } /* JADX INFO: Access modifiers changed from: package-private */ public final void a(InterfaceC15142giR interfaceC15142giR, int i) throws IOException { this.akc.a(interfaceC15142giR, i); } /* JADX INFO: Access modifiers changed from: package-private */ public final void receiveFin() { boolean isOpen; synchronized (this) { this.akc.finished = true; isOpen = isOpen(); notifyAll(); } if (isOpen) { return; } this.ajZ.v(this.id); } /* JADX INFO: Access modifiers changed from: package-private */ public final void b(ErrorCode errorCode) { synchronized (this) { if (this.akg == null) { this.akg = errorCode; notifyAll(); } } } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes6.dex */ public final class b implements giZ { static final boolean $assertionsDisabled = false; final FramedStream akj; private final giM akk; private final giM akl; private final long akm; private boolean closed; private boolean finished; private b(FramedStream framedStream, long j) { this.akj = framedStream; this.akk = new giM(); this.akl = new giM(); this.akm = j; } @Override // o.giZ public final long read(giM gim, long j) throws IOException { if (j < 0) { throw new IllegalArgumentException("byteCount < 0: ".concat(String.valueOf(j))); } synchronized (this.akj) { uC(); uw(); if (this.akl.a == 0) { return -1L; } giM gim2 = this.akl; long read = gim2.read(gim, Math.min(j, gim2.a)); this.akj.unacknowledgedBytesRead += read; if (this.akj.unacknowledgedBytesRead >= this.akj.ajZ.ajI.M(65536) / 2) { this.akj.ajZ.writeWindowUpdateLater(this.akj.id, this.akj.unacknowledgedBytesRead); this.akj.unacknowledgedBytesRead = 0L; } synchronized (this.akj.ajZ) { this.akj.ajZ.unacknowledgedBytesRead += read; if (this.akj.ajZ.unacknowledgedBytesRead >= this.akj.ajZ.ajI.M(65536) / 2) { this.akj.ajZ.writeWindowUpdateLater(0, this.akj.ajZ.unacknowledgedBytesRead); this.akj.ajZ.unacknowledgedBytesRead = 0L; } } return read; } } private void uC() throws IOException { this.akj.ake.enter(); while (this.akl.a == 0 && !this.finished && !this.closed && this.akj.akg == null) { try { this.akj.waitForIo(); } finally { this.akj.ake.exitAndThrowIfTimedOut(); } } } final void a(InterfaceC15142giR interfaceC15142giR, long j) throws IOException { boolean z; boolean z2; boolean z3; while (j > 0) { synchronized (this.akj) { z = this.finished; z2 = true; z3 = this.akl.a + j > this.akm; } if (z3) { interfaceC15142giR.f(j); this.akj.closeLater(ErrorCode.FLOW_CONTROL_ERROR); return; } if (z) { interfaceC15142giR.f(j); return; } long read = interfaceC15142giR.read(this.akk, j); if (read == -1) { throw new EOFException(); } j -= read; synchronized (this.akj) { if (this.akl.a != 0) { z2 = false; } this.akl.d(this.akk); if (z2) { this.akj.notifyAll(); } } } } @Override // o.giZ public final giY timeout() { return this.akj.ake; } @Override // o.giZ, java.io.Closeable, java.lang.AutoCloseable public final void close() throws IOException { synchronized (this.akj) { this.closed = true; giM gim = this.akl; try { gim.f(gim.a); this.akj.notifyAll(); } catch (EOFException e) { throw new AssertionError(e); } } this.akj.cancelStreamIfNecessary(); } private void uw() throws IOException { if (!this.closed) { if (this.akj.akg == null) { return; } StringBuilder sb = new StringBuilder("stream was reset: "); sb.append(this.akj.akg); throw new IOException(sb.toString()); } throw new IOException("stream closed"); } } /* JADX INFO: Access modifiers changed from: private */ public void cancelStreamIfNecessary() throws IOException { boolean z; boolean isOpen; synchronized (this) { z = !this.akc.finished && this.akc.closed && (this.akd.finished || this.akd.closed); isOpen = isOpen(); } if (z) { close(ErrorCode.CANCEL); } else { if (isOpen) { return; } this.ajZ.v(this.id); } } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes6.dex */ public final class a implements giX { static final boolean $assertionsDisabled = false; private static final long akh = 16384; private final giM aki = new giM(); final FramedStream akj; private boolean closed; private boolean finished; a(FramedStream framedStream) { this.akj = framedStream; } @Override // o.giX public final void write(giM gim, long j) throws IOException { this.aki.write(gim, j); while (this.aki.a >= 16384) { A(false); } } private void A(boolean z) throws IOException { long min; synchronized (this.akj) { this.akj.akf.enter(); while (this.akj.bytesLeftInWriteWindow <= 0 && !this.finished && !this.closed && this.akj.akg == null) { try { this.akj.waitForIo(); } finally { } } this.akj.akf.exitAndThrowIfTimedOut(); this.akj.checkOutNotClosed(); min = Math.min(this.akj.bytesLeftInWriteWindow, this.aki.a); this.akj.bytesLeftInWriteWindow -= min; } this.akj.akf.enter(); try { this.akj.ajZ.writeData(this.akj.id, z && min == this.aki.a, this.aki, min); } finally { } } @Override // o.giX, java.io.Flushable public final void flush() throws IOException { synchronized (this.akj) { this.akj.checkOutNotClosed(); } while (this.aki.a > 0) { A(false); this.akj.ajZ.flush(); } } @Override // o.giX public final giY timeout() { return this.akj.akf; } @Override // o.giX, java.io.Closeable, java.lang.AutoCloseable public final void close() throws IOException { synchronized (this.akj) { if (this.closed) { return; } if (!this.akj.akd.finished) { if (this.aki.a > 0) { while (this.aki.a > 0) { A(true); } } else { this.akj.ajZ.writeData(this.akj.id, true, null, 0L); } } synchronized (this.akj) { this.closed = true; } this.akj.ajZ.flush(); this.akj.cancelStreamIfNecessary(); } } } /* JADX INFO: Access modifiers changed from: package-private */ public final void addBytesToWriteWindow(long j) { this.bytesLeftInWriteWindow += j; if (j > 0) { notifyAll(); } } /* JADX INFO: Access modifiers changed from: private */ public void checkOutNotClosed() throws IOException { if (!this.akd.closed) { if (this.akd.finished) { throw new IOException("stream finished"); } if (this.akg == null) { return; } StringBuilder sb = new StringBuilder("stream was reset: "); sb.append(this.akg); throw new IOException(sb.toString()); } throw new IOException("stream closed"); } /* JADX INFO: Access modifiers changed from: private */ public void waitForIo() throws InterruptedIOException { try { wait(); } catch (InterruptedException unused) { throw new InterruptedIOException(); } } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes6.dex */ public class c extends C15140giG { final FramedStream akj; c(FramedStream framedStream) { this.akj = framedStream; } @Override // o.C15140giG public void timedOut() { this.akj.closeLater(ErrorCode.CANCEL); } @Override // o.C15140giG public IOException newTimeoutException(IOException iOException) { SocketTimeoutException socketTimeoutException = new SocketTimeoutException("timeout"); if (iOException != null) { socketTimeoutException.initCause(iOException); } return socketTimeoutException; } public void exitAndThrowIfTimedOut() throws IOException { if (exit()) { throw newTimeoutException(null); } } } public final giY writeTimeout() { return this.akf; } public final giY readTimeout() { return this.ake; } public final giZ getSource() { return this.akc; } public final List
getRequestHeaders() { return this.aka; } public final int getId() { return this.id; } public final FramedConnection getConnection() { return this.ajZ; } }