Search in sources :

Example 96 with BufferedSink

use of okio.BufferedSink in project spring-cloud-square by spring-projects-experimental.

the class WebClientCallAdapterFactory method processRequestBody.

private void processRequestBody(WebClient.RequestBodySpec spec, RequestBody requestBody) {
    Publisher<byte[]> requestBodyPublisher = Flux.create(sink -> {
        try {
            Sink fluxSink = new Sink() {

                @Override
                public void write(Buffer source, long byteCount) throws IOException {
                    sink.next(source.readByteArray(byteCount));
                }

                @Override
                public void flush() {
                    sink.complete();
                }

                @Override
                public Timeout timeout() {
                    return Timeout.NONE;
                }

                @Override
                public void close() {
                    sink.complete();
                }
            };
            BufferedSink bufferedSink = Okio.buffer(fluxSink);
            requestBody.writeTo(bufferedSink);
            bufferedSink.flush();
        } catch (IOException e) {
            sink.error(e);
        }
    });
    spec.body(requestBodyPublisher, byte[].class);
    MediaType requestContentType = requestBody.contentType();
    if (requestContentType != null) {
        spec.contentType(org.springframework.http.MediaType.parseMediaType(requestContentType.toString()));
    }
}
Also used : Buffer(okio.Buffer) Sink(okio.Sink) BufferedSink(okio.BufferedSink) MediaType(okhttp3.MediaType) BufferedSink(okio.BufferedSink) IOException(java.io.IOException)

Example 97 with BufferedSink

use of okio.BufferedSink in project spring-cloud-square by spring-projects-experimental.

the class SpringRequestConverter method convert.

@Override
public RequestBody convert(T value) throws IOException {
    if (value != null) {
        if (log.isDebugEnabled()) {
            if (this.contentType != null) {
                log.debug("Writing [" + value + "] as \"" + this.contentType + "\" using [" + messageConverter + "]");
            } else {
                log.debug("Writing [" + value + "] using [" + messageConverter + "]");
            }
        }
        return new RequestBody() {

            @Override
            public okhttp3.MediaType contentType() {
                return okhttp3.MediaType.parse(SpringRequestConverter.this.contentType.toString());
            }

            @Override
            public void writeTo(BufferedSink bufferedSink) throws IOException {
                @SuppressWarnings("unchecked") HttpMessageConverter<Object> copy = (HttpMessageConverter<Object>) messageConverter;
                copy.write(value, SpringRequestConverter.this.contentType, new HttpOutputMessage() {

                    @Override
                    public OutputStream getBody() throws IOException {
                        return bufferedSink.outputStream();
                    }

                    @Override
                    public HttpHeaders getHeaders() {
                        // TODO: where to get headers?
                        return new HttpHeaders();
                    }
                });
            }
        };
    /*
			 * FeignOutputMessage outputMessage = new FeignOutputMessage(request); try {
			 *
			 * @SuppressWarnings("unchecked") HttpMessageConverter<Object> copy =
			 * (HttpMessageConverter<Object>) messageConverter; copy.write(requestBody,
			 * this.contentType, outputMessage); } catch (IOException ex) { throw new
			 * EncodeException("Error converting request body", ex); } // clear headers
			 * request.headers(null); // converters can modify headers, so update the
			 * request // with the modified headers
			 * request.headers(getHeaders(outputMessage.getHeaders()));
			 * request.body(outputMessage.getOutputStream().toByteArray(),
			 * Charset.forName("UTF-8")); // TODO: set charset return;
			 */
    }
    return null;
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpOutputMessage(org.springframework.http.HttpOutputMessage) OutputStream(java.io.OutputStream) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) BufferedSink(okio.BufferedSink) IOException(java.io.IOException) RequestBody(okhttp3.RequestBody)

Example 98 with BufferedSink

use of okio.BufferedSink in project bttv by bttv-android.

the class UpdaterActivity method startDownload.

