/** Returns true if this connection is ready to host new streams. */ public boolean isHealthy(boolean doExtensiveChecks) { if (socket.isClosed() || socket.isInputShutdown() || socket.isOutputShutdown()) { return false; } if (http2Connection != null) { return !http2Connection.isShutdown(); } if (doExtensiveChecks) { try { int readTimeout = socket.getSoTimeout(); try { socket.setSoTimeout(1); if (source.exhausted()) { return false; // Stream is exhausted; socket is closed. } return true; } finally { socket.setSoTimeout(readTimeout); } } catch (SocketTimeoutException ignored) { // Read timed out; socket is good. } catch (IOException e) { return false; // Couldn't read; socket is closed. } } return true; }
if (!source.exhausted()) { rebuildJournal(); } else {
@Test public void readExhaustedSource() throws Exception { Buffer sink = new Buffer(); sink.writeUtf8(repeat('a', 10)); assertEquals(-1, source.read(sink, 10)); assertEquals(10, sink.size()); assertTrue(source.exhausted()); }
@Test public void readZeroBytesFromSource() throws Exception { Buffer sink = new Buffer(); sink.writeUtf8(repeat('a', 10)); // Either 0 or -1 is reasonable here. For consistency with Android's // ByteArrayInputStream we return 0. assertEquals(-1, source.read(sink, 0)); assertEquals(10, sink.size()); assertTrue(source.exhausted()); }
@Test public void readAllExhausted() throws IOException { MockSink mockSink = new MockSink(); assertEquals(0, source.readAll(mockSink)); assertTrue(source.exhausted()); mockSink.assertLog(); }
@Test public void sinkCloseDoesntWaitForSourceRead() throws Exception { Pipe pipe = new Pipe(100L); pipe.sink().write(new Buffer().writeUtf8("abc"), 3); pipe.sink().close(); BufferedSource bufferedSource = Okio.buffer(pipe.source()); assertEquals("abc", bufferedSource.readUtf8()); assertTrue(bufferedSource.exhausted()); }
@Test public void readAll() throws IOException { source.getBuffer().writeUtf8("abc"); sink.writeUtf8("def"); sink.emit(); Buffer sink = new Buffer(); assertEquals(6, source.readAll(sink)); assertEquals("abcdef", sink.readUtf8()); assertTrue(source.exhausted()); }
@Test public void readBytes() throws Exception { sink.write(new byte[] { (byte) 0xab, (byte) 0xcd }); sink.emit(); assertEquals(0xab, source.readByte() & 0xff); assertEquals(0xcd, source.readByte() & 0xff); assertTrue(source.exhausted()); }
@Test public void readShortLe() throws Exception { sink.write(new byte[] { (byte) 0xab, (byte) 0xcd, (byte) 0xef, (byte) 0x10 }); sink.emit(); assertEquals((short) 0xcdab, source.readShortLe()); assertEquals((short) 0x10ef, source.readShortLe()); assertTrue(source.exhausted()); }
@Test public void readLong() throws Exception { sink.write(new byte[] { (byte) 0xab, (byte) 0xcd, (byte) 0xef, (byte) 0x10, (byte) 0x87, (byte) 0x65, (byte) 0x43, (byte) 0x21, (byte) 0x36, (byte) 0x47, (byte) 0x58, (byte) 0x69, (byte) 0x12, (byte) 0x23, (byte) 0x34, (byte) 0x45 }); sink.emit(); assertEquals(0xabcdef1087654321L, source.readLong()); assertEquals(0x3647586912233445L, source.readLong()); assertTrue(source.exhausted()); }
@Test public void readIntLe() throws Exception { sink.write(new byte[] { (byte) 0xab, (byte) 0xcd, (byte) 0xef, (byte) 0x10, (byte) 0x87, (byte) 0x65, (byte) 0x43, (byte) 0x21 }); sink.emit(); assertEquals(0x10efcdab, source.readIntLe()); assertEquals(0x21436587, source.readIntLe()); assertTrue(source.exhausted()); }
@Test public void readLongLe() throws Exception { sink.write(new byte[] { (byte) 0xab, (byte) 0xcd, (byte) 0xef, (byte) 0x10, (byte) 0x87, (byte) 0x65, (byte) 0x43, (byte) 0x21, (byte) 0x36, (byte) 0x47, (byte) 0x58, (byte) 0x69, (byte) 0x12, (byte) 0x23, (byte) 0x34, (byte) 0x45 }); sink.emit(); assertEquals(0x2143658710efcdabL, source.readLongLe()); assertEquals(0x4534231269584736L, source.readLongLe()); assertTrue(source.exhausted()); }
@Test public void readShort() throws Exception { sink.write(new byte[] { (byte) 0xab, (byte) 0xcd, (byte) 0xef, (byte) 0x01 }); sink.emit(); assertEquals((short) 0xabcd, source.readShort()); assertEquals((short) 0xef01, source.readShort()); assertTrue(source.exhausted()); }
@Test public void readInt() throws Exception { sink.write(new byte[] { (byte) 0xab, (byte) 0xcd, (byte) 0xef, (byte) 0x01, (byte) 0x87, (byte) 0x65, (byte) 0x43, (byte) 0x21 }); sink.emit(); assertEquals(0xabcdef01, source.readInt()); assertEquals(0x87654321, source.readInt()); assertTrue(source.exhausted()); }
@Test public void readIntSplitAcrossMultipleSegments() throws Exception { sink.writeUtf8(repeat('a', SEGMENT_SIZE - 3)); sink.write(new byte[] { (byte) 0xab, (byte) 0xcd, (byte) 0xef, (byte) 0x01 }); sink.emit(); source.skip(SEGMENT_SIZE - 3); assertEquals(0xabcdef01, source.readInt()); assertTrue(source.exhausted()); }
@Test public void emptyLines() throws IOException { data.writeUtf8("\n\n\n"); assertEquals("", source.readUtf8LineStrict()); assertEquals("", source.readUtf8LineStrict()); assertEquals("", source.readUtf8LineStrict()); assertTrue(source.exhausted()); }
@Test public void readShortSplitAcrossMultipleSegments() throws Exception { sink.writeUtf8(repeat('a', SEGMENT_SIZE - 1)); sink.write(new byte[] { (byte) 0xab, (byte) 0xcd }); sink.emit(); source.skip(SEGMENT_SIZE - 1); assertEquals((short) 0xabcd, source.readShort()); assertTrue(source.exhausted()); }
@Test public void readLongSplitAcrossMultipleSegments() throws Exception { sink.writeUtf8(repeat('a', SEGMENT_SIZE - 7)); sink.write(new byte[] { (byte) 0xab, (byte) 0xcd, (byte) 0xef, (byte) 0x01, (byte) 0x87, (byte) 0x65, (byte) 0x43, (byte) 0x21, }); sink.emit(); source.skip(SEGMENT_SIZE - 7); assertEquals(0xabcdef0187654321L, source.readLong()); assertTrue(source.exhausted()); }
@Test public void skip() throws Exception { sink.writeUtf8("a"); sink.writeUtf8(repeat('b', SEGMENT_SIZE)); sink.writeUtf8("c"); sink.emit(); source.skip(1); assertEquals('b', source.readByte() & 0xff); source.skip(SEGMENT_SIZE - 2); assertEquals('b', source.readByte() & 0xff); source.skip(1); assertTrue(source.exhausted()); }
@Test public void select() throws IOException { Options options = Options.Companion.of( ByteString.encodeUtf8("ROCK"), ByteString.encodeUtf8("SCISSORS"), ByteString.encodeUtf8("PAPER")); sink.writeUtf8("PAPER,SCISSORS,ROCK"); sink.emit(); assertEquals(2, source.select(options)); assertEquals(',', source.readByte()); assertEquals(1, source.select(options)); assertEquals(',', source.readByte()); assertEquals(0, source.select(options)); assertTrue(source.exhausted()); }