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