Files
blitz-kt/src/main/kotlin/blitz/io/File.kt
alexander.nutz 6e9300fac6 refactor
2024-03-25 09:19:27 +01:00

34 lines
910 B
Kotlin

package blitz.io
import blitz.collections.ByteBatchSequence
import kotlinx.io.Buffer
import kotlinx.io.files.SystemFileSystem
@JvmInline
value class File internal constructor(
val path: Path
)
fun File.read(): ByteBatchSequence =
path.toKotlinxPath().let {
{ SystemFileSystem.source(it) }
}.readerSequence()
private fun File.writeAppendFrom(seq: ByteBatchSequence, append: Boolean) {
val path = path.toKotlinxPath()
SystemFileSystem.sink(path, append).use { sink ->
val iter = seq.iterator()
while (iter.hasNext()) {
val batch = iter.nextBytes(8192)
val buff = Buffer()
buff.write(batch)
sink.write(buff, buff.size)
}
}
}
fun File.write(seq: ByteBatchSequence) =
writeAppendFrom(seq, false)
fun File.append(seq: ByteBatchSequence) =
writeAppendFrom(seq, true)