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));
}
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;
}
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());
}
}
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;
}
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());
}
}
Aggregations