use of okio.Source in project okhttp by square.
the class Http2ConnectionTest method readTimesOut.
@Test
public void readTimesOut() throws Exception {
// write the mocking script
peer.sendFrame().settings(new Settings());
// ACK
peer.acceptFrame();
// SYN_STREAM
peer.acceptFrame();
peer.sendFrame().synReply(false, 3, headerEntries("a", "android"));
// RST_STREAM
peer.acceptFrame();
peer.play();
// play it back
Http2Connection connection = connect(peer);
Http2Stream stream = connection.newStream(headerEntries("b", "banana"), false);
stream.readTimeout().timeout(500, TimeUnit.MILLISECONDS);
Source source = stream.getSource();
long startNanos = System.nanoTime();
try {
source.read(new Buffer(), 1);
fail();
} catch (InterruptedIOException expected) {
}
long elapsedNanos = System.nanoTime() - startNanos;
awaitWatchdogIdle();
assertEquals(500d, TimeUnit.NANOSECONDS.toMillis(elapsedNanos), 200d);
assertEquals(0, connection.openStreamCount());
// verify the peer received what was expected
assertEquals(Http2.TYPE_HEADERS, peer.takeFrame().type);
assertEquals(Http2.TYPE_RST_STREAM, peer.takeFrame().type);
}
use of okio.Source in project okhttp by square.
the class RelayTest method singleSource.
@Test
public void singleSource() throws Exception {
Buffer upstream = new Buffer();
upstream.writeUtf8("abcdefghijklm");
Relay relay = Relay.edit(file, upstream, metadata, 1024);
Source source = relay.newSource();
Buffer sourceBuffer = new Buffer();
assertEquals(5, source.read(sourceBuffer, 5));
assertEquals("abcde", sourceBuffer.readUtf8());
assertEquals(8, source.read(sourceBuffer, 1024));
assertEquals("fghijklm", sourceBuffer.readUtf8());
assertEquals(-1, source.read(sourceBuffer, 1024));
assertEquals(0, sourceBuffer.size());
source.close();
assertTrue(relay.isClosed());
assertFile(Relay.PREFIX_CLEAN, 13L, metadata.size(), "abcdefghijklm", metadata);
}
use of okio.Source in project okhttp by square.
the class Http2ConnectionTest method testTruncatedDataFrame.
@Test
public void testTruncatedDataFrame() throws Exception {
// write the mocking script
peer.sendFrame().settings(new Settings());
// ACK
peer.acceptFrame();
// SYN_STREAM
peer.acceptFrame();
peer.sendFrame().synReply(false, 3, headerEntries("a", "android"));
peer.sendFrame().data(false, 3, data(1024), 1024);
peer.truncateLastFrame(8 + 100);
peer.play();
// play it back
Http2Connection connection = connect(peer);
Http2Stream stream = connection.newStream(headerEntries("b", "banana"), false);
assertEquals(headerEntries("a", "android"), stream.takeResponseHeaders());
Source in = stream.getSource();
try {
Okio.buffer(in).readByteString(101);
fail();
} catch (IOException expected) {
assertEquals("stream was reset: PROTOCOL_ERROR", expected.getMessage());
}
}
use of okio.Source in project okhttp by square.
the class Http2ConnectionTest method clientClosesClientInputStream.
/**
* Test that the client sends a RST_STREAM if doing so won't disrupt the output stream.
*/
@Test
public void clientClosesClientInputStream() throws Exception {
// write the mocking script
peer.sendFrame().settings(new Settings());
// ACK
peer.acceptFrame();
// SYN_STREAM
peer.acceptFrame();
// RST_STREAM
peer.acceptFrame();
peer.play();
// play it back
Http2Connection connection = connect(peer);
Http2Stream stream = connection.newStream(headerEntries("a", "android"), false);
Source in = stream.getSource();
BufferedSink out = Okio.buffer(stream.getSink());
in.close();
try {
in.read(new Buffer(), 1);
fail();
} catch (IOException expected) {
assertEquals("stream closed", expected.getMessage());
}
try {
out.writeUtf8("a");
out.flush();
fail();
} catch (IOException expected) {
assertEquals("stream finished", expected.getMessage());
}
assertEquals(0, connection.openStreamCount());
// verify the peer received what was expected
InFrame synStream = peer.takeFrame();
assertEquals(Http2.TYPE_HEADERS, synStream.type);
assertTrue(synStream.inFinished);
assertFalse(synStream.outFinished);
InFrame rstStream = peer.takeFrame();
assertEquals(Http2.TYPE_RST_STREAM, rstStream.type);
assertEquals(ErrorCode.CANCEL, rstStream.errorCode);
}
use of okio.Source in project picasso by square.
the class MediaStoreRequestHandler method load.
@Override
public Result load(Request request, int networkPolicy) throws IOException {
ContentResolver contentResolver = context.getContentResolver();
int exifOrientation = getExifOrientation(contentResolver, request.uri);
String mimeType = contentResolver.getType(request.uri);
boolean isVideo = mimeType != null && mimeType.startsWith("video/");
if (request.hasSize()) {
PicassoKind picassoKind = getPicassoKind(request.targetWidth, request.targetHeight);
if (!isVideo && picassoKind == FULL) {
Source source = Okio.source(getInputStream(request));
return new Result(null, source, DISK, exifOrientation);
}
long id = parseId(request.uri);
BitmapFactory.Options options = createBitmapOptions(request);
options.inJustDecodeBounds = true;
calculateInSampleSize(request.targetWidth, request.targetHeight, picassoKind.width, picassoKind.height, options, request);
Bitmap bitmap;
if (isVideo) {
// Since MediaStore doesn't provide the full screen kind thumbnail, we use the mini kind
// instead which is the largest thumbnail size can be fetched from MediaStore.
int kind = (picassoKind == FULL) ? Video.Thumbnails.MINI_KIND : picassoKind.androidKind;
bitmap = Video.Thumbnails.getThumbnail(contentResolver, id, kind, options);
} else {
bitmap = Images.Thumbnails.getThumbnail(contentResolver, id, picassoKind.androidKind, options);
}
if (bitmap != null) {
return new Result(bitmap, null, DISK, exifOrientation);
}
}
Source source = Okio.source(getInputStream(request));
return new Result(null, source, DISK, exifOrientation);
}
Aggregations