use of okio.BufferedSink in project okhttp by square.
the class ResponseCacheTest method gzip.
/** Returns a gzipped copy of {@code bytes}. */
public 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 SocksProxy method service.
private void service(final Socket from) {
executor.execute(new NamedRunnable("SocksProxy %s", from.getRemoteSocketAddress()) {
@Override
protected void execute() {
try {
BufferedSource fromSource = Okio.buffer(Okio.source(from));
BufferedSink fromSink = Okio.buffer(Okio.sink(from));
hello(fromSource, fromSink);
acceptCommand(from.getInetAddress(), fromSource, fromSink);
} catch (IOException e) {
logger.log(Level.WARNING, name + " failed", e);
Util.closeQuietly(from);
}
}
});
}
use of okio.BufferedSink in project okhttp by square.
the class InterceptorTest method uppercase.
private RequestBody uppercase(final RequestBody original) {
return new RequestBody() {
@Override
public MediaType contentType() {
return original.contentType();
}
@Override
public long contentLength() throws IOException {
return original.contentLength();
}
@Override
public void writeTo(BufferedSink sink) throws IOException {
Sink uppercase = uppercase(sink);
BufferedSink bufferedSink = Okio.buffer(uppercase);
original.writeTo(bufferedSink);
bufferedSink.emit();
}
};
}
use of okio.BufferedSink in project okhttp by square.
the class CallTest method reusedSinksGetIndependentTimeoutInstances.
@Test
public void reusedSinksGetIndependentTimeoutInstances() throws Exception {
server.enqueue(new MockResponse());
server.enqueue(new MockResponse());
// Call 1: set a deadline on the request body.
RequestBody requestBody1 = new RequestBody() {
@Override
public MediaType contentType() {
return MediaType.parse("text/plain");
}
@Override
public void writeTo(BufferedSink sink) throws IOException {
sink.writeUtf8("abc");
sink.timeout().deadline(5, TimeUnit.SECONDS);
}
};
Request request1 = new Request.Builder().url(server.url("/")).method("POST", requestBody1).build();
Response response1 = client.newCall(request1).execute();
assertEquals(200, response1.code());
// Call 2: check for the absence of a deadline on the request body.
RequestBody requestBody2 = new RequestBody() {
@Override
public MediaType contentType() {
return MediaType.parse("text/plain");
}
@Override
public void writeTo(BufferedSink sink) throws IOException {
assertFalse(sink.timeout().hasDeadline());
sink.writeUtf8("def");
}
};
Request request2 = new Request.Builder().url(server.url("/")).method("POST", requestBody2).build();
Response response2 = client.newCall(request2).execute();
assertEquals(200, response2.code());
// Use sequence numbers to confirm the connection was pooled.
assertEquals(0, server.takeRequest().getSequenceNumber());
assertEquals(1, server.takeRequest().getSequenceNumber());
}
use of okio.BufferedSink in project okhttp by square.
the class CallServerInterceptor method intercept.
@Override
public Response intercept(Chain chain) throws IOException {
HttpCodec httpCodec = ((RealInterceptorChain) chain).httpStream();
StreamAllocation streamAllocation = ((RealInterceptorChain) chain).streamAllocation();
Request request = chain.request();
long sentRequestMillis = System.currentTimeMillis();
httpCodec.writeRequestHeaders(request);
Response.Builder responseBuilder = null;
if (HttpMethod.permitsRequestBody(request.method()) && request.body() != null) {
// we did get (such as a 4xx response) without ever transmitting the request body.
if ("100-continue".equalsIgnoreCase(request.header("Expect"))) {
httpCodec.flushRequest();
responseBuilder = httpCodec.readResponseHeaders(true);
}
// Write the request body, unless an "Expect: 100-continue" expectation failed.
if (responseBuilder == null) {
Sink requestBodyOut = httpCodec.createRequestBody(request, request.body().contentLength());
BufferedSink bufferedRequestBody = Okio.buffer(requestBodyOut);
request.body().writeTo(bufferedRequestBody);
bufferedRequestBody.close();
}
}
httpCodec.finishRequest();
if (responseBuilder == null) {
responseBuilder = httpCodec.readResponseHeaders(false);
}
Response response = responseBuilder.request(request).handshake(streamAllocation.connection().handshake()).sentRequestAtMillis(sentRequestMillis).receivedResponseAtMillis(System.currentTimeMillis()).build();
int code = response.code();
if (forWebSocket && code == 101) {
// Connection is upgrading, but we need to ensure interceptors see a non-null response body.
response = response.newBuilder().body(Util.EMPTY_RESPONSE).build();
} else {
response = response.newBuilder().body(httpCodec.openResponseBody(response)).build();
}
if ("close".equalsIgnoreCase(response.request().header("Connection")) || "close".equalsIgnoreCase(response.header("Connection"))) {
streamAllocation.noNewStreams();
}
if ((code == 204 || code == 205) && response.body().contentLength() > 0) {
throw new ProtocolException("HTTP " + code + " had non-zero Content-Length: " + response.body().contentLength());
}
return response;
}
Aggregations