package okhttp3.internal.http2; import java.io.Closeable; import java.io.IOException; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import o.C14953gcr; import o.C14957gcv; import o.C15111ghZ; import o.InterfaceC15148gic; import okhttp3.internal.Util; import okhttp3.internal.http2.Hpack; /* loaded from: classes.dex */ public final class Http2Writer implements Closeable { public static final Companion Companion = new Companion(null); private static final Logger logger = Logger.getLogger(Http2.class.getName()); private final boolean client; private boolean closed; private final C15111ghZ hpackBuffer; private final Hpack.Writer hpackWriter; private int maxFrameSize; private final InterfaceC15148gic sink; public Http2Writer(InterfaceC15148gic interfaceC15148gic, boolean z) { C14957gcv.e(interfaceC15148gic, ""); this.sink = interfaceC15148gic; this.client = z; C15111ghZ c15111ghZ = new C15111ghZ(); this.hpackBuffer = c15111ghZ; this.maxFrameSize = 16384; this.hpackWriter = new Hpack.Writer(0, false, c15111ghZ, 3, null); } public final void connectionPreface() throws IOException { synchronized (this) { if (this.closed) { throw new IOException("closed"); } if (this.client) { Logger logger2 = logger; if (logger2.isLoggable(Level.FINE)) { logger2.fine(Util.format(C14957gcv.c(">> CONNECTION ", (Object) Http2.CONNECTION_PREFACE.c()), new Object[0])); } this.sink.b(Http2.CONNECTION_PREFACE); this.sink.flush(); } } } public final void applyAndAckSettings(Settings settings) throws IOException { synchronized (this) { C14957gcv.e(settings, ""); if (this.closed) { throw new IOException("closed"); } this.maxFrameSize = settings.getMaxFrameSize(this.maxFrameSize); if (settings.getHeaderTableSize() != -1) { this.hpackWriter.resizeHeaderTable(settings.getHeaderTableSize()); } frameHeader(0, 0, 4, 1); this.sink.flush(); } } public final void pushPromise(int i, int i2, List
list) throws IOException { synchronized (this) { C14957gcv.e(list, ""); if (this.closed) { throw new IOException("closed"); } this.hpackWriter.writeHeaders(list); long j = this.hpackBuffer.c; int min = (int) Math.min(this.maxFrameSize - 4, j); long j2 = min; frameHeader(i, min + 4, 5, j == j2 ? 4 : 0); this.sink.j(i2 & Integer.MAX_VALUE); this.sink.write(this.hpackBuffer, j2); if (j > j2) { writeContinuationFrames(i, j - j2); } } } public final void flush() throws IOException { synchronized (this) { if (this.closed) { throw new IOException("closed"); } this.sink.flush(); } } public final void rstStream(int i, ErrorCode errorCode) throws IOException { synchronized (this) { C14957gcv.e(errorCode, ""); if (this.closed) { throw new IOException("closed"); } if (errorCode.getHttpCode() == -1) { throw new IllegalArgumentException("Failed requirement.".toString()); } frameHeader(i, 4, 3, 0); this.sink.j(errorCode.getHttpCode()); this.sink.flush(); } } public final void data(boolean z, int i, C15111ghZ c15111ghZ, int i2) throws IOException { synchronized (this) { if (this.closed) { throw new IOException("closed"); } dataFrame(i, z ? 1 : 0, c15111ghZ, i2); } } public final void dataFrame(int i, int i2, C15111ghZ c15111ghZ, int i3) throws IOException { frameHeader(i, i3, 0, i2); if (i3 > 0) { InterfaceC15148gic interfaceC15148gic = this.sink; C14957gcv.e(c15111ghZ); interfaceC15148gic.write(c15111ghZ, i3); } } public final void settings(Settings settings) throws IOException { synchronized (this) { C14957gcv.e(settings, ""); if (this.closed) { throw new IOException("closed"); } int i = 0; frameHeader(0, settings.size() * 6, 4, 0); while (i < 10) { if (settings.isSet(i)) { this.sink.g(i != 4 ? i != 7 ? i : 4 : 3); this.sink.j(settings.get(i)); } i++; } this.sink.flush(); } } public final void ping(boolean z, int i, int i2) throws IOException { synchronized (this) { if (this.closed) { throw new IOException("closed"); } frameHeader(0, 8, 6, z ? 1 : 0); this.sink.j(i); this.sink.j(i2); this.sink.flush(); } } public final void goAway(int i, ErrorCode errorCode, byte[] bArr) throws IOException { synchronized (this) { C14957gcv.e(errorCode, ""); C14957gcv.e(bArr, ""); if (this.closed) { throw new IOException("closed"); } if (errorCode.getHttpCode() == -1) { throw new IllegalArgumentException("errorCode.httpCode == -1".toString()); } frameHeader(0, bArr.length + 8, 7, 0); this.sink.j(i); this.sink.j(errorCode.getHttpCode()); if (!(bArr.length == 0)) { this.sink.a(bArr); } this.sink.flush(); } } public final void windowUpdate(int i, long j) throws IOException { synchronized (this) { if (this.closed) { throw new IOException("closed"); } if (j == 0 || j > 2147483647L) { throw new IllegalArgumentException(C14957gcv.c("windowSizeIncrement == 0 || windowSizeIncrement > 0x7fffffffL: ", Long.valueOf(j)).toString()); } frameHeader(i, 4, 8, 0); this.sink.j((int) j); this.sink.flush(); } } public final void frameHeader(int i, int i2, int i3, int i4) throws IOException { Logger logger2 = logger; if (logger2.isLoggable(Level.FINE)) { logger2.fine(Http2.INSTANCE.frameLog(false, i, i2, i3, i4)); } if (i2 > this.maxFrameSize) { StringBuilder sb = new StringBuilder("FRAME_SIZE_ERROR length > "); sb.append(this.maxFrameSize); sb.append(": "); sb.append(i2); throw new IllegalArgumentException(sb.toString().toString()); } if ((Integer.MIN_VALUE & i) != 0) { throw new IllegalArgumentException(C14957gcv.c("reserved bit set: ", Integer.valueOf(i)).toString()); } Util.writeMedium(this.sink, i2); this.sink.b(i3 & 255); this.sink.b(i4 & 255); this.sink.j(i & Integer.MAX_VALUE); } @Override // java.io.Closeable, java.lang.AutoCloseable public final void close() throws IOException { synchronized (this) { this.closed = true; this.sink.close(); } } private final void writeContinuationFrames(int i, long j) throws IOException { while (j > 0) { long min = Math.min(this.maxFrameSize, j); j -= min; frameHeader(i, (int) min, 9, j == 0 ? 4 : 0); this.sink.write(this.hpackBuffer, min); } } public final void headers(boolean z, int i, List
list) throws IOException { synchronized (this) { C14957gcv.e(list, ""); if (this.closed) { throw new IOException("closed"); } this.hpackWriter.writeHeaders(list); long j = this.hpackBuffer.c; long min = Math.min(this.maxFrameSize, j); int i2 = j == min ? 4 : 0; if (z) { i2 |= 1; } frameHeader(i, (int) min, 1, i2); this.sink.write(this.hpackBuffer, min); if (j > min) { writeContinuationFrames(i, j - min); } } } /* loaded from: classes.dex */ public static final class Companion { private Companion() { } public /* synthetic */ Companion(C14953gcr c14953gcr) { this(); } } public final int maxDataLength() { return this.maxFrameSize; } public final Hpack.Writer getHpackWriter() { return this.hpackWriter; } }