Search in sources :

Example 6 with Source

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);
}
Also used : Buffer(okio.Buffer) InterruptedIOException(java.io.InterruptedIOException) Source(okio.Source) BufferedSource(okio.BufferedSource) Test(org.junit.Test)

Example 7 with Source

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);
}
Also used : Buffer(okio.Buffer) Source(okio.Source) BufferedSource(okio.BufferedSource) Test(org.junit.Test)

Example 8 with Source

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());
    }
}
Also used : InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) Source(okio.Source) BufferedSource(okio.BufferedSource) Test(org.junit.Test)

Example 9 with Source

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);
}
Also used : Buffer(okio.Buffer) InFrame(okhttp3.internal.http2.MockHttp2Peer.InFrame) BufferedSink(okio.BufferedSink) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) Source(okio.Source) BufferedSource(okio.BufferedSource) Test(org.junit.Test)

Example 10 with Source

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);
}
Also used : Bitmap(android.graphics.Bitmap) BitmapFactory(android.graphics.BitmapFactory) Source(okio.Source) ContentResolver(android.content.ContentResolver)

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