Search in sources :

Example 16 with Source

use of okio.Source in project picasso by square.

the class BitmapHunter method hunt.

Bitmap hunt() throws IOException {
    Bitmap bitmap = null;
    if (shouldReadFromMemoryCache(memoryPolicy)) {
        bitmap = cache.get(key);
        if (bitmap != null) {
            stats.dispatchCacheHit();
            loadedFrom = MEMORY;
            if (picasso.loggingEnabled) {
                log(OWNER_HUNTER, VERB_DECODED, data.logId(), "from cache");
            }
            return bitmap;
        }
    }
    networkPolicy = retryCount == 0 ? NetworkPolicy.OFFLINE.index : networkPolicy;
    RequestHandler.Result result = requestHandler.load(data, networkPolicy);
    if (result != null) {
        loadedFrom = result.getLoadedFrom();
        exifOrientation = result.getExifOrientation();
        bitmap = result.getBitmap();
        // If there was no Bitmap then we need to decode it from the stream.
        if (bitmap == null) {
            Source source = result.getSource();
            try {
                bitmap = decodeStream(source, data);
            } finally {
                try {
                    //noinspection ConstantConditions If bitmap is null then source is guranteed non-null.
                    source.close();
                } catch (IOException ignored) {
                }
            }
        }
    }
    if (bitmap != null) {
        if (picasso.loggingEnabled) {
            log(OWNER_HUNTER, VERB_DECODED, data.logId());
        }
        stats.dispatchBitmapDecoded(bitmap);
        if (data.needsTransformation() || exifOrientation != 0) {
            synchronized (DECODE_LOCK) {
                if (data.needsMatrixTransform() || exifOrientation != 0) {
                    bitmap = transformResult(data, bitmap, exifOrientation);
                    if (picasso.loggingEnabled) {
                        log(OWNER_HUNTER, VERB_TRANSFORMED, data.logId());
                    }
                }
                if (data.hasCustomTransformations()) {
                    bitmap = applyCustomTransformations(data.transformations, bitmap);
                    if (picasso.loggingEnabled) {
                        log(OWNER_HUNTER, VERB_TRANSFORMED, data.logId(), "from custom transformations");
                    }
                }
            }
            if (bitmap != null) {
                stats.dispatchBitmapTransformed(bitmap);
            }
        }
    }
    return bitmap;
}
Also used : Bitmap(android.graphics.Bitmap) IOException(java.io.IOException) Source(okio.Source) BufferedSource(okio.BufferedSource)

Example 17 with Source

use of okio.Source in project wire by square.

the class TestAllTypes method testReadFromSlowSource.

@Test
public void testReadFromSlowSource() throws IOException {
    byte[] data = adapter.encode(allTypes);
    Source input = new SlowSource(new Buffer().write(data));
    AllTypes parsed = adapter.decode(Okio.buffer(input));
    assertThat(parsed).isEqualTo(allTypes);
    assertThat(allTypes.ext_opt_bool).isEqualTo(Boolean.TRUE);
    assertThat(allTypes.ext_rep_bool).isEqualTo(list(true));
    assertThat(allTypes.ext_pack_bool).isEqualTo(list(true));
}
Also used : Buffer(okio.Buffer) AllTypes(com.squareup.wire.protos.alltypes.AllTypes) Source(okio.Source) ForwardingSource(okio.ForwardingSource) Test(org.junit.Test)

Example 18 with Source

use of okio.Source in project okhttp by square.

the class Http2Server method serveFile.

private void serveFile(Http2Stream stream, File file) throws IOException {
    List<Header> responseHeaders = Arrays.asList(new Header(":status", "200"), new Header(":version", "HTTP/1.1"), new Header("content-type", contentType(file)));
    stream.sendResponseHeaders(responseHeaders, true);
    Source source = Okio.source(file);
    try {
        BufferedSink out = Okio.buffer(stream.getSink());
        out.writeAll(source);
        out.close();
    } finally {
        Util.closeQuietly(source);
    }
}
Also used : BufferedSink(okio.BufferedSink) Source(okio.Source)

Example 19 with Source

use of okio.Source in project okhttp by square.

the class ResponseTest method responseBody.

/**
   * Returns a new response body that refuses to be read once it has been closed. This is true of
   * most {@link BufferedSource} instances, but not of {@link Buffer}.
   */
private ResponseBody responseBody(String content) {
    final Buffer data = new Buffer().writeUtf8(content);
    Source source = new Source() {

        boolean closed;

        @Override
        public void close() throws IOException {
            closed = true;
        }

        @Override
        public long read(Buffer sink, long byteCount) throws IOException {
            if (closed)
                throw new IllegalStateException();
            return data.read(sink, byteCount);
        }

        @Override
        public Timeout timeout() {
            return Timeout.NONE;
        }
    };
    return ResponseBody.create(null, -1, Okio.buffer(source));
}
Also used : Buffer(okio.Buffer) BufferedSource(okio.BufferedSource) Source(okio.Source)

Example 20 with Source

use of okio.Source in project okhttp by square.

the class RelayTest method redundantCallsToCloseAreIgnored.

@Test
public void redundantCallsToCloseAreIgnored() throws Exception {
    Buffer upstream = new Buffer();
    upstream.writeUtf8("abcde");
    Relay relay = Relay.edit(file, upstream, metadata, 1024);
    Source source1 = relay.newSource();
    Source source2 = relay.newSource();
    source1.close();
    // Unnecessary. Shouldn't decrement the reference count.
    source1.close();
    assertFalse(relay.isClosed());
    source2.close();
    assertTrue(relay.isClosed());
    assertFile(Relay.PREFIX_DIRTY, -1L, -1, null, null);
}
Also used : Buffer(okio.Buffer) 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