use of okio.BufferedSink in project buck by facebook.
the class HttpArtifactCache method storeImpl.
@Override
protected void storeImpl(ArtifactInfo info, final Path file, final Finished.Builder eventBuilder) throws IOException {
// Build the request, hitting the multi-key endpoint.
Request.Builder builder = new Request.Builder();
final HttpArtifactCacheBinaryProtocol.StoreRequest storeRequest = new HttpArtifactCacheBinaryProtocol.StoreRequest(info, new ByteSource() {
@Override
public InputStream openStream() throws IOException {
return projectFilesystem.newFileInputStream(file);
}
});
eventBuilder.getStoreBuilder().setRequestSizeBytes(storeRequest.getContentLength());
// Wrap the file into a `RequestBody` which uses `ProjectFilesystem`.
builder.put(new RequestBody() {
@Override
public MediaType contentType() {
return OCTET_STREAM_CONTENT_TYPE;
}
@Override
public long contentLength() throws IOException {
return storeRequest.getContentLength();
}
@Override
public void writeTo(BufferedSink bufferedSink) throws IOException {
StoreWriteResult writeResult = storeRequest.write(bufferedSink.outputStream());
eventBuilder.getStoreBuilder().setArtifactContentHash(writeResult.getArtifactContentHashCode().toString());
}
});
// Dispatch the store operation and verify it succeeded.
try (HttpResponse response = storeClient.makeRequest("/artifacts/key", builder)) {
final boolean requestFailed = response.statusCode() != HttpURLConnection.HTTP_ACCEPTED;
if (requestFailed) {
reportFailure("store(%s, %s): unexpected response: [%d:%s].", response.requestUrl(), info.getRuleKeys(), response.statusCode(), response.statusMessage());
}
eventBuilder.getStoreBuilder().setWasStoreSuccessful(!requestFailed);
}
}
use of okio.BufferedSink in project MVCHelper by LuckyJayce.
the class CountingRequestBody method writeTo.
@Override
public void writeTo(BufferedSink sink) throws IOException {
countingSink = new CountingSink(sink);
BufferedSink bufferedSink = Okio.buffer(countingSink);
delegate.writeTo(bufferedSink);
bufferedSink.flush();
}
use of okio.BufferedSink in project Store by NYTimes.
the class FSFile method write.
public void write(BufferedSource source) throws IOException {
File tmpFile = File.createTempFile("new", "tmp", file.getParentFile());
BufferedSink sink = null;
try {
sink = Okio.buffer(Okio.sink(tmpFile));
sink.writeAll(source);
if (!tmpFile.renameTo(file)) {
throw new IOException("unable to move tmp file to " + file.getPath());
}
} catch (Exception e) {
throw new IOException("unable to write to file", e);
} finally {
tmpFile.delete();
if (sink != null) {
sink.close();
}
}
}
use of okio.BufferedSink in project cw-omnibus by commonsguy.
the class Downloader method onHandleIntent.
@Override
public void onHandleIntent(Intent i) {
mgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
try {
String filename = i.getData().getLastPathSegment();
NotificationCompat.Builder builder = buildForeground(filename);
final Notification notif = builder.build();
startForeground(FOREGROUND_ID, notif);
File root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
root.mkdirs();
File output = new File(root, filename);
if (output.exists()) {
output.delete();
}
final ProgressResponseBody.Listener progressListener = new ProgressResponseBody.Listener() {
long lastUpdateTime = 0L;
@Override
public void onProgressChange(long bytesRead, long contentLength, boolean done) {
long now = SystemClock.uptimeMillis();
if (now - lastUpdateTime > 1000) {
notif.contentView.setProgressBar(android.R.id.progress, (int) contentLength, (int) bytesRead, false);
mgr.notify(FOREGROUND_ID, notif);
lastUpdateTime = now;
}
}
};
Interceptor nightTrain = new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Response original = chain.proceed(chain.request());
Response.Builder b = original.newBuilder().body(new ProgressResponseBody(original.body(), progressListener));
return (b.build());
}
};
OkHttpClient client = new OkHttpClient.Builder().addNetworkInterceptor(nightTrain).build();
Request request = new Request.Builder().url(i.getData().toString()).build();
Response response = client.newCall(request).execute();
String contentType = response.header("Content-type");
BufferedSink sink = Okio.buffer(Okio.sink(new File(output.getPath())));
sink.writeAll(response.body().source());
sink.close();
stopForeground(true);
raiseNotification(contentType, output, null);
} catch (IOException e2) {
stopForeground(true);
raiseNotification(null, null, e2);
}
}
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;
}
Aggregations