Skip to content

Commit 701634b

Browse files
committed
Make Path.toFile() work for Java callers
We were using extension functions but we don't need to. Having dereferencing callsites is much nicer when calling from Java.
1 parent a8ccc12 commit 701634b

18 files changed

Lines changed: 69 additions & 62 deletions

File tree

okio/src/commonMain/kotlin/okio/-Platform.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ internal expect val PLATFORM_FILE_SYSTEM: FileSystem
2222
@ExperimentalFileSystem
2323
internal expect val PLATFORM_TEMPORARY_DIRECTORY: Path
2424

25-
internal expect val DIRECTORY_SEPARATOR: String
26-
2725
internal expect fun ByteArray.toUtf8String(): String
2826

2927
internal expect fun String.asUtf8ToByteArray(): ByteArray

okio/src/commonMain/kotlin/okio/Path.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ expect class Path internal constructor(slash: ByteString, bytes: ByteString) : C
204204
override fun toString(): String
205205

206206
companion object {
207-
val directorySeparator: String
207+
val DIRECTORY_SEPARATOR: String
208208

209209
fun String.toPath(): Path
210210

okio/src/commonMain/kotlin/okio/internal/Path.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package okio.internal
1818
import okio.Buffer
1919
import okio.ByteString
2020
import okio.ByteString.Companion.encodeUtf8
21-
import okio.DIRECTORY_SEPARATOR
2221
import okio.ExperimentalFileSystem
2322
import okio.Path
2423

@@ -194,7 +193,7 @@ internal fun Buffer.toPath(directorySeparator: ByteString? = null): Path {
194193
// This path doesn't start with any slash. We must initialize the slash character to use.
195194
val limit = indexOfElement(ANY_SLASH)
196195
slash = slash ?: when (limit) {
197-
-1L -> DIRECTORY_SEPARATOR.toSlash()
196+
-1L -> Path.DIRECTORY_SEPARATOR.toSlash()
198197
else -> get(limit).toSlash()
199198
}
200199
if (startsWithVolumeLetterAndColon(slash)) {

okio/src/commonTest/kotlin/okio/AbstractFileSystemTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ abstract class AbstractFileSystemTest(
5555
val cwd = fileSystem.canonicalize(".".toPath())
5656
val cwdString = cwd.toString()
5757
assertTrue(cwdString) {
58-
cwdString.endsWith("okio${Path.directorySeparator}okio") ||
59-
cwdString.endsWith("${Path.directorySeparator}okio-parent-okio-test") || // JS
58+
cwdString.endsWith("okio${Path.DIRECTORY_SEPARATOR}okio") ||
59+
cwdString.endsWith("${Path.DIRECTORY_SEPARATOR}okio-parent-okio-test") || // JS
6060
cwdString.contains("/CoreSimulator/Devices/") || // iOS simulator.
6161
cwdString == "/" // Android emulator.
6262
}

okio/src/commonTest/kotlin/okio/SystemFileSystemTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ import kotlin.time.ExperimentalTime
2323
class SystemFileSystemTest : AbstractFileSystemTest(
2424
clock = Clock.System,
2525
fileSystem = FileSystem.SYSTEM,
26-
windowsLimitations = DIRECTORY_SEPARATOR == "\\",
26+
windowsLimitations = Path.DIRECTORY_SEPARATOR == "\\",
2727
temporaryDirectory = FileSystem.SYSTEM_TEMPORARY_DIRECTORY
2828
)

okio/src/jsMain/kotlin/okio/-Platform.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ internal actual val PLATFORM_FILE_SYSTEM: FileSystem
2525
internal actual val PLATFORM_TEMPORARY_DIRECTORY: Path
2626
get() = tmpdir().toPath()
2727

28-
internal actual val DIRECTORY_SEPARATOR: String
28+
internal actual val PLATFORM_DIRECTORY_SEPARATOR: String
2929
get() {
3030
// TODO(swankjesse): return path.path.sep instead, once it has @JsNonModule
3131
return when (platform()) {

okio/src/jvmMain/kotlin/okio/-Platform.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
package okio
1919

2020
import okio.Path.Companion.toPath
21-
import java.io.File
2221

2322
@ExperimentalFileSystem
2423
internal actual val PLATFORM_FILE_SYSTEM: FileSystem
@@ -35,8 +34,6 @@ internal actual val PLATFORM_FILE_SYSTEM: FileSystem
3534
internal actual val PLATFORM_TEMPORARY_DIRECTORY: Path
3635
get() = System.getProperty("java.io.tmpdir").toPath()
3736

38-
internal actual val DIRECTORY_SEPARATOR = File.separator
39-
4037
internal actual fun ByteArray.toUtf8String(): String = String(this, Charsets.UTF_8)
4138

4239
internal actual fun String.asUtf8ToByteArray(): ByteArray = toByteArray(Charsets.UTF_8)

okio/src/jvmMain/kotlin/okio/JvmSystemFileSystem.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package okio
1717

18+
import okio.Path.Companion.toOkioPath
19+
1820
/**
1921
* A file system that adapts `java.io`.
2022
*

okio/src/jvmMain/kotlin/okio/Path.kt

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ import okio.internal.commonResolve
2828
import okio.internal.commonToPath
2929
import okio.internal.commonToString
3030
import okio.internal.commonVolumeLetter
31+
import org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement
32+
import java.io.File
33+
import java.nio.file.Paths
34+
import java.nio.file.Path as NioPath
3135

3236
@ExperimentalFileSystem
3337
actual class Path internal actual constructor(
@@ -65,6 +69,11 @@ actual class Path internal actual constructor(
6569
@JvmName("resolve")
6670
actual operator fun div(child: Path): Path = commonResolve(child)
6771

72+
fun toFile(): File = File(toString())
73+
74+
@IgnoreJRERequirement // Can only be invoked on platforms that have java.nio.file.
75+
fun toNioPath(): NioPath = Paths.get(toString())
76+
6877
actual override fun compareTo(other: Path): Int = commonCompareTo(other)
6978

7079
actual override fun equals(other: Any?): Boolean = commonEquals(other)
@@ -74,12 +83,24 @@ actual class Path internal actual constructor(
7483
actual override fun toString() = commonToString()
7584

7685
actual companion object {
77-
actual val directorySeparator: String = DIRECTORY_SEPARATOR
86+
/**
87+
* Either `/` (on UNIX-like systems including Android, iOS, and Linux) or `\` (on Windows
88+
* systems).
89+
*/
90+
@JvmField
91+
actual val DIRECTORY_SEPARATOR: String = File.separator
7892

7993
@JvmName("get") @JvmStatic
8094
actual fun String.toPath(): Path = commonToPath()
8195

8296
@JvmName("get") @JvmStatic
8397
actual fun String.toPath(directorySeparator: String?): Path = commonToPath(directorySeparator)
98+
99+
@JvmName("get") @JvmStatic
100+
fun File.toOkioPath(): Path = toString().toPath()
101+
102+
@JvmName("get") @JvmStatic
103+
@IgnoreJRERequirement // Can only be invoked on platforms that have java.nio.file.
104+
fun NioPath.toOkioPath(): Path = toString().toPath()
84105
}
85106
}

okio/src/jvmMain/kotlin/okio/Paths.kt

Lines changed: 0 additions & 37 deletions
This file was deleted.

0 commit comments

Comments
 (0)