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