package okhttp3.internal.http2; import android.support.v4.media.session.PlaybackStateCompat; import java.io.EOFException; import java.io.IOException; import java.io.InterruptedIOException; import java.net.SocketTimeoutException; import java.util.ArrayDeque; import java.util.Deque; import okhttp3.Headers; import okhttp3.internal.Util; import okio.AsyncTimeout; import okio.Buffer; import okio.BufferedSource; import okio.Sink; import okio.Source; import okio.Timeout; /* loaded from: classes5.dex */ public final class Http2Stream { public long bytesLeftInWriteWindow; public final Http2Connection connection; public ErrorCode errorCode; public IOException errorException; public boolean hasResponseHeaders; public final Deque headersQueue; public final int id; public final StreamTimeout readTimeout; public final FramingSink sink; public final FramingSource source; public long unacknowledgedBytesRead = 0; public final StreamTimeout writeTimeout; public int getId() { return this.id; } public Source getSource() { return this.source; } public Timeout readTimeout() { return this.readTimeout; } public Timeout writeTimeout() { return this.writeTimeout; } public Http2Stream(int i, Http2Connection http2Connection, boolean z, boolean z2, Headers headers) { ArrayDeque arrayDeque = new ArrayDeque(); this.headersQueue = arrayDeque; this.readTimeout = new StreamTimeout(); this.writeTimeout = new StreamTimeout(); if (http2Connection == null) { throw new NullPointerException("connection == null"); } this.id = i; this.connection = http2Connection; this.bytesLeftInWriteWindow = http2Connection.peerSettings.getInitialWindowSize(); FramingSource framingSource = new FramingSource(http2Connection.okHttpSettings.getInitialWindowSize()); this.source = framingSource; FramingSink framingSink = new FramingSink(); this.sink = framingSink; framingSource.finished = z2; framingSink.finished = z; if (headers != null) { arrayDeque.add(headers); } if (isLocallyInitiated() && headers != null) { throw new IllegalStateException("locally-initiated streams shouldn't have headers yet"); } if (!isLocallyInitiated() && headers == null) { throw new IllegalStateException("remotely-initiated streams should have headers"); } } public synchronized boolean isOpen() { try { if (this.errorCode != null) { return false; } FramingSource framingSource = this.source; if (!framingSource.finished) { if (framingSource.closed) { } return true; } FramingSink framingSink = this.sink; if (framingSink.finished || framingSink.closed) { if (this.hasResponseHeaders) { return false; } } return true; } catch (Throwable th) { throw th; } } public boolean isLocallyInitiated() { return this.connection.client == ((this.id & 1) == 1); } public synchronized Headers takeHeaders() { this.readTimeout.enter(); while (this.headersQueue.isEmpty() && this.errorCode == null) { try { waitForIo(); } catch (Throwable th) { this.readTimeout.exitAndThrowIfTimedOut(); throw th; } } this.readTimeout.exitAndThrowIfTimedOut(); if (this.headersQueue.isEmpty()) { IOException iOException = this.errorException; if (iOException != null) { throw iOException; } throw new StreamResetException(this.errorCode); } return (Headers) this.headersQueue.removeFirst(); } public Sink getSink() { synchronized (this) { try { if (!this.hasResponseHeaders && !isLocallyInitiated()) { throw new IllegalStateException("reply before requesting the sink"); } } catch (Throwable th) { throw th; } } return this.sink; } public void close(ErrorCode errorCode, IOException iOException) { if (closeInternal(errorCode, iOException)) { this.connection.writeSynReset(this.id, errorCode); } } public void closeLater(ErrorCode errorCode) { if (closeInternal(errorCode, null)) { this.connection.writeSynResetLater(this.id, errorCode); } } public final boolean closeInternal(ErrorCode errorCode, IOException iOException) { synchronized (this) { try { if (this.errorCode != null) { return false; } if (this.source.finished && this.sink.finished) { return false; } this.errorCode = errorCode; this.errorException = iOException; notifyAll(); this.connection.removeStream(this.id); return true; } catch (Throwable th) { throw th; } } } public void receiveData(BufferedSource bufferedSource, int i) { this.source.receive(bufferedSource, i); } /* JADX WARN: Removed duplicated region for block: B:9:0x001a A[Catch: all -> 0x000f, TryCatch #0 {all -> 0x000f, blocks: (B:3:0x0001, B:7:0x0009, B:9:0x001a, B:10:0x001e, B:11:0x0025, B:18:0x0011), top: B:2:0x0001 }] */ /* Code decompiled incorrectly, please refer to instructions dump. To view partially-correct add '--show-bad-code' argument */ public void receiveHeaders(okhttp3.Headers r3, boolean r4) { /* r2 = this; monitor-enter(r2) boolean r0 = r2.hasResponseHeaders // Catch: java.lang.Throwable -> Lf r1 = 1 if (r0 == 0) goto L11 if (r4 != 0) goto L9 goto L11 L9: okhttp3.internal.http2.Http2Stream$FramingSource r0 = r2.source // Catch: java.lang.Throwable -> Lf okhttp3.internal.http2.Http2Stream.FramingSource.access$202(r0, r3) // Catch: java.lang.Throwable -> Lf goto L18 Lf: r3 = move-exception goto L30 L11: r2.hasResponseHeaders = r1 // Catch: java.lang.Throwable -> Lf java.util.Deque r0 = r2.headersQueue // Catch: java.lang.Throwable -> Lf r0.add(r3) // Catch: java.lang.Throwable -> Lf L18: if (r4 == 0) goto L1e okhttp3.internal.http2.Http2Stream$FramingSource r3 = r2.source // Catch: java.lang.Throwable -> Lf r3.finished = r1 // Catch: java.lang.Throwable -> Lf L1e: boolean r3 = r2.isOpen() // Catch: java.lang.Throwable -> Lf r2.notifyAll() // Catch: java.lang.Throwable -> Lf monitor-exit(r2) // Catch: java.lang.Throwable -> Lf if (r3 != 0) goto L2f okhttp3.internal.http2.Http2Connection r3 = r2.connection int r4 = r2.id r3.removeStream(r4) L2f: return L30: monitor-exit(r2) // Catch: java.lang.Throwable -> Lf throw r3 */ throw new UnsupportedOperationException("Method not decompiled: okhttp3.internal.http2.Http2Stream.receiveHeaders(okhttp3.Headers, boolean):void"); } public synchronized void receiveRstStream(ErrorCode errorCode) { if (this.errorCode == null) { this.errorCode = errorCode; notifyAll(); } } public final class FramingSource implements Source { public boolean closed; public boolean finished; public final long maxByteCount; public Headers trailers; public final Buffer receiveBuffer = new Buffer(); public final Buffer readBuffer = new Buffer(); public FramingSource(long j) { this.maxByteCount = j; } /* JADX WARN: Code restructure failed: missing block: B:25:0x0085, code lost: r12 = -1; */ /* JADX WARN: Removed duplicated region for block: B:30:0x0092 */ /* JADX WARN: Removed duplicated region for block: B:33:0x0096 */ @Override // okio.Source /* Code decompiled incorrectly, please refer to instructions dump. To view partially-correct add '--show-bad-code' argument */ public long read(okio.Buffer r12, long r13) { /* r11 = this; r0 = 0 int r2 = (r13 > r0 ? 1 : (r13 == r0 ? 0 : -1)) if (r2 < 0) goto Lac L6: okhttp3.internal.http2.Http2Stream r2 = okhttp3.internal.http2.Http2Stream.this monitor-enter(r2) okhttp3.internal.http2.Http2Stream r3 = okhttp3.internal.http2.Http2Stream.this // Catch: java.lang.Throwable -> L83 okhttp3.internal.http2.Http2Stream$StreamTimeout r3 = r3.readTimeout // Catch: java.lang.Throwable -> L83 r3.enter() // Catch: java.lang.Throwable -> L83 okhttp3.internal.http2.Http2Stream r3 = okhttp3.internal.http2.Http2Stream.this // Catch: java.lang.Throwable -> L25 okhttp3.internal.http2.ErrorCode r4 = r3.errorCode // Catch: java.lang.Throwable -> L25 if (r4 == 0) goto L28 java.io.IOException r3 = r3.errorException // Catch: java.lang.Throwable -> L25 if (r3 == 0) goto L1b goto L29 L1b: okhttp3.internal.http2.StreamResetException r3 = new okhttp3.internal.http2.StreamResetException // Catch: java.lang.Throwable -> L25 okhttp3.internal.http2.Http2Stream r4 = okhttp3.internal.http2.Http2Stream.this // Catch: java.lang.Throwable -> L25 okhttp3.internal.http2.ErrorCode r4 = r4.errorCode // Catch: java.lang.Throwable -> L25 r3.(r4) // Catch: java.lang.Throwable -> L25 goto L29 L25: r12 = move-exception goto La2 L28: r3 = 0 L29: boolean r4 = r11.closed // Catch: java.lang.Throwable -> L25 if (r4 != 0) goto L9a okio.Buffer r4 = r11.readBuffer // Catch: java.lang.Throwable -> L25 long r4 = r4.size() // Catch: java.lang.Throwable -> L25 int r4 = (r4 > r0 ? 1 : (r4 == r0 ? 0 : -1)) r5 = -1 if (r4 <= 0) goto L6f okio.Buffer r4 = r11.readBuffer // Catch: java.lang.Throwable -> L25 long r7 = r4.size() // Catch: java.lang.Throwable -> L25 long r13 = java.lang.Math.min(r13, r7) // Catch: java.lang.Throwable -> L25 long r12 = r4.read(r12, r13) // Catch: java.lang.Throwable -> L25 okhttp3.internal.http2.Http2Stream r14 = okhttp3.internal.http2.Http2Stream.this // Catch: java.lang.Throwable -> L25 long r7 = r14.unacknowledgedBytesRead // Catch: java.lang.Throwable -> L25 long r7 = r7 + r12 r14.unacknowledgedBytesRead = r7 // Catch: java.lang.Throwable -> L25 if (r3 != 0) goto L86 okhttp3.internal.http2.Http2Connection r14 = r14.connection // Catch: java.lang.Throwable -> L25 okhttp3.internal.http2.Settings r14 = r14.okHttpSettings // Catch: java.lang.Throwable -> L25 int r14 = r14.getInitialWindowSize() // Catch: java.lang.Throwable -> L25 int r14 = r14 / 2 long r9 = (long) r14 // Catch: java.lang.Throwable -> L25 int r14 = (r7 > r9 ? 1 : (r7 == r9 ? 0 : -1)) if (r14 < 0) goto L86 okhttp3.internal.http2.Http2Stream r14 = okhttp3.internal.http2.Http2Stream.this // Catch: java.lang.Throwable -> L25 okhttp3.internal.http2.Http2Connection r4 = r14.connection // Catch: java.lang.Throwable -> L25 int r7 = r14.id // Catch: java.lang.Throwable -> L25 long r8 = r14.unacknowledgedBytesRead // Catch: java.lang.Throwable -> L25 r4.writeWindowUpdateLater(r7, r8) // Catch: java.lang.Throwable -> L25 okhttp3.internal.http2.Http2Stream r14 = okhttp3.internal.http2.Http2Stream.this // Catch: java.lang.Throwable -> L25 r14.unacknowledgedBytesRead = r0 // Catch: java.lang.Throwable -> L25 goto L86 L6f: boolean r4 = r11.finished // Catch: java.lang.Throwable -> L25 if (r4 != 0) goto L85 if (r3 != 0) goto L85 okhttp3.internal.http2.Http2Stream r3 = okhttp3.internal.http2.Http2Stream.this // Catch: java.lang.Throwable -> L25 r3.waitForIo() // Catch: java.lang.Throwable -> L25 okhttp3.internal.http2.Http2Stream r3 = okhttp3.internal.http2.Http2Stream.this // Catch: java.lang.Throwable -> L83 okhttp3.internal.http2.Http2Stream$StreamTimeout r3 = r3.readTimeout // Catch: java.lang.Throwable -> L83 r3.exitAndThrowIfTimedOut() // Catch: java.lang.Throwable -> L83 monitor-exit(r2) // Catch: java.lang.Throwable -> L83 goto L6 L83: r12 = move-exception goto Laa L85: r12 = r5 L86: okhttp3.internal.http2.Http2Stream r14 = okhttp3.internal.http2.Http2Stream.this // Catch: java.lang.Throwable -> L83 okhttp3.internal.http2.Http2Stream$StreamTimeout r14 = r14.readTimeout // Catch: java.lang.Throwable -> L83 r14.exitAndThrowIfTimedOut() // Catch: java.lang.Throwable -> L83 monitor-exit(r2) // Catch: java.lang.Throwable -> L83 int r14 = (r12 > r5 ? 1 : (r12 == r5 ? 0 : -1)) if (r14 == 0) goto L96 r11.updateConnectionFlowControl(r12) return r12 L96: if (r3 != 0) goto L99 return r5 L99: throw r3 L9a: java.io.IOException r12 = new java.io.IOException // Catch: java.lang.Throwable -> L25 java.lang.String r13 = "stream closed" r12.(r13) // Catch: java.lang.Throwable -> L25 throw r12 // Catch: java.lang.Throwable -> L25 La2: okhttp3.internal.http2.Http2Stream r13 = okhttp3.internal.http2.Http2Stream.this // Catch: java.lang.Throwable -> L83 okhttp3.internal.http2.Http2Stream$StreamTimeout r13 = r13.readTimeout // Catch: java.lang.Throwable -> L83 r13.exitAndThrowIfTimedOut() // Catch: java.lang.Throwable -> L83 throw r12 // Catch: java.lang.Throwable -> L83 Laa: monitor-exit(r2) // Catch: java.lang.Throwable -> L83 throw r12 Lac: java.lang.IllegalArgumentException r12 = new java.lang.IllegalArgumentException java.lang.StringBuilder r0 = new java.lang.StringBuilder r0.() java.lang.String r1 = "byteCount < 0: " r0.append(r1) r0.append(r13) java.lang.String r13 = r0.toString() r12.(r13) throw r12 */ throw new UnsupportedOperationException("Method not decompiled: okhttp3.internal.http2.Http2Stream.FramingSource.read(okio.Buffer, long):long"); } public final void updateConnectionFlowControl(long j) { Http2Stream.this.connection.updateConnectionFlowControl(j); } public void receive(BufferedSource bufferedSource, long j) { boolean z; boolean z2; long j2; while (j > 0) { synchronized (Http2Stream.this) { z = this.finished; z2 = this.readBuffer.size() + j > this.maxByteCount; } if (z2) { bufferedSource.skip(j); Http2Stream.this.closeLater(ErrorCode.FLOW_CONTROL_ERROR); return; } if (z) { bufferedSource.skip(j); return; } long read = bufferedSource.read(this.receiveBuffer, j); if (read == -1) { throw new EOFException(); } j -= read; synchronized (Http2Stream.this) { try { if (this.closed) { j2 = this.receiveBuffer.size(); this.receiveBuffer.clear(); } else { boolean z3 = this.readBuffer.size() == 0; this.readBuffer.writeAll(this.receiveBuffer); if (z3) { Http2Stream.this.notifyAll(); } j2 = 0; } } finally { } } if (j2 > 0) { updateConnectionFlowControl(j2); } } } @Override // okio.Source public Timeout timeout() { return Http2Stream.this.readTimeout; } @Override // okio.Source, java.io.Closeable, java.lang.AutoCloseable public void close() { long size; synchronized (Http2Stream.this) { this.closed = true; size = this.readBuffer.size(); this.readBuffer.clear(); Http2Stream.this.notifyAll(); } if (size > 0) { updateConnectionFlowControl(size); } Http2Stream.this.cancelStreamIfNecessary(); } } public void cancelStreamIfNecessary() { boolean z; boolean isOpen; synchronized (this) { try { FramingSource framingSource = this.source; if (!framingSource.finished && framingSource.closed) { FramingSink framingSink = this.sink; if (!framingSink.finished) { if (framingSink.closed) { } } z = true; isOpen = isOpen(); } z = false; isOpen = isOpen(); } catch (Throwable th) { throw th; } } if (z) { close(ErrorCode.CANCEL, null); } else { if (isOpen) { return; } this.connection.removeStream(this.id); } } public final class FramingSink implements Sink { public boolean closed; public boolean finished; public final Buffer sendBuffer = new Buffer(); public Headers trailers; public FramingSink() { } @Override // okio.Sink public void write(Buffer buffer, long j) { this.sendBuffer.write(buffer, j); while (this.sendBuffer.size() >= PlaybackStateCompat.ACTION_PREPARE) { emitFrame(false); } } public final void emitFrame(boolean z) { Http2Stream http2Stream; long min; Http2Stream http2Stream2; boolean z2; synchronized (Http2Stream.this) { Http2Stream.this.writeTimeout.enter(); while (true) { try { http2Stream = Http2Stream.this; if (http2Stream.bytesLeftInWriteWindow > 0 || this.finished || this.closed || http2Stream.errorCode != null) { break; } else { http2Stream.waitForIo(); } } finally { Http2Stream.this.writeTimeout.exitAndThrowIfTimedOut(); } } http2Stream.writeTimeout.exitAndThrowIfTimedOut(); Http2Stream.this.checkOutNotClosed(); min = Math.min(Http2Stream.this.bytesLeftInWriteWindow, this.sendBuffer.size()); http2Stream2 = Http2Stream.this; http2Stream2.bytesLeftInWriteWindow -= min; } http2Stream2.writeTimeout.enter(); if (z) { try { if (min == this.sendBuffer.size()) { z2 = true; boolean z3 = z2; Http2Stream http2Stream3 = Http2Stream.this; http2Stream3.connection.writeData(http2Stream3.id, z3, this.sendBuffer, min); } } catch (Throwable th) { throw th; } } z2 = false; boolean z32 = z2; Http2Stream http2Stream32 = Http2Stream.this; http2Stream32.connection.writeData(http2Stream32.id, z32, this.sendBuffer, min); } @Override // okio.Sink, java.io.Flushable public void flush() { synchronized (Http2Stream.this) { Http2Stream.this.checkOutNotClosed(); } while (this.sendBuffer.size() > 0) { emitFrame(false); Http2Stream.this.connection.flush(); } } @Override // okio.Sink public Timeout timeout() { return Http2Stream.this.writeTimeout; } @Override // okio.Sink, java.io.Closeable, java.lang.AutoCloseable public void close() { synchronized (Http2Stream.this) { try { if (this.closed) { return; } if (!Http2Stream.this.sink.finished) { boolean z = this.sendBuffer.size() > 0; if (this.trailers != null) { while (this.sendBuffer.size() > 0) { emitFrame(false); } Http2Stream http2Stream = Http2Stream.this; http2Stream.connection.writeHeaders(http2Stream.id, true, Util.toHeaderBlock(this.trailers)); } else if (z) { while (this.sendBuffer.size() > 0) { emitFrame(true); } } else { Http2Stream http2Stream2 = Http2Stream.this; http2Stream2.connection.writeData(http2Stream2.id, true, null, 0L); } } synchronized (Http2Stream.this) { this.closed = true; } Http2Stream.this.connection.flush(); Http2Stream.this.cancelStreamIfNecessary(); } catch (Throwable th) { throw th; } } } } public void addBytesToWriteWindow(long j) { this.bytesLeftInWriteWindow += j; if (j > 0) { notifyAll(); } } public void checkOutNotClosed() { FramingSink framingSink = this.sink; if (framingSink.closed) { throw new IOException("stream closed"); } if (framingSink.finished) { throw new IOException("stream finished"); } if (this.errorCode != null) { IOException iOException = this.errorException; if (iOException == null) { throw new StreamResetException(this.errorCode); } } } public void waitForIo() { try { wait(); } catch (InterruptedException unused) { Thread.currentThread().interrupt(); throw new InterruptedIOException(); } } public class StreamTimeout extends AsyncTimeout { public StreamTimeout() { } @Override // okio.AsyncTimeout public void timedOut() { Http2Stream.this.closeLater(ErrorCode.CANCEL); Http2Stream.this.connection.sendDegradedPingLater(); } @Override // okio.AsyncTimeout public IOException newTimeoutException(IOException iOException) { SocketTimeoutException socketTimeoutException = new SocketTimeoutException("timeout"); if (iOException != null) { socketTimeoutException.initCause(iOException); } return socketTimeoutException; } public void exitAndThrowIfTimedOut() { if (exit()) { throw newTimeoutException(null); } } } }