Skip to content

Commit

Permalink
Merge pull request square#863 from square/jwilson.1230.one_platform_file
Browse files Browse the repository at this point in the history
Combine Platform.kt and -Platform.kt
  • Loading branch information
swankjesse authored Dec 31, 2020
2 parents d9d97ad + c8e75f5 commit 7027351
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 118 deletions.
8 changes: 8 additions & 0 deletions okio/src/commonMain/kotlin/okio/-Platform.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@

package okio

@ExperimentalFilesystem
internal expect val PLATFORM_FILESYSTEM: 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
42 changes: 42 additions & 0 deletions okio/src/commonMain/kotlin/okio/-Util.kt
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,45 @@ internal fun Long.toHexString(): String {

return String(result, i, result.size - i)
}

internal inline fun <S : Source, T> S.use(block: (S) -> T): T {
var result: T? = null
var thrown: Throwable? = null

try {
result = block(this)
} catch (t: Throwable) {
thrown = t
}

try {
close()
} catch (t: Throwable) {
if (thrown == null) thrown = t
else thrown.addSuppressed(t)
}

if (thrown != null) throw thrown
return result!!
}

internal inline fun <S : Sink, T> S.use(block: (S) -> T): T {
var result: T? = null
var thrown: Throwable? = null

try {
result = block(this)
} catch (t: Throwable) {
thrown = t
}

try {
close()
} catch (t: Throwable) {
if (thrown == null) thrown = t
else thrown.addSuppressed(t)
}

if (thrown != null) throw thrown
return result!!
}
24 changes: 0 additions & 24 deletions okio/src/commonMain/kotlin/okio/Platform.kt

This file was deleted.

58 changes: 0 additions & 58 deletions okio/src/commonMain/kotlin/okio/common.kt

This file was deleted.

File renamed without changes.
20 changes: 20 additions & 0 deletions okio/src/jvmMain/kotlin/okio/-Platform.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,26 @@
@file:JvmName("-Platform")
package okio

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

@ExperimentalFilesystem
internal actual val PLATFORM_FILESYSTEM: Filesystem
get() {
try {
Class.forName("java.nio.file.Files")
return NioSystemFilesystem()
} catch (e: ClassNotFoundException) {
return JvmSystemFilesystem()
}
}

@ExperimentalFilesystem
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
36 changes: 0 additions & 36 deletions okio/src/jvmMain/kotlin/okio/Platform.kt

This file was deleted.

File renamed without changes.
File renamed without changes.

0 comments on commit 7027351

Please sign in to comment.