Search in sources :

Example 1 with BufferedSink

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);
    }
}
Also used : DataInputStream(java.io.DataInputStream) InputStream(java.io.InputStream) Request(okhttp3.Request) HttpResponse(com.facebook.buck.slb.HttpResponse) BufferedSink(okio.BufferedSink) IOException(java.io.IOException) ByteSource(com.google.common.io.ByteSource) MediaType(okhttp3.MediaType) RequestBody(okhttp3.RequestBody)

Example 2 with BufferedSink

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

Example 3 with BufferedSink

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();
        }
    }
}
Also used : BufferedSink(okio.BufferedSink) IOException(java.io.IOException) File(java.io.File) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 4 with BufferedSink

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);
    }
}
Also used : OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) BufferedSink(okio.BufferedSink) IOException(java.io.IOException) Notification(android.app.Notification) Response(okhttp3.Response) NotificationCompat(android.support.v4.app.NotificationCompat) File(java.io.File) Interceptor(okhttp3.Interceptor)

Example 5 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)

Aggregations

BufferedSink (okio.BufferedSink)164 IOException (java.io.IOException)58 Buffer (okio.Buffer)30 Test (org.junit.Test)29 File (java.io.File)28 BufferedSource (okio.BufferedSource)27 RequestBody (okhttp3.RequestBody)26 Response (okhttp3.Response)24 Source (okio.Source)22 Request (okhttp3.Request)20 GzipSink (okio.GzipSink)20 Test (org.junit.jupiter.api.Test)17 MockResponse (mockwebserver3.MockResponse)14 Call (okhttp3.Call)10 Sink (okio.Sink)10 MockDuplexResponseBody (mockwebserver3.internal.duplex.MockDuplexResponseBody)9 MediaType (okhttp3.MediaType)9 AsyncRequestBody (okhttp3.internal.duplex.AsyncRequestBody)9 InFrame (okhttp3.internal.http2.MockHttp2Peer.InFrame)9 InterruptedIOException (java.io.InterruptedIOException)8