private void startDownload(final String url, final ProgressBar bar) {
    final String filename = "bttv.apk";
    final ProgressListener progListener = getProgressListener(bar);
    final OkHttpClient client = getClient(progListener);
    final Request request = new Request.Builder().url(url).build();
    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                Response response = client.newCall(request).execute();
                if (!response.isSuccessful())
                    throw new IOException("Unexpected code " + response);
                Log.d("LBTTVUpdaterActivity", "sucessful");
                File downloadedFile = new File(UpdaterActivity.this.getCacheDir(), filename);
                FileOutputStream os = new FileOutputStream(downloadedFile);
                BufferedSink sink = Okio.buffer(Okio.sink(os));
                sink.writeAll(response.body().source());
                sink.close();
                Intent intent = new Intent("android.intent.action.INSTALL_PACKAGE");
                Uri uri = FileProvider.getUriForFile(UpdaterActivity.this, "tv.twitch.bttvandroid.app.provider", downloadedFile);
                intent.setDataAndType(uri, "application/vnd.android.package-archive");
                intent.setFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_GRANT_READ_URI_PERMISSION);
                startActivity(intent);
            } catch (Exception e) {
                Log.e("LBTTVUpdaterActivity", "Update failed", e);
                UpdaterActivity.this.runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        Util.showError(UpdaterActivity.this, e);
                    }
                });
            }
        }
    }).start();
}
Also used : OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) BufferedSink(okio.BufferedSink) Intent(android.content.Intent) IOException(java.io.IOException) Uri(android.net.Uri) IOException(java.io.IOException) Response(okhttp3.Response) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 99 with BufferedSink

use of okio.BufferedSink in project marketing-api-java-sdks by Hyq0719.

the class OkhttpHttpHandler method downloadFileFromResponse.

public File downloadFileFromResponse(Response response) throws ApiException {
    try {
        File file = prepareDownloadFile(response);
        BufferedSink sink = Okio.buffer(Okio.sink(file));
        sink.writeAll(response.body().source());
        sink.close();
        return file;
    } catch (IOException e) {
        throw new ApiException(e);
    }
}
Also used : BufferedSink(okio.BufferedSink) IOException(java.io.IOException) File(java.io.File) ApiException(com.hyq0719.mktapi.common.exception.ApiException)

Example 100 with BufferedSink

use of okio.BufferedSink in project edx-app-android by openedx.

the class GzipRequestInterceptor method gzip.

private RequestBody gzip(final RequestBody body) {
    return new RequestBody() {

        @Override
        public MediaType contentType() {
            return body.contentType();
        }

        @Override
        public long contentLength() {
            // We don't know the compressed length in advance!
            return -1;
        }

        @Override
        public void writeTo(BufferedSink sink) throws IOException {
            BufferedSink gzipSink = Okio.buffer(new GzipSink(sink));
            body.writeTo(gzipSink);
            gzipSink.close();
        }
    };
}
Also used : GzipSink(okio.GzipSink) BufferedSink(okio.BufferedSink) RequestBody(okhttp3.RequestBody)

Aggregations

BufferedSink (okio.BufferedSink)260 IOException (java.io.IOException)94 File (java.io.File)47 RequestBody (okhttp3.RequestBody)45 Buffer (okio.Buffer)45 GzipSink (okio.GzipSink)41 BufferedSource (okio.BufferedSource)35 Test (org.junit.Test)35 Request (okhttp3.Request)33 Response (okhttp3.Response)33 Source (okio.Source)31 Sink (okio.Sink)19 Test (org.junit.jupiter.api.Test)19 MediaType (okhttp3.MediaType)17 MockResponse (mockwebserver3.MockResponse)14 OkHttpClient (okhttp3.OkHttpClient)13 Call (okhttp3.Call)12 InputStream (java.io.InputStream)11 InterruptedIOException (java.io.InterruptedIOException)9 MockDuplexResponseBody (mockwebserver3.internal.duplex.MockDuplexResponseBody)9