Skip to content

Add SystemFileSystem.copy() #233

Open
@StefanOltmann

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.

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions