Skip to content

Commit

Permalink
Inline single-use method in web socket writer.
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeWharton committed Sep 20, 2016
1 parent 6306aec commit 814d254
Showing 1 changed file with 10 additions and 17 deletions.
27 changes: 10 additions & 17 deletions okhttp/src/main/java/okhttp3/internal/ws/WebSocketWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.util.Random;
import okio.Buffer;
import okio.BufferedSink;
import okio.BufferedSource;
import okio.ByteString;
import okio.Sink;
import okio.Timeout;
Expand Down Expand Up @@ -185,7 +184,6 @@ void writeMessageFrameSynchronized(int formatOpcode, long byteCount, boolean isF
int b1 = 0;
if (isClient) {
b1 |= B1_FLAG_MASK;
random.nextBytes(maskKey);
}
if (byteCount <= PAYLOAD_BYTE_MAX) {
b1 |= (int) byteCount;
Expand All @@ -201,29 +199,24 @@ void writeMessageFrameSynchronized(int formatOpcode, long byteCount, boolean isF
}

if (isClient) {
random.nextBytes(maskKey);
sink.write(maskKey);
writeMaskedSynchronized(buffer, byteCount);

for (long written = 0; written < byteCount; ) {
int toRead = (int) Math.min(byteCount, maskBuffer.length);
int read = buffer.read(maskBuffer, 0, toRead);
if (read == -1) throw new AssertionError();
toggleMask(maskBuffer, read, maskKey, written);
sink.write(maskBuffer, 0, read);
written += read;
}
} else {
sink.write(buffer, byteCount);
}

sink.emit();
}

private void writeMaskedSynchronized(BufferedSource source, long byteCount) throws IOException {
assert Thread.holdsLock(this);

long written = 0;
while (written < byteCount) {
int toRead = (int) Math.min(byteCount, maskBuffer.length);
int read = source.read(maskBuffer, 0, toRead);
if (read == -1) throw new AssertionError();
toggleMask(maskBuffer, read, maskKey, written);
sink.write(maskBuffer, 0, read);
written += read;
}
}

final class FrameSink implements Sink {
int formatOpcode;
long contentLength;
Expand Down

0 comments on commit 814d254

Please sign in to comment.