@Test public void bytesEmittedToSinkWithEmit() throws Exception { Buffer sink = new Buffer(); BufferedSink bufferedSink = Okio.buffer((Sink) sink); bufferedSink.writeUtf8("abc"); bufferedSink.emit(); assertEquals(3, sink.size()); }
@Test public void inputStreamCharByChar() throws Exception { sink.writeUtf8("abc"); sink.emit(); InputStream in = source.inputStream(); assertEquals('a', in.read()); assertEquals('b', in.read()); assertEquals('c', in.read()); assertEquals(-1, in.read()); }
@Test public void longHexStringTooShortThrows() throws IOException { try { sink.writeUtf8(" "); sink.emit(); source.readHexadecimalUnsignedLong(); fail(); } catch (NumberFormatException e) { assertEquals("Expected leading [0-9a-fA-F] character but was 0x20", e.getMessage()); } }
@Test public void longDecimalEmptyThrows() throws IOException { try { sink.writeUtf8(""); sink.emit(); source.readDecimalLong(); fail(); } catch (EOFException expected) { } }
@Test public void readFullyTooShortThrows() throws IOException { sink.writeUtf8("Hi"); sink.emit(); Buffer sink = new Buffer(); try { source.readFully(sink, 5); fail(); } catch (EOFException ignored) { } // Verify we read all that we could from the source. assertEquals("Hi", sink.readUtf8()); }
@Test public void readLongTooShortThrows() throws IOException { sink.writeLong(Long.MAX_VALUE); sink.emit(); source.readByte(); try { source.readLong(); fail(); } catch (EOFException expected) { } }
@Test public void readFullyByteArrayTooShortThrows() throws IOException { sink.writeUtf8("Hello"); sink.emit(); byte[] array = new byte[6]; try { source.readFully(array); fail(); } catch (EOFException ignored) { } // Verify we read all that we could from the source. assertByteArraysEquals(new byte[] { 'H', 'e', 'l', 'l', 'o', 0 }, array); }
@Test public void readByteArrayTooShortThrows() throws IOException { sink.writeUtf8("abc"); sink.emit(); try { source.readByteArray(4); fail(); } catch (EOFException expected) { } assertEquals("abc", source.readUtf8()); // The read shouldn't consume any data. }
@Test public void readUtf8PartialBuffer() throws Exception { sink.writeUtf8(repeat('a', SEGMENT_SIZE + 20)); sink.emit(); assertEquals(repeat('a', SEGMENT_SIZE + 10), source.readUtf8(SEGMENT_SIZE + 10)); }
@Test public void readUtf8TooShortThrows() throws IOException { sink.writeUtf8("abc"); sink.emit(); try { source.readUtf8(4L); fail(); } catch (EOFException expected) { } assertEquals("abc", source.readUtf8()); // The read shouldn't consume any data. }
@Test public void longHexStringAcrossSegment() throws IOException { sink.writeUtf8(repeat('a', SEGMENT_SIZE - 8)).writeUtf8("FFFFFFFFFFFFFFFF"); sink.emit(); source.skip(SEGMENT_SIZE - 8); assertEquals(-1, source.readHexadecimalUnsignedLong()); }
@Test public void readFullyByteArray() throws IOException { Buffer data = new Buffer(); data.writeUtf8("Hello").writeUtf8(repeat('e', SEGMENT_SIZE)); byte[] expected = data.clone().readByteArray(); sink.write(data, data.size()); sink.emit(); byte[] sink = new byte[SEGMENT_SIZE + 5]; source.readFully(sink); assertByteArraysEquals(expected, sink); }
@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 indexOfByteWithStartOffset() throws IOException { sink.writeUtf8("a").writeUtf8(repeat('b', SEGMENT_SIZE)).writeUtf8("c"); sink.emit(); assertEquals(-1, source.indexOf((byte) 'a', 1)); assertEquals(15, source.indexOf((byte) 'b', 15)); }
@Test public void rangeEqualsOnlyReadsUntilMismatch() throws IOException { assumeTrue(factory == Factory.ONE_BYTE_AT_A_TIME_BUFFERED_SOURCE); // Other sources read in chunks anyway. sink.writeUtf8("A man, a plan, a canal. Panama."); sink.emit(); assertFalse(source.rangeEquals(0, ByteString.encodeUtf8("A man."))); assertEquals("A man,", source.getBuffer().readUtf8()); }
@Test public void selectNotFound() throws IOException { Options options = Options.Companion.of( ByteString.encodeUtf8("ROCK"), ByteString.encodeUtf8("SCISSORS"), ByteString.encodeUtf8("PAPER")); sink.writeUtf8("SPOCK"); sink.emit(); assertEquals(-1, source.select(options)); assertEquals("SPOCK", source.readUtf8()); }
@Test public void indexOfByteStringWithFromIndex() throws Exception { sink.writeUtf8("aaa"); sink.emit(); assertEquals(0, source.indexOf(ByteString.encodeUtf8("a"))); assertEquals(0, source.indexOf(ByteString.encodeUtf8("a"), 0)); assertEquals(1, source.indexOf(ByteString.encodeUtf8("a"), 1)); assertEquals(2, source.indexOf(ByteString.encodeUtf8("a"), 2)); }
@Test public void indexOfElementWithFromIndex() throws Exception { sink.writeUtf8("aaa"); sink.emit(); assertEquals(0, source.indexOfElement(ByteString.encodeUtf8("a"))); assertEquals(0, source.indexOfElement(ByteString.encodeUtf8("a"), 0)); assertEquals(1, source.indexOfElement(ByteString.encodeUtf8("a"), 1)); assertEquals(2, source.indexOfElement(ByteString.encodeUtf8("a"), 2)); }
@Test public void indexOfElement() throws IOException { sink.writeUtf8("a").writeUtf8(repeat('b', SEGMENT_SIZE)).writeUtf8("c"); sink.emit(); assertEquals(0, source.indexOfElement(ByteString.encodeUtf8("DEFGaHIJK"))); assertEquals(1, source.indexOfElement(ByteString.encodeUtf8("DEFGHIJKb"))); assertEquals(SEGMENT_SIZE + 1, source.indexOfElement(ByteString.encodeUtf8("cDEFGHIJK"))); assertEquals(1, source.indexOfElement(ByteString.encodeUtf8("DEFbGHIc"))); assertEquals(-1L, source.indexOfElement(ByteString.encodeUtf8("DEFGHIJK"))); assertEquals(-1L, source.indexOfElement(ByteString.encodeUtf8(""))); }