Search in sources :

Example 1 with Source

use of okio.Source in project PocketHub by pockethub.

the class ImageBinPoster method post.

/**
     * Post the image to ImageBin
     *
     * @param context A context
     * @param uri The content URI
     * @param callback Request callback
     * @return If the file was successfully retrieved
     */
public static boolean post(Context context, Uri uri, Callback callback) {
    byte[] bytes = null;
    try {
        InputStream stream = context.getContentResolver().openInputStream(uri);
        if (stream != null) {
            Source source = Okio.source(stream);
            bytes = Okio.buffer(source).readByteArray();
        }
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
    post(bytes, callback);
    return true;
}
Also used : InputStream(java.io.InputStream) IOException(java.io.IOException) Source(okio.Source)

Example 2 with Source

use of okio.Source in project okhttp by square.

the class InMemoryFileSystem method source.

@Override
public Source source(File file) throws FileNotFoundException {
    Buffer result = files.get(file);
    if (result == null)
        throw new FileNotFoundException();
    final Source source = result.clone();
    openSources.put(source, file);
    return new ForwardingSource(source) {

        @Override
        public void close() throws IOException {
            openSources.remove(source);
            super.close();
        }
    };
}
Also used : Buffer(okio.Buffer) FileNotFoundException(java.io.FileNotFoundException) ForwardingSource(okio.ForwardingSource) Source(okio.Source) ForwardingSource(okio.ForwardingSource)

Example 3 with Source

use of okio.Source in project okhttp by square.

the class RequestBody method create.

/** Returns a new request body that transmits the content of {@code file}. */
public static RequestBody create(final MediaType contentType, final File file) {
    if (file == null)
        throw new NullPointerException("content == null");
    return new RequestBody() {

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

        @Override
        public long contentLength() {
            return file.length();
        }

        @Override
        public void writeTo(BufferedSink sink) throws IOException {
            Source source = null;
            try {
                source = Okio.source(file);
                sink.writeAll(source);
            } finally {
                Util.closeQuietly(source);
            }
        }
    };
}
Also used : BufferedSink(okio.BufferedSink) Source(okio.Source)

Example 4 with Source

use of okio.Source in project okhttp by square.

the class CacheInterceptor method cacheWritingResponse.

/**
   * Returns a new source that writes bytes to {@code cacheRequest} as they are read by the source
   * consumer. This is careful to discard bytes left over when the stream is closed; otherwise we
   * may never exhaust the source stream and therefore not complete the cached response.
   */
private Response cacheWritingResponse(final CacheRequest cacheRequest, Response response) throws IOException {
    // Some apps return a null body; for compatibility we treat that like a null cache request.
    if (cacheRequest == null)
        return response;
    Sink cacheBodyUnbuffered = cacheRequest.body();
    if (cacheBodyUnbuffered == null)
        return response;
    final BufferedSource source = response.body().source();
    final BufferedSink cacheBody = Okio.buffer(cacheBodyUnbuffered);
    Source cacheWritingSource = new Source() {

        boolean cacheRequestClosed;

        @Override
        public long read(Buffer sink, long byteCount) throws IOException {
            long bytesRead;
            try {
                bytesRead = source.read(sink, byteCount);
            } catch (IOException e) {
                if (!cacheRequestClosed) {
                    cacheRequestClosed = true;
                    // Failed to write a complete cache response.
                    cacheRequest.abort();
                }
                throw e;
            }
            if (bytesRead == -1) {
                if (!cacheRequestClosed) {
                    cacheRequestClosed = true;
                    // The cache response is complete!
                    cacheBody.close();
                }
                return -1;
            }
            sink.copyTo(cacheBody.buffer(), sink.size() - bytesRead, bytesRead);
            cacheBody.emitCompleteSegments();
            return bytesRead;
        }

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

        @Override
        public void close() throws IOException {
            if (!cacheRequestClosed && !discard(this, HttpCodec.DISCARD_STREAM_TIMEOUT_MILLIS, MILLISECONDS)) {
                cacheRequestClosed = true;
                cacheRequest.abort();
            }
            source.close();
        }
    };
    return response.newBuilder().body(new RealResponseBody(response.headers(), Okio.buffer(cacheWritingSource))).build();
}
Also used : Buffer(okio.Buffer) Sink(okio.Sink) BufferedSink(okio.BufferedSink) RealResponseBody(okhttp3.internal.http.RealResponseBody) BufferedSink(okio.BufferedSink) IOException(java.io.IOException) Source(okio.Source) BufferedSource(okio.BufferedSource) BufferedSource(okio.BufferedSource)

Example 5 with Source

use of okio.Source in project okhttp by square.

the class Http2ConnectionTest method readSendsWindowUpdate.

@Test
public void readSendsWindowUpdate() throws Exception {
    int windowSize = 100;
    int windowUpdateThreshold = 50;
    // Write the mocking script.
    peer.sendFrame().settings(new Settings());
    // ACK
    peer.acceptFrame();
    // SYN_STREAM
    peer.acceptFrame();
    peer.sendFrame().synReply(false, 3, headerEntries("a", "android"));
    for (int i = 0; i < 3; i++) {
        // Send frames of summing to size 50, which is windowUpdateThreshold.
        peer.sendFrame().data(false, 3, data(24), 24);
        peer.sendFrame().data(false, 3, data(25), 25);
        peer.sendFrame().data(false, 3, data(1), 1);
        // connection WINDOW UPDATE
        peer.acceptFrame();
        // stream WINDOW UPDATE
        peer.acceptFrame();
    }
    peer.sendFrame().data(true, 3, data(0), 0);
    peer.play();
    // Play it back.
    Http2Connection connection = connect(peer);
    connection.okHttpSettings.set(INITIAL_WINDOW_SIZE, windowSize);
    Http2Stream stream = connection.newStream(headerEntries("b", "banana"), false);
    assertEquals(0, stream.unacknowledgedBytesRead);
    assertEquals(headerEntries("a", "android"), stream.takeResponseHeaders());
    Source in = stream.getSource();
    Buffer buffer = new Buffer();
    buffer.writeAll(in);
    assertEquals(-1, in.read(buffer, 1));
    assertEquals(150, buffer.size());
    InFrame synStream = peer.takeFrame();
    assertEquals(Http2.TYPE_HEADERS, synStream.type);
    for (int i = 0; i < 3; i++) {
        List<Integer> windowUpdateStreamIds = new ArrayList<>(2);
        for (int j = 0; j < 2; j++) {
            InFrame windowUpdate = peer.takeFrame();
            assertEquals(Http2.TYPE_WINDOW_UPDATE, windowUpdate.type);
            windowUpdateStreamIds.add(windowUpdate.streamId);
            assertEquals(windowUpdateThreshold, windowUpdate.windowSizeIncrement);
        }
        // connection
        assertTrue(windowUpdateStreamIds.contains(0));
        // stream
        assertTrue(windowUpdateStreamIds.contains(3));
    }
}
Also used : Buffer(okio.Buffer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InFrame(okhttp3.internal.http2.MockHttp2Peer.InFrame) ArrayList(java.util.ArrayList) Source(okio.Source) BufferedSource(okio.BufferedSource) Test(org.junit.Test)

Aggregations

Source (okio.Source)29 BufferedSource (okio.BufferedSource)17 IOException (java.io.IOException)15 Buffer (okio.Buffer)15 BufferedSink (okio.BufferedSink)11 Test (org.junit.Test)10 InFrame (okhttp3.internal.http2.MockHttp2Peer.InFrame)5 InterruptedIOException (java.io.InterruptedIOException)4 FileNotFoundException (java.io.FileNotFoundException)3 InputStream (java.io.InputStream)3 Bitmap (android.graphics.Bitmap)2 Socket (java.net.Socket)2 Response (okhttp3.Response)2 ContentResolver (android.content.ContentResolver)1 Context (android.content.Context)1 BitmapFactory (android.graphics.BitmapFactory)1 Uri (android.net.Uri)1 Task (bolts.Task)1 FileUploadingHelper (chat.rocket.android.api.FileUploadingHelper)1 LogIfError (chat.rocket.android.helper.LogIfError)1