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