SNRK-67: Use kotlin.io.Path
This commit is contained in:
parent
7c6157be96
commit
e9eaa0f8c2
@ -3,43 +3,50 @@ package space.kscience.snark.storage.local
|
||||
import space.kscience.snark.storage.Directory
|
||||
import space.kscience.snark.storage.FileReader
|
||||
import space.kscience.snark.storage.FileWriter
|
||||
import java.io.File
|
||||
import java.nio.file.Path
|
||||
import kotlin.io.path.*
|
||||
|
||||
public fun localStorage(rootPath: Path): Directory {
|
||||
return LocalDirectory(rootPath, ".")
|
||||
return LocalDirectory(rootPath)
|
||||
}
|
||||
|
||||
private class LocalFile(private val path: String) : FileReader, FileWriter {
|
||||
private class LocalFile(private val path: Path) : FileReader, FileWriter {
|
||||
override fun close() {}
|
||||
override suspend fun readAll(): ByteArray = File(this.path).readBytes()
|
||||
override suspend fun readAll(): ByteArray = path.readBytes()
|
||||
|
||||
override suspend fun write(bytes: ByteArray) = File(this.path).writeBytes(bytes)
|
||||
override suspend fun write(bytes: ByteArray) = path.writeBytes(bytes)
|
||||
}
|
||||
|
||||
private class LocalDirectory(private val root: Path, path: String) : Directory {
|
||||
private val current = "$root${File.separator}$path"
|
||||
|
||||
private fun child(child: String): String = "$current${File.separator}$child"
|
||||
private class LocalDirectory(private val path: Path) : Directory {
|
||||
private fun child(child: String): Path = path / child
|
||||
private fun child(child: Path): Path = path / child
|
||||
|
||||
override fun close() {}
|
||||
|
||||
override suspend fun get(filename: String): FileReader = LocalFile(child(filename))
|
||||
|
||||
override suspend fun create(filename: String, ignoreIfExists: Boolean) {
|
||||
if (!File(child(filename)).createNewFile() && !ignoreIfExists) {
|
||||
throw UnsupportedOperationException("File already exists")
|
||||
try {
|
||||
child(filename).createFile()
|
||||
} catch (ex: FileAlreadyExistsException) {
|
||||
if (!ignoreIfExists) {
|
||||
throw ex
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun put(filename: String): FileWriter = LocalFile(child(filename))
|
||||
|
||||
override suspend fun getSubdir(path: Path): Directory = LocalDirectory(root, child(path.toString()))
|
||||
override suspend fun getSubdir(path: Path): Directory = LocalDirectory(child(path))
|
||||
override suspend fun createSubdir(dirname: String, ignoreIfExists: Boolean): Directory {
|
||||
val dir = child(dirname)
|
||||
if (!File(dir).mkdir() && !ignoreIfExists) {
|
||||
throw UnsupportedOperationException("File already exists")
|
||||
}
|
||||
return this.getSubdir(File(dir).toPath())
|
||||
try {
|
||||
dir.createDirectory()
|
||||
} catch (ex: FileAlreadyExistsException) {
|
||||
if (!ignoreIfExists) {
|
||||
throw ex
|
||||
}
|
||||
}
|
||||
return LocalDirectory(dir)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user