Search in sources :

Example 6 with BufferedSink

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

Example 7 with BufferedSink

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

Example 8 with BufferedSink

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

Example 9 with BufferedSink

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

Example 10 with BufferedSink

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;
}
Also used : StreamAllocation(okhttp3.internal.connection.StreamAllocation) Response(okhttp3.Response) ProtocolException(java.net.ProtocolException) BufferedSink(okio.BufferedSink) Sink(okio.Sink) Request(okhttp3.Request) BufferedSink(okio.BufferedSink)

Aggregations

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