Open
Description
I wonder why there isn't a convenient copy method.
I would have expected that in SystemFileSystem
.
For now I wrote a handy extension function to Path:
@OptIn(ExperimentalStdlibApi::class)
fun Path.copyTo(destination: Path) {
require(SystemFileSystem.exists(this)) { "$this does not exist." }
val metadata = SystemFileSystem.metadataOrNull(this)
requireNotNull(metadata) { "Failed to read metadata of $this" }
require(metadata.isRegularFile) { "Source $this must be a regular file." }
SystemFileSystem.source(this).buffered().use { rawSource ->
SystemFileSystem.sink(destination).buffered().use { sink ->
sink.write(rawSource, metadata.size)
}
}
}
Do I miss something? Is copying hard? There must be a reason it's not there yet.