okio.BufferedSource.indexOf()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(10.5k)|赞(0)|评价(0)|浏览(123)

本文整理了Java中okio.BufferedSource.indexOf()方法的一些代码示例,展示了BufferedSource.indexOf()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。BufferedSource.indexOf()方法的具体详情如下:
包路径:okio.BufferedSource
类名称:BufferedSource
方法名:indexOf

BufferedSource.indexOf介绍

[英]Returns the index of the first b in the buffer. This expands the buffer as necessary until b is found. This reads an unbounded number of bytes into the buffer. Returns -1 if the stream is exhausted before the requested byte is found.
[中]返回缓冲区中第一个b的索引。这会根据需要扩展缓冲区,直到找到b为止。这会将无限数量的字节读入缓冲区。如果在找到请求的字节之前流已耗尽,则返回-1。

代码示例

代码示例来源:origin: square/moshi

/**
 * Skips through the next closing block comment.
 */
private boolean skipToEndOfBlockComment() throws IOException {
 long index = source.indexOf(CLOSING_BLOCK_COMMENT);
 boolean found = index != -1;
 buffer.skip(found ? index + CLOSING_BLOCK_COMMENT.size() : buffer.size());
 return found;
}

代码示例来源:origin: square/okio

@Test public void indexOfByteWithFromIndex() throws Exception {
 sink.writeUtf8("aaa");
 sink.emit();
 assertEquals(0, source.indexOf((byte) 'a'));
 assertEquals(0, source.indexOf((byte) 'a', 0));
 assertEquals(1, source.indexOf((byte) 'a', 1));
 assertEquals(2, source.indexOf((byte) 'a', 2));
}

代码示例来源:origin: square/okio

@Test public void indexOfByteInvalidBoundsThrows() throws IOException {
 sink.writeUtf8("abc");
 sink.emit();
 try {
  source.indexOf((byte) 'a', -1);
  fail("Expected failure: fromIndex < 0");
 } catch (IllegalArgumentException expected) {
 }
 try {
  source.indexOf((byte) 'a', 10, 0);
  fail("Expected failure: fromIndex > toIndex");
 } catch (IllegalArgumentException expected) {
 }
}

代码示例来源:origin: square/okio

@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));
}

代码示例来源:origin: square/okio

@Test public void indexOfByteStringAtSegmentBoundary() throws IOException {
 sink.writeUtf8(repeat('a', SEGMENT_SIZE - 1));
 sink.writeUtf8("bcd");
 sink.emit();
 assertEquals(SEGMENT_SIZE - 3, source.indexOf(ByteString.encodeUtf8("aabc"), SEGMENT_SIZE - 4));
 assertEquals(SEGMENT_SIZE - 3, source.indexOf(ByteString.encodeUtf8("aabc"), SEGMENT_SIZE - 3));
 assertEquals(SEGMENT_SIZE - 2, source.indexOf(ByteString.encodeUtf8("abcd"), SEGMENT_SIZE - 2));
 assertEquals(SEGMENT_SIZE - 2, source.indexOf(ByteString.encodeUtf8("abc"),  SEGMENT_SIZE - 2));
 assertEquals(SEGMENT_SIZE - 2, source.indexOf(ByteString.encodeUtf8("abc"),  SEGMENT_SIZE - 2));
 assertEquals(SEGMENT_SIZE - 2, source.indexOf(ByteString.encodeUtf8("ab"),   SEGMENT_SIZE - 2));
 assertEquals(SEGMENT_SIZE - 2, source.indexOf(ByteString.encodeUtf8("a"),    SEGMENT_SIZE - 2));
 assertEquals(SEGMENT_SIZE - 1, source.indexOf(ByteString.encodeUtf8("bc"),   SEGMENT_SIZE - 2));
 assertEquals(SEGMENT_SIZE - 1, source.indexOf(ByteString.encodeUtf8("b"),    SEGMENT_SIZE - 2));
 assertEquals(SEGMENT_SIZE,     source.indexOf(ByteString.encodeUtf8("c"),    SEGMENT_SIZE - 2));
 assertEquals(SEGMENT_SIZE,     source.indexOf(ByteString.encodeUtf8("c"),    SEGMENT_SIZE    ));
 assertEquals(SEGMENT_SIZE + 1, source.indexOf(ByteString.encodeUtf8("d"),    SEGMENT_SIZE - 2));
 assertEquals(SEGMENT_SIZE + 1, source.indexOf(ByteString.encodeUtf8("d"),    SEGMENT_SIZE + 1));
}

