Skip to content

Commit

Permalink
Merge pull request square#872 from square/jwilson.0106.java_sugar
Browse files Browse the repository at this point in the history
Make Path.toFile() work for Java callers
  • Loading branch information
swankjesse authored Jan 7, 2021
2 parents a8ccc12 + 701634b commit 1977d3b
Show file tree
Hide file tree
Showing 18 changed files with 69 additions and 62 deletions.
2 changes: 0 additions & 2 deletions okio/src/commonMain/kotlin/okio/-Platform.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ internal expect val PLATFORM_FILE_SYSTEM: FileSystem
@ExperimentalFileSystem
internal expect val PLATFORM_TEMPORARY_DIRECTORY: Path

internal expect val DIRECTORY_SEPARATOR: String

internal expect fun ByteArray.toUtf8String(): String

internal expect fun String.asUtf8ToByteArray(): ByteArray
Expand Down
2 changes: 1 addition & 1 deletion okio/src/commonMain/kotlin/okio/Path.kt
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ expect class Path internal constructor(slash: ByteString, bytes: ByteString) : C
override fun toString(): String

companion object {
val directorySeparator: String
val DIRECTORY_SEPARATOR: String

fun String.toPath(): Path

Expand Down
3 changes: 1 addition & 2 deletions okio/src/commonMain/kotlin/okio/internal/Path.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package okio.internal
import okio.Buffer
import okio.ByteString
import okio.ByteString.Companion.encodeUtf8
import okio.DIRECTORY_SEPARATOR
import okio.ExperimentalFileSystem
import okio.Path

Expand Down Expand Up @@ -194,7 +193,7 @@ internal fun Buffer.toPath(directorySeparator: ByteString? = null): Path {
// This path doesn't start with any slash. We must initialize the slash character to use.
val limit = indexOfElement(ANY_SLASH)
slash = slash ?: when (limit) {
-1L -> DIRECTORY_SEPARATOR.toSlash()
-1L -> Path.DIRECTORY_SEPARATOR.toSlash()
else -> get(limit).toSlash()
}
if (startsWithVolumeLetterAndColon(slash)) {
Expand Down
4 changes: 2 additions & 2 deletions okio/src/commonTest/kotlin/okio/AbstractFileSystemTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ abstract class AbstractFileSystemTest(
val cwd = fileSystem.canonicalize(".".toPath())
val cwdString = cwd.toString()
assertTrue(cwdString) {
cwdString.endsWith("okio${Path.directorySeparator}okio") ||
cwdString.endsWith("${Path.directorySeparator}okio-parent-okio-test") || // JS
cwdString.endsWith("okio${Path.DIRECTORY_SEPARATOR}okio") ||
cwdString.endsWith("${Path.DIRECTORY_SEPARATOR}okio-parent-okio-test") || // JS
cwdString.contains("/CoreSimulator/Devices/") || // iOS simulator.
cwdString == "/" // Android emulator.
}
Expand Down
2 changes: 1 addition & 1 deletion okio/src/commonTest/kotlin/okio/SystemFileSystemTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ import kotlin.time.ExperimentalTime
class SystemFileSystemTest : AbstractFileSystemTest(
clock = Clock.System,
fileSystem = FileSystem.SYSTEM,
windowsLimitations = DIRECTORY_SEPARATOR == "\\",
windowsLimitations = Path.DIRECTORY_SEPARATOR == "\\",
temporaryDirectory = FileSystem.SYSTEM_TEMPORARY_DIRECTORY
)
2 changes: 1 addition & 1 deletion okio/src/jsMain/kotlin/okio/-Platform.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ internal actual val PLATFORM_FILE_SYSTEM: FileSystem
internal actual val PLATFORM_TEMPORARY_DIRECTORY: Path
get() = tmpdir().toPath()

internal actual val DIRECTORY_SEPARATOR: String
internal actual val PLATFORM_DIRECTORY_SEPARATOR: String
get() {
// TODO(swankjesse): return path.path.sep instead, once it has @JsNonModule
return when (platform()) {
Expand Down
3 changes: 0 additions & 3 deletions okio/src/jvmMain/kotlin/okio/-Platform.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package okio

import okio.Path.Companion.toPath
import java.io.File

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

internal actual val DIRECTORY_SEPARATOR = File.separator

internal actual fun ByteArray.toUtf8String(): String = String(this, Charsets.UTF_8)

internal actual fun String.asUtf8ToByteArray(): ByteArray = toByteArray(Charsets.UTF_8)
Expand Down
2 changes: 2 additions & 0 deletions okio/src/jvmMain/kotlin/okio/JvmSystemFileSystem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package okio

import okio.Path.Companion.toOkioPath

/**
* A file system that adapts `java.io`.
*
Expand Down
23 changes: 22 additions & 1 deletion okio/src/jvmMain/kotlin/okio/Path.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ import okio.internal.commonResolve
import okio.internal.commonToPath
import okio.internal.commonToString
import okio.internal.commonVolumeLetter
import org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement
import java.io.File
import java.nio.file.Paths
import java.nio.file.Path as NioPath

@ExperimentalFileSystem
actual class Path internal actual constructor(
Expand Down Expand Up @@ -65,6 +69,11 @@ actual class Path internal actual constructor(
@JvmName("resolve")
actual operator fun div(child: Path): Path = commonResolve(child)

fun toFile(): File = File(toString())

@IgnoreJRERequirement // Can only be invoked on platforms that have java.nio.file.
fun toNioPath(): NioPath = Paths.get(toString())

actual override fun compareTo(other: Path): Int = commonCompareTo(other)

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

actual companion object {
actual val directorySeparator: String = DIRECTORY_SEPARATOR
/**
* Either `/` (on UNIX-like systems including Android, iOS, and Linux) or `\` (on Windows
* systems).
*/
@JvmField
actual val DIRECTORY_SEPARATOR: String = File.separator

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

@JvmName("get") @JvmStatic
actual fun String.toPath(directorySeparator: String?): Path = commonToPath(directorySeparator)

@JvmName("get") @JvmStatic
fun File.toOkioPath(): Path = toString().toPath()

@JvmName("get") @JvmStatic
@IgnoreJRERequirement // Can only be invoked on platforms that have java.nio.file.
fun NioPath.toOkioPath(): Path = toString().toPath()
}
}
37 changes: 0 additions & 37 deletions okio/src/jvmMain/kotlin/okio/Paths.kt

This file was deleted.

27 changes: 27 additions & 0 deletions okio/src/jvmTest/java/okio/FileSystemJavaTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
*/
package okio;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import okio.fakefilesystem.FakeFileSystem;
Expand All @@ -39,6 +41,31 @@ public void pathApi() {
assertThat(path.volumeLetter()).isNull();
}

@Test
public void directorySeparator() {
assertThat(Path.DIRECTORY_SEPARATOR).isIn("/", "\\");
}

/** Like the same test in JvmTest, but this is using the Java APIs. */
@Test
public void javaIoFileToOkioPath() {
String string = "/foo/bar/baz".replace("/", Path.DIRECTORY_SEPARATOR);
File javaIoFile = new File(string);
Path okioPath = Path.get(string);
assertThat(Path.get(javaIoFile)).isEqualTo(okioPath);
assertThat(okioPath.toFile()).isEqualTo(javaIoFile);
}

/** Like the same test in JvmTest, but this is using the Java APIs. */
@Test
public void nioPathToOkioPath() {
String string = "/foo/bar/baz".replace("/", okio.Path.DIRECTORY_SEPARATOR);
java.nio.file.Path nioPath = Paths.get(string);
Path okioPath = Path.get(string);
assertThat(Path.get(nioPath)).isEqualTo(okioPath);
assertThat((Object) okioPath.toNioPath()).isEqualTo(nioPath);
}

@Test
public void fileSystemApi() throws IOException {
assertThat(FileSystem.SYSTEM.metadata(FileSystem.SYSTEM_TEMPORARY_DIRECTORY)).isNotNull();
Expand Down
11 changes: 7 additions & 4 deletions okio/src/jvmTest/kotlin/okio/JvmTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package okio

import okio.Path.Companion.toOkioPath
import okio.Path.Companion.toPath
import org.assertj.core.api.Assertions.assertThat
import java.io.File
Expand All @@ -31,16 +32,18 @@ class JvmTest {

@Test
fun javaIoFileToOkioPath() {
val javaIoFile = File("/foo/bar/baz")
val okioPath = "/foo/bar/baz".toPath(Path.directorySeparator)
val string = "/foo/bar/baz".replace("/", Path.DIRECTORY_SEPARATOR)
val javaIoFile = File(string)
val okioPath = string.toPath()
assertThat(javaIoFile.toOkioPath()).isEqualTo(okioPath)
assertThat(okioPath.toFile()).isEqualTo(javaIoFile)
}

@Test
fun nioPathToOkioPath() {
val nioPath = Paths.get("/foo/bar/baz")
val okioPath = "/foo/bar/baz".toPath(Path.directorySeparator)
val string = "/foo/bar/baz".replace("/", Path.DIRECTORY_SEPARATOR)
val nioPath = Paths.get(string)
val okioPath = string.toPath()
assertThat(nioPath.toOkioPath()).isEqualTo(okioPath)
assertThat(okioPath.toNioPath() as Any).isEqualTo(nioPath)
}
Expand Down
2 changes: 1 addition & 1 deletion okio/src/mingwX64Main/kotlin/okio/posixVariant.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import platform.posix.rmdir
import platform.windows.MOVEFILE_REPLACE_EXISTING
import platform.windows.MoveFileExA

internal actual val VARIANT_DIRECTORY_SEPARATOR = "\\"
internal actual val PLATFORM_DIRECTORY_SEPARATOR = "\\"

@ExperimentalFileSystem
internal actual fun PosixFileSystem.variantDelete(path: Path) {
Expand Down
3 changes: 0 additions & 3 deletions okio/src/nativeMain/kotlin/okio/-Platform.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,3 @@ package okio
@ExperimentalFileSystem
internal actual val PLATFORM_FILE_SYSTEM: FileSystem
get() = PosixFileSystem

internal actual val DIRECTORY_SEPARATOR
get() = VARIANT_DIRECTORY_SEPARATOR
2 changes: 0 additions & 2 deletions okio/src/nativeMain/kotlin/okio/posixVariant.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*/
package okio

internal expect val VARIANT_DIRECTORY_SEPARATOR: String

@ExperimentalFileSystem
internal expect fun PosixFileSystem.variantDelete(path: Path)

Expand Down
2 changes: 2 additions & 0 deletions okio/src/nonJvmMain/kotlin/okio/-Platform.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package okio
import okio.internal.commonAsUtf8ToByteArray
import okio.internal.commonToUtf8String

internal expect val PLATFORM_DIRECTORY_SEPARATOR: String

internal actual fun ByteArray.toUtf8String(): String = commonToUtf8String()

internal actual fun String.asUtf8ToByteArray(): ByteArray = commonAsUtf8ToByteArray()
Expand Down
2 changes: 1 addition & 1 deletion okio/src/nonJvmMain/kotlin/okio/Path.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ actual class Path internal actual constructor(
actual override fun toString() = commonToString()

actual companion object {
actual val directorySeparator: String = DIRECTORY_SEPARATOR
actual val DIRECTORY_SEPARATOR: String = PLATFORM_DIRECTORY_SEPARATOR

actual fun String.toPath(): Path = commonToPath()

Expand Down
2 changes: 1 addition & 1 deletion okio/src/unixMain/kotlin/okio/posixVariant.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import platform.posix.remove
import platform.posix.rename
import platform.posix.timespec

internal actual val VARIANT_DIRECTORY_SEPARATOR = "/"
internal actual val PLATFORM_DIRECTORY_SEPARATOR = "/"

@ExperimentalFileSystem
internal actual fun PosixFileSystem.variantDelete(path: Path) {
Expand Down

0 comments on commit 1977d3b

Please sign in to comment.