Skip to content

Commit

Permalink
Add toFile and toNioPath for Path on JVM
Browse files Browse the repository at this point in the history
  • Loading branch information
Goooler committed Apr 27, 2024
1 parent a04ef0d commit dd3ec91
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
2 changes: 2 additions & 0 deletions core/api/kotlinx-io-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ public final class kotlinx/io/files/PathsJvmKt {
public static final fun Path (Ljava/lang/String;)Lkotlinx/io/files/Path;
public static final fun sink (Lkotlinx/io/files/Path;)Lkotlinx/io/Sink;
public static final fun source (Lkotlinx/io/files/Path;)Lkotlinx/io/Source;
public static final fun toFile (Lkotlinx/io/files/Path;)Ljava/io/File;
public static final fun toNioPath (Lkotlinx/io/files/Path;)Ljava/nio/file/Path;
}

public final class kotlinx/io/files/PathsKt {
Expand Down
4 changes: 4 additions & 0 deletions core/jvm/src/files/PathsJvm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ public actual val SystemPathSeparator: Char = File.separatorChar

public actual fun Path(path: String): Path = Path(File(path))

public fun Path.toFile(): File = file

public fun Path.toNioPath(): java.nio.file.Path = file.toPath()

// Function only exists to provide binary compatibility with the earlier releases
@JvmName("source")
@PublishedApi
Expand Down
17 changes: 17 additions & 0 deletions core/jvm/test/JvmPlatformTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ import java.nio.file.StandardOpenOption
import kotlin.io.path.inputStream
import kotlin.io.path.outputStream
import kotlin.test.*
import kotlinx.io.files.Path
import kotlinx.io.files.toFile
import kotlinx.io.files.toNioPath

class JvmPlatformTest {
@TempDir
Expand Down Expand Up @@ -200,4 +203,18 @@ class JvmPlatformTest {
source.readAtMostTo(buffer, 1L)
assertEquals(buffer.readString(), "a")
}

@Test
fun kotlinxPathToJavaIoFile() {
val file = File(tempDir, "new")
val kotlinxPath = Path(file.toString())
assertEquals(file, kotlinxPath.toFile())
}

@Test
fun kotlinxPathToJavaNioPath() {
val file = File(tempDir, "new")
val kotlinxPath = Path(file.toString())
assertEquals(file.toPath(), kotlinxPath.toNioPath())
}
}

0 comments on commit dd3ec91

Please sign in to comment.