代码示例来源:origin: square/okio

sink.emit();
assertEquals( p, source.indexOf(c, 0,      size     ));
assertEquals( p, source.indexOf(c, 0,      p + 1    ));
assertEquals( p, source.indexOf(c, p,      size     ));
assertEquals( p, source.indexOf(c, p,      p + 1    ));
assertEquals( p, source.indexOf(c, p / 2,  p * 2 + 1));
assertEquals(-1, source.indexOf(c, 0,      p / 2    ));
assertEquals(-1, source.indexOf(c, 0,      p        ));
assertEquals(-1, source.indexOf(c, 0,      0        ));
assertEquals(-1, source.indexOf(c, p,      p        ));

代码示例来源:origin: square/okio

@Test public void indexOfByteStringInvalidArgumentsThrows() throws IOException {
 try {
  source.indexOf(ByteString.of());
  fail();
 } catch (IllegalArgumentException e) {
  assertEquals("bytes is empty", e.getMessage());
 }
 try {
  source.indexOf(ByteString.encodeUtf8("hi"), -1);
  fail();
 } catch (IllegalArgumentException e) {
  assertEquals("fromIndex < 0: -1", e.getMessage());
 }
}

代码示例来源:origin: square/okio

@Test public void indexOf() throws Exception {
 assertEquals(-1, source.indexOf((byte) 'a'));
 assertEquals(0, source.indexOf((byte) 'a'));
 assertEquals(-1, source.indexOf((byte) 'b'));
 assertEquals(0, source.indexOf((byte) 'a'));
 assertEquals(1, source.indexOf((byte) 'b'));
 assertEquals(-1, source.indexOf((byte) 'c'));
 assertEquals(-1, source.indexOf((byte) 'a'));
 assertEquals(0, source.indexOf((byte) 'b'));
 assertEquals(-1, source.indexOf((byte) 'c'));
 assertEquals(-1, source.indexOf((byte) 'a'));
 assertEquals(0, source.indexOf((byte) 'b'));
 assertEquals(SEGMENT_SIZE - 3, source.indexOf((byte) 'c'));
 assertEquals(-1, source.indexOf((byte) 'a'));
 assertEquals(0, source.indexOf((byte) 'b'));
 assertEquals(SEGMENT_SIZE - 5, source.indexOf((byte) 'c'));
 assertEquals(SEGMENT_SIZE - 4, source.indexOf((byte) 'd'));
 assertEquals(-1, source.indexOf((byte) 'e'));

代码示例来源:origin: square/okio

/**
 * With {@link Factory#ONE_BYTE_AT_A_TIME_BUFFERED_SOURCE}, this code was extremely slow.
 * https://github.com/square/okio/issues/171
 */
@Test public void indexOfByteStringAcrossSegmentBoundaries() throws IOException {
 sink.writeUtf8(repeat('a', SEGMENT_SIZE * 2 - 3));
 sink.writeUtf8("bcdefg");
 sink.emit();
 assertEquals(SEGMENT_SIZE * 2 - 4, source.indexOf(ByteString.encodeUtf8("ab")));
 assertEquals(SEGMENT_SIZE * 2 - 4, source.indexOf(ByteString.encodeUtf8("abc")));
 assertEquals(SEGMENT_SIZE * 2 - 4, source.indexOf(ByteString.encodeUtf8("abcd")));
 assertEquals(SEGMENT_SIZE * 2 - 4, source.indexOf(ByteString.encodeUtf8("abcde")));
 assertEquals(SEGMENT_SIZE * 2 - 4, source.indexOf(ByteString.encodeUtf8("abcdef")));
 assertEquals(SEGMENT_SIZE * 2 - 4, source.indexOf(ByteString.encodeUtf8("abcdefg")));
 assertEquals(SEGMENT_SIZE * 2 - 3, source.indexOf(ByteString.encodeUtf8("bcdefg")));
 assertEquals(SEGMENT_SIZE * 2 - 2, source.indexOf(ByteString.encodeUtf8("cdefg")));
 assertEquals(SEGMENT_SIZE * 2 - 1, source.indexOf(ByteString.encodeUtf8("defg")));
 assertEquals(SEGMENT_SIZE * 2,     source.indexOf(ByteString.encodeUtf8("efg")));
 assertEquals(SEGMENT_SIZE * 2 + 1, source.indexOf(ByteString.encodeUtf8("fg")));
 assertEquals(SEGMENT_SIZE * 2 + 2, source.indexOf(ByteString.encodeUtf8("g")));
}

代码示例来源:origin: square/okio

@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));
}

