diff --git a/common/build.gradle b/common/build.gradle index 840e2fb8a68..852eddb17fe 100644 --- a/common/build.gradle +++ b/common/build.gradle @@ -11,6 +11,7 @@ kotlin { commonMain { dependencies { api libs.klock + api libs.kt.io.core } } jvmMain { @@ -23,7 +24,6 @@ kotlin { api project(":micro_utils.coroutines") api libs.android.fragment } - dependsOn jvmMain } linuxX64Main { 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..b47b3530b93 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,9 @@ package dev.inmo.micro_utils.common +import kotlinx.io.RawSource +import kotlinx.io.Source +import kotlinx.io.buffered +import kotlinx.io.readByteArray import kotlinx.serialization.Serializable import kotlin.jvm.JvmInline @@ -23,12 +27,21 @@ value class FileName(val string: String) { } -expect class MPPFile +expect class MPPFile = File +expect val MPPFile.rawSource: RawSource +val MPPFile.source: Source + get() = rawSource.buffered() expect val MPPFile.filename: FileName expect val MPPFile.filesize: Long -expect val MPPFile.bytesAllocatorSync: ByteArrayAllocator -expect val MPPFile.bytesAllocator: SuspendByteArrayAllocator +val MPPFile.bytesAllocatorSync: ByteArrayAllocator + get() = { + source.readByteArray() + } +val MPPFile.bytesAllocator: SuspendByteArrayAllocator + get() = { + source.readByteArray() + } fun MPPFile.bytesSync() = bytesAllocatorSync() suspend fun MPPFile.bytes() = bytesAllocator() diff --git a/common/src/jsMain/kotlin/dev/inmo/micro_utils/common/JSMPPFile.kt b/common/src/jsMain/kotlin/dev/inmo/micro_utils/common/JSMPPFile.kt index 7e3d865bfb6..2a7ee1fe8e8 100644 --- a/common/src/jsMain/kotlin/dev/inmo/micro_utils/common/JSMPPFile.kt +++ b/common/src/jsMain/kotlin/dev/inmo/micro_utils/common/JSMPPFile.kt @@ -1,5 +1,6 @@ package dev.inmo.micro_utils.common +import kotlinx.io.RawSource import org.khronos.webgl.ArrayBuffer import org.w3c.dom.ErrorEvent import org.w3c.files.* @@ -40,15 +41,6 @@ actual val MPPFile.filename: FileName */ actual val MPPFile.filesize: Long get() = size.toLong() -/** - * @suppress - */ -@Warning("That is not optimized version of bytes allocator. Use asyncBytesAllocator everywhere you can") -actual val MPPFile.bytesAllocatorSync: ByteArrayAllocator - get() = ::readBytes -/** - * @suppress - */ -@Warning("That is not optimized version of bytes allocator. Use asyncBytesAllocator everywhere you can") -actual val MPPFile.bytesAllocator: SuspendByteArrayAllocator - get() = ::dirtyReadBytes + +actual val MPPFile.rawSource: RawSource + get() = diff --git a/common/src/jsMain/kotlin/dev/inmo/micro_utils/common/MPPFile.dev.inmo.micro_utils.micro_utils.common.js.kt b/common/src/jsMain/kotlin/dev/inmo/micro_utils/common/MPPFile.dev.inmo.micro_utils.micro_utils.common.js.kt new file mode 100644 index 00000000000..303d0d32620 --- /dev/null +++ b/common/src/jsMain/kotlin/dev/inmo/micro_utils/common/MPPFile.dev.inmo.micro_utils.micro_utils.common.js.kt @@ -0,0 +1,3 @@ +package dev.inmo.micro_utils.common + +import kotlinx.io.RawSource \ No newline at end of file diff --git a/common/src/jvmMain/kotlin/dev/inmo/micro_utils/common/JVMMPPFile.kt b/common/src/jvmMain/kotlin/dev/inmo/micro_utils/common/JVMMPPFile.kt index e245fe93836..01c49a0c6a0 100644 --- a/common/src/jvmMain/kotlin/dev/inmo/micro_utils/common/JVMMPPFile.kt +++ b/common/src/jvmMain/kotlin/dev/inmo/micro_utils/common/JVMMPPFile.kt @@ -18,20 +18,4 @@ actual val MPPFile.filename: FileName * @suppress */ actual val MPPFile.filesize: Long - get() = length() -/** - * @suppress - */ -actual val MPPFile.bytesAllocatorSync: ByteArrayAllocator - get() = ::readBytes -/** - * @suppress - */ -actual val MPPFile.bytesAllocator: SuspendByteArrayAllocator - get() = { - doInIO { - doOutsideOfCoroutine { - readBytes() - } - } - } + get() = if (exists()) length() else -1L diff --git a/common/src/jvmMain/kotlin/dev/inmo/micro_utils/common/MPPFile.dev.inmo.micro_utils.micro_utils.common.jvm.kt b/common/src/jvmMain/kotlin/dev/inmo/micro_utils/common/MPPFile.dev.inmo.micro_utils.micro_utils.common.jvm.kt new file mode 100644 index 00000000000..cc5eaf38794 --- /dev/null +++ b/common/src/jvmMain/kotlin/dev/inmo/micro_utils/common/MPPFile.dev.inmo.micro_utils.micro_utils.common.jvm.kt @@ -0,0 +1,6 @@ +package dev.inmo.micro_utils.common + +import kotlinx.io.RawSource + +actual val MPPFile.rawSource: RawSource + get() = TODO("Not yet implemented") \ No newline at end of file diff --git a/common/src/linuxArm64Main/kotlin/ActualMPPFile.kt b/common/src/linuxArm64Main/kotlin/ActualMPPFile.kt deleted file mode 100644 index ff5f9619c53..00000000000 --- a/common/src/linuxArm64Main/kotlin/ActualMPPFile.kt +++ /dev/null @@ -1,36 +0,0 @@ -package dev.inmo.micro_utils.common - -import okio.FileSystem -import okio.Path -import okio.use - -actual typealias MPPFile = Path - -/** - * @suppress - */ -actual val MPPFile.filename: FileName - get() = FileName(toString()) -/** - * @suppress - */ -actual val MPPFile.filesize: Long - get() = FileSystem.SYSTEM.openReadOnly(this).use { - it.size() - } -/** - * @suppress - */ -actual val MPPFile.bytesAllocatorSync: ByteArrayAllocator - get() = { - FileSystem.SYSTEM.read(this) { - readByteArray() - } - } -/** - * @suppress - */ -actual val MPPFile.bytesAllocator: SuspendByteArrayAllocator - get() = { - bytesAllocatorSync() - } diff --git a/common/src/linuxX64Main/kotlin/ActualMPPFile.kt b/common/src/linuxX64Main/kotlin/ActualMPPFile.kt deleted file mode 100644 index ff5f9619c53..00000000000 --- a/common/src/linuxX64Main/kotlin/ActualMPPFile.kt +++ /dev/null @@ -1,36 +0,0 @@ -package dev.inmo.micro_utils.common - -import okio.FileSystem -import okio.Path -import okio.use - -actual typealias MPPFile = Path - -/** - * @suppress - */ -actual val MPPFile.filename: FileName - get() = FileName(toString()) -/** - * @suppress - */ -actual val MPPFile.filesize: Long - get() = FileSystem.SYSTEM.openReadOnly(this).use { - it.size() - } -/** - * @suppress - */ -actual val MPPFile.bytesAllocatorSync: ByteArrayAllocator - get() = { - FileSystem.SYSTEM.read(this) { - readByteArray() - } - } -/** - * @suppress - */ -actual val MPPFile.bytesAllocator: SuspendByteArrayAllocator - get() = { - bytesAllocatorSync() - } diff --git a/common/src/linuxX64Main/kotlin/fixed.kt b/common/src/linuxX64Main/kotlin/fixed.kt deleted file mode 100644 index 14e0a168cba..00000000000 --- a/common/src/linuxX64Main/kotlin/fixed.kt +++ /dev/null @@ -1,25 +0,0 @@ -package dev.inmo.micro_utils.common - -import kotlinx.cinterop.* -import platform.posix.snprintf -import platform.posix.sprintf - -@OptIn(ExperimentalForeignApi::class) -actual fun Float.fixed(signs: Int): Float { - return memScoped { - val buff = allocArray(Float.SIZE_BYTES * 2) - - sprintf(buff, "%.${signs}f", this@fixed) - buff.toKString().toFloat() - } -} - -@OptIn(ExperimentalForeignApi::class) -actual fun Double.fixed(signs: Int): Double { - return memScoped { - val buff = allocArray(Double.SIZE_BYTES * 2) - - sprintf(buff, "%.${signs}f", this@fixed) - buff.toKString().toDouble() - } -} diff --git a/common/src/mingwX64Main/kotlin/ActualMPPFile.kt b/common/src/mingwX64Main/kotlin/ActualMPPFile.kt deleted file mode 100644 index ff5f9619c53..00000000000 --- a/common/src/mingwX64Main/kotlin/ActualMPPFile.kt +++ /dev/null @@ -1,36 +0,0 @@ -package dev.inmo.micro_utils.common - -import okio.FileSystem -import okio.Path -import okio.use - -actual typealias MPPFile = Path - -/** - * @suppress - */ -actual val MPPFile.filename: FileName - get() = FileName(toString()) -/** - * @suppress - */ -actual val MPPFile.filesize: Long - get() = FileSystem.SYSTEM.openReadOnly(this).use { - it.size() - } -/** - * @suppress - */ -actual val MPPFile.bytesAllocatorSync: ByteArrayAllocator - get() = { - FileSystem.SYSTEM.read(this) { - readByteArray() - } - } -/** - * @suppress - */ -actual val MPPFile.bytesAllocator: SuspendByteArrayAllocator - get() = { - bytesAllocatorSync() - } diff --git a/common/src/mingwX64Main/kotlin/fixed.kt b/common/src/mingwX64Main/kotlin/fixed.kt deleted file mode 100644 index 14e0a168cba..00000000000 --- a/common/src/mingwX64Main/kotlin/fixed.kt +++ /dev/null @@ -1,25 +0,0 @@ -package dev.inmo.micro_utils.common - -import kotlinx.cinterop.* -import platform.posix.snprintf -import platform.posix.sprintf - -@OptIn(ExperimentalForeignApi::class) -actual fun Float.fixed(signs: Int): Float { - return memScoped { - val buff = allocArray(Float.SIZE_BYTES * 2) - - sprintf(buff, "%.${signs}f", this@fixed) - buff.toKString().toFloat() - } -} - -@OptIn(ExperimentalForeignApi::class) -actual fun Double.fixed(signs: Int): Double { - return memScoped { - val buff = allocArray(Double.SIZE_BYTES * 2) - - sprintf(buff, "%.${signs}f", this@fixed) - buff.toKString().toDouble() - } -} diff --git a/common/src/nativeMain/kotlin/ActualMPPFile.kt b/common/src/nativeMain/kotlin/ActualMPPFile.kt new file mode 100644 index 00000000000..da4975411a2 --- /dev/null +++ b/common/src/nativeMain/kotlin/ActualMPPFile.kt @@ -0,0 +1,22 @@ +package dev.inmo.micro_utils.common + +import kotlinx.io.RawSource +import kotlinx.io.buffered +import kotlinx.io.files.FileSystem +import kotlinx.io.files.Path +import kotlinx.io.files.SystemFileSystem + +actual typealias MPPFile = Path + +actual val MPPFile.rawSource: RawSource + get() = SystemFileSystem.source(this) +/** + * @suppress + */ +actual val MPPFile.filename: FileName + get() = FileName(toString()) +/** + * @suppress + */ +actual val MPPFile.filesize: Long + get() = SystemFileSystem.metadataOrNull(this) ?.size ?: -1L \ No newline at end of file diff --git a/common/src/linuxArm64Main/kotlin/fixed.kt b/common/src/nativeMain/kotlin/fixed.kt similarity index 100% rename from common/src/linuxArm64Main/kotlin/fixed.kt rename to common/src/nativeMain/kotlin/fixed.kt diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index edf7baf31e0..136a8981b52 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -3,6 +3,7 @@ kt = "1.9.23" kt-serialization = "1.6.3" kt-coroutines = "1.8.0" +kt-io = "0.3.3" kslog = "1.3.3" @@ -53,6 +54,8 @@ 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-core = { 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" }