indexOf

fun Buffer.indexOf(byte: Byte, startIndex: Long = 0, endIndex: Long = size): Long(source)

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

byte

the value to find.

startIndex

the start of the range (inclusive) to find byte, 0 by default.

endIndex

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
}

fun Source.indexOf(byteString: ByteString, startIndex: Long = 0): Long(source)

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

byteString

the sequence of bytes to find within the source.

startIndex

the index into the source to start searching from.

Throws

if the source is closed.

when some I/O error occurs.

Samples

import kotlinx.io.*
import kotlinx.io.bytestring.ByteString
import kotlinx.io.bytestring.encodeToByteString
import kotlin.test.*

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
}

fun Buffer.indexOf(byteString: ByteString, startIndex: Long = 0): Long(source)


fun Source.indexOf(byte: Byte, startIndex: Long = 0, endIndex: Long = Long.MAX_VALUE): Long(source)

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

byte

the value to find.

startIndex

the start of the range (inclusive) to find byte, 0 by default.

endIndex

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.

when some I/O error occurs.

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
}