use of okio.BufferedSource in project okhttp by square.
the class FileOperatorTest method snapshot.
private ByteString snapshot() throws IOException {
randomAccessFile.getChannel().force(false);
BufferedSource source = Okio.buffer(Okio.source(file));
return source.readByteString();
}
use of okio.BufferedSource in project okhttp by square.
the class RelayTest method readFromBuffer.
@Test
public void readFromBuffer() throws Exception {
Buffer upstream = new Buffer();
upstream.writeUtf8("abcdefghij");
Relay relay = Relay.edit(file, upstream, metadata, 5);
BufferedSource source1 = Okio.buffer(relay.newSource());
BufferedSource source2 = Okio.buffer(relay.newSource());
assertEquals("abcde", source1.readUtf8(5));
assertEquals("abcde", source2.readUtf8(5));
assertEquals("fghij", source2.readUtf8(5));
assertEquals("fghij", source1.readUtf8(5));
assertTrue(source1.exhausted());
assertTrue(source2.exhausted());
source1.close();
source2.close();
assertTrue(relay.isClosed());
assertFile(Relay.PREFIX_CLEAN, 10L, metadata.size(), "abcdefghij", metadata);
}
use of okio.BufferedSource in project okhttp by square.
the class RelayTest method readFromFile.
@Test
public void readFromFile() throws Exception {
Buffer upstream = new Buffer();
upstream.writeUtf8("abcdefghijklmnopqrst");
Relay relay = Relay.edit(file, upstream, metadata, 5);
BufferedSource source1 = Okio.buffer(relay.newSource());
BufferedSource source2 = Okio.buffer(relay.newSource());
assertEquals("abcdefghij", source1.readUtf8(10));
assertEquals("abcdefghij", source2.readUtf8(10));
assertEquals("klmnopqrst", source2.readUtf8(10));
assertEquals("klmnopqrst", source1.readUtf8(10));
assertTrue(source1.exhausted());
assertTrue(source2.exhausted());
source1.close();
source2.close();
assertTrue(relay.isClosed());
assertFile(Relay.PREFIX_CLEAN, 20L, metadata.size(), "abcdefghijklmnopqrst", metadata);
}
use of okio.BufferedSource in project okhttp by square.
the class RelayTest method assertFile.
private void assertFile(ByteString prefix, long upstreamSize, int metadataSize, String upstream, ByteString metadata) throws IOException {
BufferedSource source = Okio.buffer(Okio.source(file));
assertEquals(prefix, source.readByteString(prefix.size()));
assertEquals(upstreamSize, source.readLong());
assertEquals(metadataSize, source.readLong());
if (upstream != null) {
assertEquals(upstream, source.readUtf8(upstreamSize));
}
if (metadata != null) {
assertEquals(metadata, source.readByteString(metadataSize));
}
source.close();
}
use of okio.BufferedSource in project picasso by square.
the class BitmapHunter method decodeStream.
/**
* Decode a byte stream into a Bitmap. This method will take into account additional information
* about the supplied request in order to do the decoding efficiently (such as through leveraging
* {@code inSampleSize}).
*/
static Bitmap decodeStream(Source source, Request request) throws IOException {
BufferedSource bufferedSource = Okio.buffer(source);
boolean isWebPFile = Utils.isWebPFile(bufferedSource);
boolean isPurgeable = request.purgeable && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP;
BitmapFactory.Options options = RequestHandler.createBitmapOptions(request);
boolean calculateSize = RequestHandler.requiresInSampleSize(options);
// purgeable, which only affects bitmaps decoded from byte arrays.
if (isWebPFile || isPurgeable) {
byte[] bytes = bufferedSource.readByteArray();
if (calculateSize) {
BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options);
RequestHandler.calculateInSampleSize(request.targetWidth, request.targetHeight, options, request);
}
return BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options);
} else {
InputStream stream = bufferedSource.inputStream();
if (calculateSize) {
// TODO use an InputStream that buffers with Okio...
MarkableInputStream markStream = new MarkableInputStream(stream);
stream = markStream;
markStream.allowMarksToExpire(false);
long mark = markStream.savePosition(1024);
BitmapFactory.decodeStream(stream, null, options);
RequestHandler.calculateInSampleSize(request.targetWidth, request.targetHeight, options, request);
markStream.reset(mark);
markStream.allowMarksToExpire(true);
}
Bitmap bitmap = BitmapFactory.decodeStream(stream, null, options);
if (bitmap == null) {
// Treat null as an IO exception, we will eventually retry.
throw new IOException("Failed to decode stream.");
}
return bitmap;
}
}
Aggregations