what-the-bank/sources/okhttp3/internal/ws/WebSocketWriter.java

185 lines
6.4 KiB
Java
Raw Permalink Normal View History

2024-07-27 18:17:47 +07:00
package okhttp3.internal.ws;
import java.io.Closeable;
import java.io.IOException;
import java.util.Random;
import o.C14957gcv;
import o.C15111ghZ;
import o.C15155gij;
import o.InterfaceC15148gic;
import o.giJ;
/* loaded from: classes6.dex */
public final class WebSocketWriter implements Closeable {
private final boolean isClient;
private final C15111ghZ.RVV maskCursor;
private final byte[] maskKey;
private final C15111ghZ messageBuffer;
private MessageDeflater messageDeflater;
private final long minimumDeflateSize;
private final boolean noContextTakeover;
private final boolean perMessageDeflate;
private final Random random;
private final InterfaceC15148gic sink;
private final C15111ghZ sinkBuffer;
private boolean writerClosed;
public WebSocketWriter(boolean z, InterfaceC15148gic interfaceC15148gic, Random random, boolean z2, boolean z3, long j) {
C14957gcv.e(interfaceC15148gic, "");
C14957gcv.e(random, "");
this.isClient = z;
this.sink = interfaceC15148gic;
this.random = random;
this.perMessageDeflate = z2;
this.noContextTakeover = z3;
this.minimumDeflateSize = j;
this.messageBuffer = new C15111ghZ();
this.sinkBuffer = interfaceC15148gic.i();
this.maskKey = z ? new byte[4] : null;
this.maskCursor = z ? new C15111ghZ.RVV() : null;
}
public final void writePing(C15155gij c15155gij) throws IOException {
C14957gcv.e(c15155gij, "");
writeControlFrame(9, c15155gij);
}
public final void writePong(C15155gij c15155gij) throws IOException {
C14957gcv.e(c15155gij, "");
writeControlFrame(10, c15155gij);
}
public final void writeClose(int i, C15155gij c15155gij) throws IOException {
C15155gij c15155gij2 = C15155gij.e;
if (i != 0 || c15155gij != null) {
if (i != 0) {
WebSocketProtocol.INSTANCE.validateCloseCode(i);
}
C15111ghZ c15111ghZ = new C15111ghZ();
c15111ghZ.g(i);
if (c15155gij != null) {
C14957gcv.e(c15155gij, "");
c15155gij.a(c15111ghZ, c15155gij.a());
}
c15155gij2 = c15111ghZ.c(c15111ghZ.c);
}
try {
writeControlFrame(8, c15155gij2);
} finally {
this.writerClosed = true;
}
}
public final void writeMessageFrame(int i, C15155gij c15155gij) throws IOException {
C14957gcv.e(c15155gij, "");
if (this.writerClosed) {
throw new IOException("closed");
}
C15111ghZ c15111ghZ = this.messageBuffer;
C14957gcv.e(c15155gij, "");
c15155gij.a(c15111ghZ, c15155gij.a());
int i2 = i | 128;
if (this.perMessageDeflate && c15155gij.a() >= this.minimumDeflateSize) {
MessageDeflater messageDeflater = this.messageDeflater;
if (messageDeflater == null) {
messageDeflater = new MessageDeflater(this.noContextTakeover);
this.messageDeflater = messageDeflater;
}
messageDeflater.deflate(this.messageBuffer);
i2 = i | 192;
}
long j = this.messageBuffer.c;
this.sinkBuffer.b(i2);
int i3 = this.isClient ? 128 : 0;
if (j <= 125) {
this.sinkBuffer.b(i3 | ((int) j));
} else if (j <= WebSocketProtocol.PAYLOAD_SHORT_MAX) {
this.sinkBuffer.b(i3 | 126);
this.sinkBuffer.g((int) j);
} else {
this.sinkBuffer.b(i3 | 127);
this.sinkBuffer.n(j);
}
if (this.isClient) {
Random random = this.random;
byte[] bArr = this.maskKey;
C14957gcv.e(bArr);
random.nextBytes(bArr);
C15111ghZ c15111ghZ2 = this.sinkBuffer;
byte[] bArr2 = this.maskKey;
C14957gcv.e(bArr2, "");
c15111ghZ2.b(bArr2, 0, bArr2.length);
if (j > 0) {
C15111ghZ c15111ghZ3 = this.messageBuffer;
C15111ghZ.RVV rvv = this.maskCursor;
C14957gcv.e(rvv);
C14957gcv.e(rvv, "");
giJ.d(c15111ghZ3, rvv);
this.maskCursor.e(0L);
WebSocketProtocol.INSTANCE.toggleMask(this.maskCursor, this.maskKey);
this.maskCursor.close();
}
}
this.sinkBuffer.write(this.messageBuffer, j);
this.sink.a();
}
@Override // java.io.Closeable, java.lang.AutoCloseable
public final void close() {
MessageDeflater messageDeflater = this.messageDeflater;
if (messageDeflater != null) {
messageDeflater.close();
}
}
private final void writeControlFrame(int i, C15155gij c15155gij) throws IOException {
if (this.writerClosed) {
throw new IOException("closed");
}
int a = c15155gij.a();
if (a > 125) {
throw new IllegalArgumentException("Payload size must be less than or equal to 125".toString());
}
this.sinkBuffer.b(i | 128);
if (this.isClient) {
this.sinkBuffer.b(a | 128);
Random random = this.random;
byte[] bArr = this.maskKey;
C14957gcv.e(bArr);
random.nextBytes(bArr);
C15111ghZ c15111ghZ = this.sinkBuffer;
byte[] bArr2 = this.maskKey;
C14957gcv.e(bArr2, "");
c15111ghZ.b(bArr2, 0, bArr2.length);
if (a > 0) {
long j = this.sinkBuffer.c;
C15111ghZ c15111ghZ2 = this.sinkBuffer;
C14957gcv.e(c15155gij, "");
c15155gij.a(c15111ghZ2, c15155gij.a());
C15111ghZ c15111ghZ3 = this.sinkBuffer;
C15111ghZ.RVV rvv = this.maskCursor;
C14957gcv.e(rvv);
C14957gcv.e(rvv, "");
giJ.d(c15111ghZ3, rvv);
this.maskCursor.e(j);
WebSocketProtocol.INSTANCE.toggleMask(this.maskCursor, this.maskKey);
this.maskCursor.close();
}
} else {
this.sinkBuffer.b(a);
C15111ghZ c15111ghZ4 = this.sinkBuffer;
C14957gcv.e(c15155gij, "");
c15155gij.a(c15111ghZ4, c15155gij.a());
}
this.sink.flush();
}
public final InterfaceC15148gic getSink() {
return this.sink;
}
public final Random getRandom() {
return this.random;
}
}