Rabbit-R1/original r1/java/sources/okhttp3/internal/ws/WebSocketWriter.java

173 lines
9.1 KiB
Java
Raw Normal View History

2024-05-21 16:08:36 -05:00
package okhttp3.internal.ws;
import io.sentry.clientreport.DiscardedEvent;
import java.io.Closeable;
import java.io.IOException;
import java.util.Random;
import kotlin.Metadata;
import kotlin.jvm.internal.Intrinsics;
import okio.Buffer;
import okio.BufferedSink;
import okio.ByteString;
/* compiled from: WebSocketWriter.kt */
@Metadata(bv = {1, 0, 3}, d1 = {"\u0000V\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u000b\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0010\t\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u0012\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0007\n\u0002\u0010\u0002\n\u0002\b\u0002\n\u0002\u0010\b\n\u0000\n\u0002\u0018\u0002\n\u0002\b\t\u0018\u00002\u00020\u0001B5\u0012\u0006\u0010\u0002\u001a\u00020\u0003\u0012\u0006\u0010\u0004\u001a\u00020\u0005\u0012\u0006\u0010\u0006\u001a\u00020\u0007\u0012\u0006\u0010\b\u001a\u00020\u0003\u0012\u0006\u0010\t\u001a\u00020\u0003\u0012\u0006\u0010\n\u001a\u00020\u000b¢\u0006\u0002\u0010\fJ\b\u0010\u001b\u001a\u00020\u001cH\u0016J\u0018\u0010\u001d\u001a\u00020\u001c2\u0006\u0010\u001e\u001a\u00020\u001f2\b\u0010 \u001a\u0004\u0018\u00010!J\u0018\u0010\"\u001a\u00020\u001c2\u0006\u0010#\u001a\u00020\u001f2\u0006\u0010$\u001a\u00020!H\u0002J\u0016\u0010%\u001a\u00020\u001c2\u0006\u0010&\u001a\u00020\u001f2\u0006\u0010'\u001a\u00020!J\u000e\u0010(\u001a\u00020\u001c2\u0006\u0010$\u001a\u00020!J\u000e\u0010)\u001a\u00020\u001c2\u0006\u0010$\u001a\u00020!R\u000e\u0010\u0002\u001a\u00020\u0003X\u0082\u0004¢\u0006\u0002\n\u0000R\u0010\u0010\r\u001a\u0004\u0018\u00010\u000eX\u0082\u0004¢\u0006\u0002\n\u0000R\u0010\u0010\u000f\u001a\u0004\u0018\u00010\u0010X\u0082\u0004¢\u0006\u0002\n\u0000R\u000e\u0010\u0011\u001a\u00020\u0012X\u0082\u0004¢\u0006\u0002\n\u0000R\u0010\u0010\u0013\u001a\u0004\u0018\u00010\u0014X\u0082\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\n\u001a\u00020\u000bX\u0082\u0004¢\u0006\u0002\n\u0000R\u000e\u0010\t\u001a\u00020\u0003X\u0082\u0004¢\u0006\u0002\n\u0000R\u000e\u0010\b\u001a\u00020\u0003X\u0082\u0004¢\u0006\u0002\n\u0000R\u0011\u0010\u0006\u001a\u00020\u0007¢\u0006\b\n\u0000\u001a\u0004\b\u0015\u0010\u0016R\u0011\u0010\u0004\u001a\u00020\u0005¢\u0006\b\n\u0000\u001a\u0004\b\u0017\u0010\u0018R\u000e\u0010\u0019\u001a\u00020\u0012X\u0082\u0004¢\u0006\u0002\n\u0000R\u000e\u0010\u001a\u001a\u00020\u0003X\u0082\u000e¢\u0006\u0002\n\u0000¨\u0006*"}, d2 = {"Lokhttp3/internal/ws/WebSocketWriter;", "Ljava/io/Closeable;", "isClient", "", "sink", "Lokio/BufferedSink;", "random", "Ljava/util/Random;", "perMessageDeflate", "noContextTakeover", "minimumDeflateSize", "", "(ZLokio/BufferedSink;Ljava/util/Random;ZZJ)V", "maskCursor", "Lokio/Buffer$UnsafeCursor;", "maskKey", "", "messageBuffer", "Lokio/Buffer;", "messageDeflater", "Lokhttp3/internal/ws/MessageDeflater;", "getRandom", "()Ljava/util/Random;", "getSink", "()Lokio/BufferedSink;", "sinkBuffer", "writerClosed", "close", "", "writeClose", "code", "", DiscardedEvent.JsonKeys.REASON, "Lokio/ByteString;", "writeControlFrame", "opcode", "payload", "writeMessageFrame", "formatOpcode", "data", "writePing", "writePong", "okhttp"}, k = 1, mv = {1, 4, 0})
/* loaded from: classes3.dex */
public final class WebSocketWriter implements Closeable {
private final boolean isClient;
private final Buffer.UnsafeCursor maskCursor;
private final byte[] maskKey;
private final Buffer messageBuffer;
private MessageDeflater messageDeflater;
private final long minimumDeflateSize;
private final boolean noContextTakeover;
private final boolean perMessageDeflate;
private final Random random;
private final BufferedSink sink;
private final Buffer sinkBuffer;
private boolean writerClosed;
public final Random getRandom() {
return this.random;
}
public final BufferedSink getSink() {
return this.sink;
}
public WebSocketWriter(boolean z, BufferedSink sink, Random random, boolean z2, boolean z3, long j) {
Intrinsics.checkNotNullParameter(sink, "sink");
Intrinsics.checkNotNullParameter(random, "random");
this.isClient = z;
this.sink = sink;
this.random = random;
this.perMessageDeflate = z2;
this.noContextTakeover = z3;
this.minimumDeflateSize = j;
this.messageBuffer = new Buffer();
this.sinkBuffer = sink.getBuffer();
this.maskKey = z ? new byte[4] : null;
this.maskCursor = z ? new Buffer.UnsafeCursor() : null;
}
public final void writePing(ByteString payload) throws IOException {
Intrinsics.checkNotNullParameter(payload, "payload");
writeControlFrame(9, payload);
}
public final void writePong(ByteString payload) throws IOException {
Intrinsics.checkNotNullParameter(payload, "payload");
writeControlFrame(10, payload);
}
public final void writeClose(int code, ByteString reason) throws IOException {
ByteString byteString = ByteString.EMPTY;
if (code != 0 || reason != null) {
if (code != 0) {
WebSocketProtocol.INSTANCE.validateCloseCode(code);
}
Buffer buffer = new Buffer();
buffer.writeShort(code);
if (reason != null) {
buffer.write(reason);
}
byteString = buffer.readByteString();
}
try {
writeControlFrame(8, byteString);
} finally {
this.writerClosed = true;
}
}
private final void writeControlFrame(int opcode, ByteString payload) throws IOException {
if (this.writerClosed) {
throw new IOException("closed");
}
int size = payload.size();
if (!(((long) size) <= 125)) {
throw new IllegalArgumentException("Payload size must be less than or equal to 125".toString());
}
this.sinkBuffer.writeByte(opcode | 128);
if (this.isClient) {
this.sinkBuffer.writeByte(size | 128);
Random random = this.random;
byte[] bArr = this.maskKey;
Intrinsics.checkNotNull(bArr);
random.nextBytes(bArr);
this.sinkBuffer.write(this.maskKey);
if (size > 0) {
long size2 = this.sinkBuffer.size();
this.sinkBuffer.write(payload);
Buffer buffer = this.sinkBuffer;
Buffer.UnsafeCursor unsafeCursor = this.maskCursor;
Intrinsics.checkNotNull(unsafeCursor);
buffer.readAndWriteUnsafe(unsafeCursor);
this.maskCursor.seek(size2);
WebSocketProtocol.INSTANCE.toggleMask(this.maskCursor, this.maskKey);
this.maskCursor.close();
}
} else {
this.sinkBuffer.writeByte(size);
this.sinkBuffer.write(payload);
}
this.sink.flush();
}
public final void writeMessageFrame(int formatOpcode, ByteString data) throws IOException {
Intrinsics.checkNotNullParameter(data, "data");
if (this.writerClosed) {
throw new IOException("closed");
}
this.messageBuffer.write(data);
int i = formatOpcode | 128;
if (this.perMessageDeflate && data.size() >= this.minimumDeflateSize) {
MessageDeflater messageDeflater = this.messageDeflater;
if (messageDeflater == null) {
messageDeflater = new MessageDeflater(this.noContextTakeover);
this.messageDeflater = messageDeflater;
}
messageDeflater.deflate(this.messageBuffer);
i = formatOpcode | 192;
}
long size = this.messageBuffer.size();
this.sinkBuffer.writeByte(i);
int i2 = this.isClient ? 128 : 0;
if (size <= 125) {
this.sinkBuffer.writeByte(i2 | ((int) size));
} else if (size <= WebSocketProtocol.PAYLOAD_SHORT_MAX) {
this.sinkBuffer.writeByte(i2 | WebSocketProtocol.PAYLOAD_SHORT);
this.sinkBuffer.writeShort((int) size);
} else {
this.sinkBuffer.writeByte(i2 | 127);
this.sinkBuffer.writeLong(size);
}
if (this.isClient) {
Random random = this.random;
byte[] bArr = this.maskKey;
Intrinsics.checkNotNull(bArr);
random.nextBytes(bArr);
this.sinkBuffer.write(this.maskKey);
if (size > 0) {
Buffer buffer = this.messageBuffer;
Buffer.UnsafeCursor unsafeCursor = this.maskCursor;
Intrinsics.checkNotNull(unsafeCursor);
buffer.readAndWriteUnsafe(unsafeCursor);
this.maskCursor.seek(0L);
WebSocketProtocol.INSTANCE.toggleMask(this.maskCursor, this.maskKey);
this.maskCursor.close();
}
}
this.sinkBuffer.write(this.messageBuffer, size);
this.sink.emit();
}
@Override // java.io.Closeable, java.lang.AutoCloseable
public void close() {
MessageDeflater messageDeflater = this.messageDeflater;
if (messageDeflater != null) {
messageDeflater.close();
}
}
}