Skip to content

okio / okio / BufferedSource / indexOf

indexOf

(js, jvm, native) abstract fun indexOf(b:Byte):Long

Equivalent to indexOf(b, 0).

(js, jvm, native) abstract fun indexOf(b:Byte, fromIndex:Long):Long

Returns the index of the first b in the buffer at or after fromIndex. 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.

Buffer buffer = new Buffer();
buffer.writeUtf8("Don't move! He can't see us if we don't move.");

byte m = 'm';
assertEquals(6,  buffer.indexOf(m));
assertEquals(40, buffer.indexOf(m, 12));

(js, jvm, native) abstract fun indexOf(b:Byte, fromIndex:Long, toIndex:Long):Long

Returns the index of b if it is found in the range of fromIndex inclusive to toIndex exclusive. If b isn’t found, or if fromIndex == toIndex, then -1 is returned.

The scan terminates at either toIndex or the end of the buffer, whichever comes first. The maximum number of bytes scanned is toIndex-fromIndex.

(js, jvm, native) abstract fun indexOf(bytes:ByteString):Long

Equivalent to indexOf(bytes, 0).

(js, jvm, native) abstract fun indexOf(bytes:ByteString, fromIndex:Long):Long

Returns the index of the first match for bytes in the buffer at or after fromIndex. This expands the buffer as necessary until bytes is found. This reads an unbounded number of bytes into the buffer. Returns -1 if the stream is exhausted before the requested bytes are found.

ByteString MOVE = ByteString.encodeUtf8("move");

Buffer buffer = new Buffer();
buffer.writeUtf8("Don't move! He can't see us if we don't move.");

assertEquals(6,  buffer.indexOf(MOVE));
assertEquals(40, buffer.indexOf(MOVE, 12));