use of okio.BufferedSink in project okhttputils by hongyangAndroid.
the class CountingRequestBody method writeTo.
@Override
public void writeTo(BufferedSink sink) throws IOException {
countingSink = new CountingSink(sink);
BufferedSink bufferedSink = Okio.buffer(countingSink);
delegate.writeTo(bufferedSink);
bufferedSink.flush();
}
use of okio.BufferedSink in project grpc-java by grpc.
the class OkHttpClientTransport method createHttpProxySocket.
private Socket createHttpProxySocket(InetSocketAddress address, InetSocketAddress proxyAddress, String proxyUsername, String proxyPassword) throws IOException, StatusException {
try {
Socket sock = new Socket(proxyAddress.getAddress(), proxyAddress.getPort());
sock.setTcpNoDelay(true);
Source source = Okio.source(sock);
BufferedSink sink = Okio.buffer(Okio.sink(sock));
// Prepare headers and request method line
Request proxyRequest = createHttpProxyRequest(address, proxyUsername, proxyPassword);
HttpUrl url = proxyRequest.httpUrl();
String requestLine = String.format("CONNECT %s:%d HTTP/1.1", url.host(), url.port());
// Write request to socket
sink.writeUtf8(requestLine).writeUtf8("\r\n");
for (int i = 0, size = proxyRequest.headers().size(); i < size; i++) {
sink.writeUtf8(proxyRequest.headers().name(i)).writeUtf8(": ").writeUtf8(proxyRequest.headers().value(i)).writeUtf8("\r\n");
}
sink.writeUtf8("\r\n");
// Flush buffer (flushes socket and sends request)
sink.flush();
// Read status line, check if 2xx was returned
StatusLine statusLine = StatusLine.parse(readUtf8LineStrictUnbuffered(source));
// Drain rest of headers
while (!readUtf8LineStrictUnbuffered(source).equals("")) {
}
if (statusLine.code < 200 || statusLine.code >= 300) {
Buffer body = new Buffer();
try {
sock.shutdownOutput();
source.read(body, 1024);
} catch (IOException ex) {
body.writeUtf8("Unable to read body: " + ex.toString());
}
try {
sock.close();
} catch (IOException ignored) {
// ignored
}
String message = String.format("Response returned from proxy was not successful (expected 2xx, got %d %s). " + "Response body:\n%s", statusLine.code, statusLine.message, body.readUtf8());
throw Status.UNAVAILABLE.withDescription(message).asException();
}
return sock;
} catch (IOException e) {
throw Status.UNAVAILABLE.withDescription("Failed trying to connect with proxy").withCause(e).asException();
}
}
use of okio.BufferedSink in project u2020 by JakeWharton.
the class SocketActivityHierarchyServer method writeValue.
private static boolean writeValue(Socket client, String value) {
boolean result;
BufferedSink out = null;
try {
out = Okio.buffer(Okio.sink(client));
out.writeUtf8(value);
out.writeByte('\n');
out.flush();
result = true;
} catch (Exception e) {
result = false;
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
result = false;
}
}
}
return result;
}
use of okio.BufferedSink in project okhttp by square.
the class WebSocketWriterTest method serverSmallBufferedPayloadWrittenAsOneFrame.
@Test
public void serverSmallBufferedPayloadWrittenAsOneFrame() throws IOException {
int length = 5;
byte[] bytes = binaryData(length);
RequestBody body = RequestBody.create(null, bytes);
BufferedSink sink = Okio.buffer(serverWriter.newMessageSink(OPCODE_TEXT, length));
body.writeTo(sink);
sink.close();
assertData("8105");
assertData(bytes);
assertTrue(data.exhausted());
}
use of okio.BufferedSink in project okhttp by square.
the class WebSocketWriterTest method closeFlushes.
@Test
public void closeFlushes() throws IOException {
BufferedSink sink = Okio.buffer(serverWriter.newMessageSink(OPCODE_TEXT, -1));
sink.writeUtf8("Hel").flush();
assertData("010348656c");
sink.writeUtf8("lo").close();
assertData("80026c6f");
}
Aggregations