Search in sources :

Example 11 with Sink

use of okio.Sink in project okhttp by square.

the class InMemoryFileSystem method sink.

private Sink sink(File file, boolean appending) {
    Buffer result = null;
    if (appending) {
        result = files.get(file);
    }
    if (result == null) {
        result = new Buffer();
    }
    files.put(file, result);
    final Sink sink = result;
    openSinks.put(sink, file);
    return new ForwardingSink(sink) {

        @Override
        public void close() throws IOException {
            openSinks.remove(sink);
            super.close();
        }
    };
}
Also used : Buffer(okio.Buffer) Sink(okio.Sink) ForwardingSink(okio.ForwardingSink) ForwardingSink(okio.ForwardingSink)

Example 12 with Sink

use of okio.Sink in project okhttp by square.

the class Http2ConnectionTest method writeTimesOutAwaitingStreamWindow.

@Test
public void writeTimesOutAwaitingStreamWindow() throws Exception {
    // Set the peer's receive window to 5 bytes!
    Settings peerSettings = new Settings().set(INITIAL_WINDOW_SIZE, 5);
    // write the mocking script
    peer.sendFrame().settings(peerSettings);
    // ACK SETTINGS
    peer.acceptFrame();
    // PING
    peer.acceptFrame();
    peer.sendFrame().ping(true, 1, 0);
    // SYN_STREAM
    peer.acceptFrame();
    peer.sendFrame().synReply(false, 3, headerEntries("a", "android"));
    // DATA
    peer.acceptFrame();
    // RST_STREAM
    peer.acceptFrame();
    peer.play();
    // play it back
    Http2Connection connection = connect(peer);
    // Make sure settings have been received.
    connection.ping().roundTripTime();
    Http2Stream stream = connection.newStream(headerEntries("b", "banana"), true);
    Sink sink = stream.getSink();
    sink.write(new Buffer().writeUtf8("abcde"), 5);
    stream.writeTimeout().timeout(500, TimeUnit.MILLISECONDS);
    long startNanos = System.nanoTime();
    sink.write(new Buffer().writeUtf8("f"), 1);
    try {
        // This will time out waiting on the write window.
        sink.flush();
        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_PING, peer.takeFrame().type);
    assertEquals(Http2.TYPE_HEADERS, peer.takeFrame().type);
    assertEquals(Http2.TYPE_DATA, peer.takeFrame().type);
    assertEquals(Http2.TYPE_RST_STREAM, peer.takeFrame().type);
}
Also used : Buffer(okio.Buffer) InterruptedIOException(java.io.InterruptedIOException) Sink(okio.Sink) BufferedSink(okio.BufferedSink) Test(org.junit.Test)

Example 13 with Sink

use of okio.Sink in project okhttp by square.

the class Http2ConnectionTest method writeTimesOutAwaitingConnectionWindow.

@Test
public void writeTimesOutAwaitingConnectionWindow() throws Exception {
    // Set the peer's receive window to 5 bytes. Give the stream 5 bytes back, so only the
    // connection-level window is applicable.
    Settings peerSettings = new Settings().set(INITIAL_WINDOW_SIZE, 5);
    // write the mocking script
    peer.sendFrame().settings(peerSettings);
    // ACK SETTINGS
    peer.acceptFrame();
    // PING
    peer.acceptFrame();
    peer.sendFrame().ping(true, 1, 0);
    // SYN_STREAM
    peer.acceptFrame();
    peer.sendFrame().synReply(false, 3, headerEntries("a", "android"));
    peer.sendFrame().windowUpdate(3, 5);
    // PING
    peer.acceptFrame();
    peer.sendFrame().ping(true, 3, 0);
    // DATA
    peer.acceptFrame();
    // RST_STREAM
    peer.acceptFrame();
    peer.play();
    // play it back
    Http2Connection connection = connect(peer);
    // Make sure settings have been acked.
    connection.ping().roundTripTime();
    Http2Stream stream = connection.newStream(headerEntries("b", "banana"), true);
    // Make sure the window update has been received.
    connection.ping().roundTripTime();
    Sink sink = stream.getSink();
    stream.writeTimeout().timeout(500, TimeUnit.MILLISECONDS);
    sink.write(new Buffer().writeUtf8("abcdef"), 6);
    long startNanos = System.nanoTime();
    try {
        // This will time out waiting on the write window.
        sink.flush();
        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_PING, peer.takeFrame().type);
    assertEquals(Http2.TYPE_HEADERS, peer.takeFrame().type);
    assertEquals(Http2.TYPE_PING, peer.takeFrame().type);
    assertEquals(Http2.TYPE_DATA, peer.takeFrame().type);
    assertEquals(Http2.TYPE_RST_STREAM, peer.takeFrame().type);
}
Also used : Buffer(okio.Buffer) InterruptedIOException(java.io.InterruptedIOException) Sink(okio.Sink) BufferedSink(okio.BufferedSink) Test(org.junit.Test)

Example 14 with Sink

use of okio.Sink in project okhttp by square.

the class Http2ConnectionTest method outgoingWritesAreBatched.

@Test
public void outgoingWritesAreBatched() 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"));
    // DATA
    peer.acceptFrame();
    peer.play();
    // play it back
    Http2Connection connection = connect(peer);
    Http2Stream stream = connection.newStream(headerEntries("b", "banana"), true);
    // two outgoing writes
    Sink sink = stream.getSink();
    sink.write(new Buffer().writeUtf8("abcde"), 5);
    sink.write(new Buffer().writeUtf8("fghij"), 5);
    sink.close();
    // verify the peer received one incoming frame
    assertEquals(Http2.TYPE_HEADERS, peer.takeFrame().type);
    InFrame data = peer.takeFrame();
    assertEquals(Http2.TYPE_DATA, data.type);
    assertTrue(Arrays.equals("abcdefghij".getBytes("UTF-8"), data.data));
    assertTrue(data.inFinished);
}
Also used : Buffer(okio.Buffer) InFrame(okhttp3.internal.http2.MockHttp2Peer.InFrame) Sink(okio.Sink) BufferedSink(okio.BufferedSink) Test(org.junit.Test)

Example 15 with Sink

use of okio.Sink in project okhttp by square.

the class DiskLruCache method newJournalWriter.

private BufferedSink newJournalWriter() throws FileNotFoundException {
    Sink fileSink = fileSystem.appendingSink(journalFile);
    Sink faultHidingSink = new FaultHidingSink(fileSink) {

        @Override
        protected void onException(IOException e) {
            assert (Thread.holdsLock(DiskLruCache.this));
            hasJournalErrors = true;
        }
    };
    return Okio.buffer(faultHidingSink);
}
Also used : Sink(okio.Sink) BufferedSink(okio.BufferedSink) IOException(java.io.IOException)

Aggregations

Sink (okio.Sink)15 BufferedSink (okio.BufferedSink)11 Buffer (okio.Buffer)9 Test (org.junit.Test)6 IOException (java.io.IOException)5 Request (okhttp3.Request)4 Response (okhttp3.Response)4 BufferedSource (okio.BufferedSource)4 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 InterruptedIOException (java.io.InterruptedIOException)2 OkHttpClient (okhttp3.OkHttpClient)2 ForwardingSink (okio.ForwardingSink)2 GzipSink (okio.GzipSink)2 Source (okio.Source)2 BufferedOutputStream (java.io.BufferedOutputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 RandomAccessFile (java.io.RandomAccessFile)1