代码示例来源:origin: square/okio

@Test public void indexOfStopsReadingAtLimit() throws Exception {
 Buffer buffer = new Buffer().writeUtf8("abcdef");
 BufferedSource bufferedSource = Okio.buffer(new ForwardingSource(buffer) {
  @Override public long read(Buffer sink, long byteCount) throws IOException {
   return super.read(sink, Math.min(1, byteCount));
  }
 });
 assertEquals(6, buffer.size());
 assertEquals(-1, bufferedSource.indexOf((byte) 'e', 0, 4));
 assertEquals(2, buffer.size());
}

代码示例来源:origin: square/okio

@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));
}

代码示例来源:origin: square/okio

@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")));
}

代码示例来源:origin: square/okio

@Test public void indexOfDoesNotWrapAround() throws IOException {
 sink.writeUtf8(repeat('a', SEGMENT_SIZE - 1));
 sink.writeUtf8("bcd");
 sink.emit();
 assertEquals(-1, source.indexOf(ByteString.encodeUtf8("abcda"), SEGMENT_SIZE - 3));
}

代码示例来源:origin: square/okio

bufferedSource.indexOf((byte) 1);
 fail();
} catch (IllegalStateException expected) {

代码示例来源:origin: Tickaroo/tikxml

/**
 * Returns the index of the last character before starting the CDATA closing tag "{@code ]]>}".
 * This method does not consume the closing CDATA tag.
 *
 * @return index of last character before closing tag.
 * @throws IOException
 */
private long indexOfClosingCDATA() throws IOException {
 long index = source.indexOf(CDATA_CLOSE);
 if (index == -1) {
  throw new EOFException("<![CDATA[ at " + getPath() + " has never been closed with ]]>");
 }
 return index;
}

代码示例来源:origin: com.squareup.moshi/moshi

/**
 * Skips through the next closing block comment.
 */
private boolean skipToEndOfBlockComment() throws IOException {
 long index = source.indexOf(CLOSING_BLOCK_COMMENT);
 boolean found = index != -1;
 buffer.skip(found ? index + CLOSING_BLOCK_COMMENT.size() : buffer.size());
 return found;
}

代码示例来源:origin: Tickaroo/tikxml

StringBuilder builder = null;
while (true) {
 long index = source.indexOf(runTerminator);
 if (index == -1L) {
  throw syntaxError(

代码示例来源:origin: Tickaroo/tikxml

/**
 * Skip a quoted value
 *
 * @param runTerminator The terminator to skip
 * @throws IOException
 */
private void skipQuotedValue(Byte runTerminator) throws IOException {
 while (true) {
  long index = source.indexOf(runTerminator);
  if (index == -1L) throw syntaxError("Unterminated string");
  if (buffer.getByte(index) == '\\') {
   buffer.skip(index + 1);
   readEscapeCharacter();
  } else {
   buffer.skip(index + 1);
   return;
  }
 }
}

代码示例来源:origin: Tickaroo/tikxml

/**
 * Skip the text content. Text content is {@code <element>text content</element>}
 *
 * @throws IOException
 */
public void skipTextContent() throws IOException {
 int p = peeked;
 if (p == PEEKED_NONE) {
  p = doPeek();
 }
 if (p == PEEKED_ELEMENT_TEXT_CONTENT) {
  peeked = PEEKED_NONE;
  // Read text until '<' found
  long index = source.indexOf(OPENING_XML_ELEMENT);
  if (index == -1L) {
   throw syntaxError("Unterminated element text content. Expected </"
     + pathNames[stackSize - 1]
     + "> but haven't found");
  }
  buffer.skip(index);
 } else if (p == PEEKED_CDATA) {
  peeked = PEEKED_NONE;
  // Search index of closing CDATA tag ]]>
  long index = indexOfClosingCDATA();
  buffer.skip(index + 3); // +3 because of consuming closing tag
 } else {
  throw new XmlDataException("Expected xml element text content but was " + peek()
    + " at path " + getPath());
 }
}

相关文章

微信公众号

最新文章

更多