Search in sources :

Example 46 with BufferedSink

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();
}
Also used : BufferedSink(okio.BufferedSink)

Example 47 with BufferedSink

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();
    }
}
Also used : StatusLine(com.squareup.okhttp.internal.http.StatusLine) Buffer(okio.Buffer) Request(com.squareup.okhttp.Request) BufferedSink(okio.BufferedSink) ByteString(okio.ByteString) IOException(java.io.IOException) Socket(java.net.Socket) BufferedSource(okio.BufferedSource) Source(okio.Source) HttpUrl(com.squareup.okhttp.HttpUrl)

Example 48 with BufferedSink

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;
}
Also used : BufferedSink(okio.BufferedSink) IOException(java.io.IOException) IOException(java.io.IOException)

Example 49 with BufferedSink

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());
}
Also used : BufferedSink(okio.BufferedSink) RequestBody(okhttp3.RequestBody) Test(org.junit.Test)

Example 50 with BufferedSink

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");
}
Also used : BufferedSink(okio.BufferedSink) Test(org.junit.Test)

Aggregations

BufferedSink (okio.BufferedSink)91 Test (org.junit.Test)31 IOException (java.io.IOException)30 Buffer (okio.Buffer)19 File (java.io.File)15 RequestBody (okhttp3.RequestBody)14 GzipSink (okio.GzipSink)12 Source (okio.Source)11 Response (okhttp3.Response)10 BufferedSource (okio.BufferedSource)10 Request (okhttp3.Request)9 InFrame (okhttp3.internal.http2.MockHttp2Peer.InFrame)9 MediaType (okhttp3.MediaType)7 InterruptedIOException (java.io.InterruptedIOException)6 MockResponse (okhttp3.mockwebserver.MockResponse)5 Sink (okio.Sink)5 InputStream (java.io.InputStream)4 Socket (java.net.Socket)4 OkHttpClient (okhttp3.OkHttpClient)4 Request (com.squareup.okhttp.Request)3