From 52157ee0e7f3d4dca66a32a71c3d597dac5c881a Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 24 Jul 2024 00:34:49 +0600 Subject: [PATCH] start moving to kotlinx-io --- common/build.gradle | 1 + .../dev/inmo/micro_utils/common/MPPFile.kt | 35 ++++++++++++++++--- gradle/libs.versions.toml | 3 ++ 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/common/build.gradle b/common/build.gradle index 01489bbf4f1..d4fc46ec83d 100644 --- a/common/build.gradle +++ b/common/build.gradle @@ -11,6 +11,7 @@ kotlin { commonMain { dependencies { api libs.klock + api libs.kt.io } } jvmMain { diff --git a/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/MPPFile.kt b/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/MPPFile.kt index 7aef8ed8c9a..fd397e0d811 100644 --- a/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/MPPFile.kt +++ b/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/MPPFile.kt @@ -1,5 +1,11 @@ package dev.inmo.micro_utils.common +import kotlinx.io.Buffer +import kotlinx.io.Source +import kotlinx.io.buffered +import kotlinx.io.files.Path +import kotlinx.io.files.SystemFileSystem +import kotlinx.io.readByteArray import kotlinx.serialization.Serializable import kotlin.jvm.JvmInline @@ -23,12 +29,31 @@ value class FileName(val string: String) { } -expect class MPPFile +typealias MPPFile = Path -expect val MPPFile.filename: FileName -expect val MPPFile.filesize: Long -expect val MPPFile.bytesAllocatorSync: ByteArrayAllocator -expect val MPPFile.bytesAllocator: SuspendByteArrayAllocator +val MPPFile.filename: FileName + get() = FileName(name) +val MPPFile.filesize: Long + get() = SystemFileSystem.metadataOrNull(this) ?.size ?.takeIf { it > -1 } ?: error("Path $filename does not exists or is folder") +val MPPFile.bytesAllocatorSync: ByteArrayAllocator + get() = { + source().readByteArray() + } +val MPPFile.bytesAllocator: SuspendByteArrayAllocator + get() = { + bytesAllocatorSync() + } fun MPPFile.bytesSync() = bytesAllocatorSync() suspend fun MPPFile.bytes() = bytesAllocator() +fun MPPFile.source(): Source = SystemFileSystem.source(this).buffered() + + +//expect class MPPFile +// +//expect val MPPFile.filename: FileName +//expect val MPPFile.filesize: Long +//expect val MPPFile.bytesAllocatorSync: ByteArrayAllocator +//expect val MPPFile.bytesAllocator: SuspendByteArrayAllocator +//fun MPPFile.bytesSync() = bytesAllocatorSync() +//suspend fun MPPFile.bytes() = bytesAllocator() diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 84313b0b175..e68dec8a2d6 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -4,6 +4,8 @@ kt = "1.9.23" kt-serialization = "1.6.3" kt-coroutines = "1.8.1" +kt-io = "0.5.1" + kslog = "1.3.4" jb-compose = "1.6.2" @@ -53,6 +55,7 @@ kt-coroutines = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", vers kt-coroutines-android = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-android", version.ref = "kt-coroutines" } kt-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "kt-coroutines" } +kt-io = { module = "org.jetbrains.kotlinx:kotlinx-io-core", version.ref = "kt-io" } ktor-io = { module = "io.ktor:ktor-io", version.ref = "ktor" } ktor-serialization-kotlinx-json = { module = "io.ktor:ktor-serialization-kotlinx-json", version.ref = "ktor" }