This commit is contained in:
SuperCraftAlex
2024-03-09 18:34:25 +01:00
parent a2f89fc962
commit 6f80ab30a3
31 changed files with 860 additions and 890 deletions

View File

@@ -0,0 +1,51 @@
package blitz.io
import kotlinx.io.RawSource
import kotlinx.io.buffered
import blitz.ByteBatchIterator
import blitz.ByteBatchSequence
import blitz.Provider
fun Provider<RawSource>.readerSequence(): ByteBatchSequence =
object : ByteBatchSequence {
inner class Iter: ByteBatchIterator {
val buffered = this@readerSequence().buffered()
override fun nextBytes(limit: Int): ByteArray {
val out = ByteArray(limit)
var i = 0
while (!(buffered.exhausted() || i == limit - 1))
out[i ++] = buffered.readByte()
return out.sliceArray(0..i)
}
override fun nextBytes(dest: ByteArray): Int =
nextBytes(dest.size).also { it.copyInto(dest) }.size
override fun next(limit: Int): List<Byte> =
nextBytes(limit).toList()
override fun next(dest: MutableList<Byte>, limit: Int) {
for (x in nextBytes(limit)) {
dest.add(x)
}
}
override fun next(dest: Array<Byte>): Int {
var i = 0
for (x in nextBytes(dest.size)) {
dest[i ++] = x
}
return i
}
override fun next(): Byte =
buffered.readByte()
override fun hasNext(): Boolean =
!buffered.exhausted()
}
override fun iterator(): ByteBatchIterator =
Iter()
}