Search in sources :

Example 21 with BufferedSink

use of okio.BufferedSink in project okhttp by square.

the class Http2ConnectionTest method peerSetsZeroFlowControl.

/**
   * Webservers may set the initial window size to zero, which is a special case because it means
   * that we have to flush headers immediately before any request body can be sent.
   * https://github.com/square/okhttp/issues/2543
   */
@Test
public void peerSetsZeroFlowControl() throws Exception {
    peer.setClient(true);
    // Write the mocking script.
    peer.sendFrame().settings(new Settings().set(INITIAL_WINDOW_SIZE, 0));
    // ACK
    peer.acceptFrame();
    // Increase the connection window size.
    peer.sendFrame().windowUpdate(0, 10);
    // PING
    peer.acceptFrame();
    peer.sendFrame().ping(true, 1, 0);
    // HEADERS STREAM 3
    peer.acceptFrame();
    peer.sendFrame().windowUpdate(3, 5);
    // DATA STREAM 3 "abcde"
    peer.acceptFrame();
    peer.sendFrame().windowUpdate(3, 5);
    // DATA STREAM 3 "fghi"
    peer.acceptFrame();
    peer.play();
    // Play it back.
    Http2Connection connection = connect(peer);
    // Ensure the SETTINGS have been received.
    connection.ping().roundTripTime();
    Http2Stream stream = connection.newStream(headerEntries("a", "android"), true);
    BufferedSink sink = Okio.buffer(stream.getSink());
    sink.writeUtf8("abcdefghi");
    sink.flush();
    // Verify the peer received what was expected.
    // PING
    peer.takeFrame();
    InFrame headers = peer.takeFrame();
    assertEquals(Http2.TYPE_HEADERS, headers.type);
    InFrame data1 = peer.takeFrame();
    assertEquals(Http2.TYPE_DATA, data1.type);
    assertEquals(3, data1.streamId);
    assertTrue(Arrays.equals("abcde".getBytes("UTF-8"), data1.data));
    InFrame data2 = peer.takeFrame();
    assertEquals(Http2.TYPE_DATA, data2.type);
    assertEquals(3, data2.streamId);
    assertTrue(Arrays.equals("fghi".getBytes("UTF-8"), data2.data));
}
Also used : InFrame(okhttp3.internal.http2.MockHttp2Peer.InFrame) BufferedSink(okio.BufferedSink) Test(org.junit.Test)

Example 22 with BufferedSink

use of okio.BufferedSink in project okhttp by square.

the class URLConnectionTest method gzip.

/** Returns a gzipped copy of {@code bytes}. */
public Buffer gzip(String data) throws IOException {
    Buffer result = new Buffer();
    BufferedSink gzipSink = Okio.buffer(new GzipSink(result));
    gzipSink.writeUtf8(data);
    gzipSink.close();
    return result;
}
Also used : Buffer(okio.Buffer) GzipSink(okio.GzipSink) BufferedSink(okio.BufferedSink)

Example 23 with BufferedSink

use of okio.BufferedSink in project okhttp by square.

the class PostStreaming method run.

public void run() throws Exception {
    RequestBody requestBody = new RequestBody() {

        @Override
        public MediaType contentType() {
            return MEDIA_TYPE_MARKDOWN;
        }

        @Override
        public void writeTo(BufferedSink sink) throws IOException {
            sink.writeUtf8("Numbers\n");
            sink.writeUtf8("-------\n");
            for (int i = 2; i <= 997; i++) {
                sink.writeUtf8(String.format(" * %s = %s\n", i, factor(i)));
            }
        }

        private String factor(int n) {
            for (int i = 2; i < n; i++) {
                int x = n / i;
                if (x * i == n)
                    return factor(x) + " × " + i;
            }
            return Integer.toString(n);
        }
    };
    Request request = new Request.Builder().url("https://api.github.com/markdown/raw").post(requestBody).build();
    try (Response response = client.newCall(request).execute()) {
        if (!response.isSuccessful())
            throw new IOException("Unexpected code " + response);
        System.out.println(response.body().string());
    }
}
Also used : Response(okhttp3.Response) Request(okhttp3.Request) BufferedSink(okio.BufferedSink) IOException(java.io.IOException) RequestBody(okhttp3.RequestBody)

Example 24 with BufferedSink

use of okio.BufferedSink in project okhttp by square.

the class CallTest method gzip.

private Buffer gzip(String data) throws IOException {
    Buffer result = new Buffer();
    BufferedSink sink = Okio.buffer(new GzipSink(result));
    sink.writeUtf8(data);
    sink.close();
    return result;
}
Also used : Buffer(okio.Buffer) GzipSink(okio.GzipSink) BufferedSink(okio.BufferedSink)

Example 25 with BufferedSink

use of okio.BufferedSink in project okhttp by square.

the class WebSocketWriterTest method noWritesAfterClose.

@Test
public void noWritesAfterClose() throws IOException {
    Sink sink = serverWriter.newMessageSink(OPCODE_TEXT, -1);
    sink.close();
    assertData("8100");
    Buffer payload = new Buffer().writeUtf8("Hello");
    try {
        // Write to the unbuffered sink as BufferedSink keeps its own closed state.
        sink.write(payload, payload.size());
        fail();
    } catch (IOException e) {
        assertEquals("closed", e.getMessage());
    }
}
Also used : Buffer(okio.Buffer) Sink(okio.Sink) BufferedSink(okio.BufferedSink) IOException(java.io.IOException) 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