public void run() throws IOException, InterruptedException { Random random = new Random(3782615686L); BufferedSource source = Okio.buffer(new RandomSource(random, 5)); System.out.println(source.readUtf8()); }
private String parseEvent(long end) throws IOException { String type = null; end -= skipNameAndDivider(5L); if (end != 0L) { type = source.readUtf8(end); } skipCrAndOrLf(); return type; }
private String parseId(long end) throws IOException { String id; end -= skipNameAndDivider(2L); if (end != 0L) { id = source.readUtf8(end); } else { id = null; } skipCrAndOrLf(); return id; }
private void parseRetry(long end) throws IOException { end -= skipNameAndDivider(5L); String retryString = source.readUtf8(end); long retryMs = -1L; try { retryMs = Long.parseLong(retryString); } catch (NumberFormatException ignored) { } if (retryMs != -1L) { callback.onRetryChange(retryMs); } skipCrAndOrLf(); }
@Test public void testReadChannelFully() throws Exception { ReadableByteChannel channel = new Buffer().writeUtf8(quote); BufferedSource source = Okio.buffer(new ByteChannelSource(channel, Timeout.NONE)); assertThat(source.readUtf8()) .isEqualTo(quote); }
@Test public void markNothingBuffered() throws Exception { SourceMarker marker = new SourceMarker(new Buffer().writeUtf8("ABCDEFGHIJKLMNOPQRSTUVWXYZ")); BufferedSource source = marker.source(); long pos0 = marker.mark(5); assertThat(source.readUtf8(4)).isEqualTo("ABCD"); marker.reset(pos0); assertThat(source.readUtf8(6)).isEqualTo("ABCDEF"); }
@Test public void closeThenRead() throws Exception { SourceMarker marker = new SourceMarker(new Buffer().writeUtf8("ABCDEFGHIJKLMNOPQRSTUVWXYZ")); BufferedSource source = marker.source(); source.close(); try { source.readUtf8(3); fail(); } catch (IllegalStateException expected) { assertThat(expected).hasMessage("closed"); } }
@Test public void readWritePath() throws Exception { Path path = temporaryFolder.newFile().toPath(); BufferedSink sink = Okio.buffer(Okio.sink(path)); sink.writeUtf8("Hello, java.nio file!"); sink.close(); assertTrue(Files.exists(path)); assertEquals(21, Files.size(path)); BufferedSource source = Okio.buffer(Okio.source(path)); assertEquals("Hello, java.nio file!", source.readUtf8()); source.close(); }
@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 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 readUtf8SpansSegments() throws Exception { sink.writeUtf8(repeat('a', SEGMENT_SIZE * 2)); sink.emit(); source.skip(SEGMENT_SIZE - 1); assertEquals("aa", source.readUtf8(2)); }
@Test public void readUtf8Segment() throws Exception { sink.writeUtf8(repeat('a', SEGMENT_SIZE)); sink.emit(); assertEquals(repeat('a', SEGMENT_SIZE), source.readUtf8(SEGMENT_SIZE)); }
@Test public void readByteStringTooShortThrows() throws IOException { sink.writeUtf8("abc"); sink.emit(); try { source.readByteString(4); fail(); } catch (EOFException expected) { } assertEquals("abc", source.readUtf8()); // The read shouldn't consume any data. }
@Test public void readUtf8EntireBuffer() throws Exception { sink.writeUtf8(repeat('a', SEGMENT_SIZE * 2)); sink.emit(); assertEquals(repeat('a', SEGMENT_SIZE * 2), source.readUtf8()); }
@Test public void longDecimalStringAcrossSegment() throws IOException { sink.writeUtf8(repeat('a', SEGMENT_SIZE - 8)).writeUtf8("1234567890123456"); sink.writeUtf8("zzz"); sink.emit(); source.skip(SEGMENT_SIZE - 8); assertEquals(1234567890123456L, source.readDecimalLong()); assertEquals("zzz", source.readUtf8()); }
@Test public void selectLongerThanSource() throws IOException { Options options = Options.Companion.of( ByteString.encodeUtf8("abcd"), ByteString.encodeUtf8("abce"), ByteString.encodeUtf8("abcc")); sink.writeUtf8("abc"); sink.emit(); assertEquals(-1, source.select(options)); assertEquals("abc", source.readUtf8()); }
@Test public void selectReturnsFirstByteStringThatMatches() throws IOException { Options options = Options.Companion.of( ByteString.encodeUtf8("abcd"), ByteString.encodeUtf8("abc"), ByteString.encodeUtf8("abcde")); sink.writeUtf8("abcdef"); sink.emit(); assertEquals(0, source.select(options)); assertEquals("ef", source.readUtf8()); }
@Test public void indexOfByteStringWithOffset() throws IOException { assertEquals(-1, source.indexOf(ByteString.encodeUtf8("flop"), 1)); sink.writeUtf8("flop flip flop"); sink.emit(); assertEquals(10, source.indexOf(ByteString.encodeUtf8("flop"), 1)); source.readUtf8(); // Clear stream // Make sure we backtrack and resume searching after partial match. sink.writeUtf8("hi hi hi hi hey"); sink.emit(); assertEquals(6, source.indexOf(ByteString.encodeUtf8("hi hi hey"), 1)); }
@Test public void indexOfByteString() throws IOException { assertEquals(-1, source.indexOf(ByteString.encodeUtf8("flop"))); sink.writeUtf8("flip flop"); sink.emit(); assertEquals(5, source.indexOf(ByteString.encodeUtf8("flop"))); source.readUtf8(); // Clear stream. // Make sure we backtrack and resume searching after partial match. sink.writeUtf8("hi hi hi hey"); sink.emit(); assertEquals(3, source.indexOf(ByteString.encodeUtf8("hi hi hey"))); }