indexOf
Returns an index of byte first occurrence in the range of startIndex to endIndex, or -1
when the range doesn't contain byte.
The scan terminates at either endIndex or buffers' exhaustion, whichever comes first.
Parameters
the value to find.
the start of the range (inclusive) to find byte, 0
by default.
the end of the range (exclusive) to find byte, Buffer.size by default.
Throws
when the source is closed.
when startIndex > endIndex
or either of indices is negative.
Samples
import kotlinx.io.*
import kotlin.test.*
fun main() {
//sampleStart
val buffer = Buffer()
assertEquals(-1, buffer.indexOf('\n'.code.toByte()))
buffer.writeString("Hello\nThis is line 2\nAnd this one is third.")
assertEquals(5, buffer.indexOf('\n'.code.toByte()))
assertEquals(20, buffer.indexOf('\n'.code.toByte(), startIndex = 6))
assertEquals(-1, buffer.indexOf('\n'.code.toByte(), startIndex = 21))
//sampleEnd
}
Returns the index of the first match for byteString in the source at or after startIndex. This expands the source's buffer as necessary until byteString 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.
Parameters
the sequence of bytes to find within the source.
the index into the source to start searching from.
Throws
if startIndex is negative.
if the source is closed.
Samples
import kotlinx.io.*
import kotlinx.io.bytestring.ByteString
import kotlinx.io.bytestring.encodeToByteString
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue
fun main() {
//sampleStart
val buffer = Buffer()
assertEquals(-1, buffer.indexOf(ByteString(1, 2, 3, 4)))
assertEquals(0, buffer.indexOf(ByteString(/* empty */)))
buffer.writeString("Content-Type: text/plain\nContent-Length: 12\n\nhello world!")
assertEquals(43, buffer.indexOf("\n\n".encodeToByteString()))
assertEquals(-1, buffer.indexOf("application/json".encodeToByteString()))
//sampleEnd
}
Returns an index of byte first occurrence in the range of startIndex to endIndex, or -1
when the range doesn't contain byte.
The scan terminates at either endIndex or source's exhaustion, whichever comes first. The maximum number of bytes scanned is toIndex-fromIndex
. If byte not found in buffered data, endIndex is yet to be reached and the underlying source is not yet exhausted then new data will be read from the underlying source into the buffer.
Parameters
the value to find.
the start of the range (inclusive) to find byte, 0
by default.
the end of the range (exclusive) to find byte, Long.MAX_VALUE by default.
Throws
when the source is closed.
when startIndex > endIndex
or either of indices is negative.
Samples
import kotlinx.io.*
import kotlin.test.*
fun main() {
//sampleStart
val buffer = Buffer()
assertEquals(-1, buffer.indexOf('\n'.code.toByte()))
buffer.writeString("Hello\nThis is line 2\nAnd this one is third.")
assertEquals(5, buffer.indexOf('\n'.code.toByte()))
assertEquals(20, buffer.indexOf('\n'.code.toByte(), startIndex = 6))
assertEquals(-1, buffer.indexOf('\n'.code.toByte(), startIndex = 21))
//sampleEnd
}