mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-06-01 23:45:28 +00:00
start moving onto kotlinx io
This commit is contained in:
parent
fbb999ccf3
commit
a7e54e2a38
|
@ -11,6 +11,7 @@ kotlin {
|
||||||
commonMain {
|
commonMain {
|
||||||
dependencies {
|
dependencies {
|
||||||
api libs.klock
|
api libs.klock
|
||||||
|
api libs.kt.io.core
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
jvmMain {
|
jvmMain {
|
||||||
|
@ -23,7 +24,6 @@ kotlin {
|
||||||
api project(":micro_utils.coroutines")
|
api project(":micro_utils.coroutines")
|
||||||
api libs.android.fragment
|
api libs.android.fragment
|
||||||
}
|
}
|
||||||
dependsOn jvmMain
|
|
||||||
}
|
}
|
||||||
|
|
||||||
linuxX64Main {
|
linuxX64Main {
|
||||||
|
|
|
@ -1,5 +1,9 @@
|
||||||
package dev.inmo.micro_utils.common
|
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 kotlinx.serialization.Serializable
|
||||||
import kotlin.jvm.JvmInline
|
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.filename: FileName
|
||||||
expect val MPPFile.filesize: Long
|
expect val MPPFile.filesize: Long
|
||||||
expect val MPPFile.bytesAllocatorSync: ByteArrayAllocator
|
val MPPFile.bytesAllocatorSync: ByteArrayAllocator
|
||||||
expect val MPPFile.bytesAllocator: SuspendByteArrayAllocator
|
get() = {
|
||||||
|
source.readByteArray()
|
||||||
|
}
|
||||||
|
val MPPFile.bytesAllocator: SuspendByteArrayAllocator
|
||||||
|
get() = {
|
||||||
|
source.readByteArray()
|
||||||
|
}
|
||||||
fun MPPFile.bytesSync() = bytesAllocatorSync()
|
fun MPPFile.bytesSync() = bytesAllocatorSync()
|
||||||
suspend fun MPPFile.bytes() = bytesAllocator()
|
suspend fun MPPFile.bytes() = bytesAllocator()
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package dev.inmo.micro_utils.common
|
package dev.inmo.micro_utils.common
|
||||||
|
|
||||||
|
import kotlinx.io.RawSource
|
||||||
import org.khronos.webgl.ArrayBuffer
|
import org.khronos.webgl.ArrayBuffer
|
||||||
import org.w3c.dom.ErrorEvent
|
import org.w3c.dom.ErrorEvent
|
||||||
import org.w3c.files.*
|
import org.w3c.files.*
|
||||||
|
@ -40,15 +41,6 @@ actual val MPPFile.filename: FileName
|
||||||
*/
|
*/
|
||||||
actual val MPPFile.filesize: Long
|
actual val MPPFile.filesize: Long
|
||||||
get() = size.toLong()
|
get() = size.toLong()
|
||||||
/**
|
|
||||||
* @suppress
|
actual val MPPFile.rawSource: RawSource
|
||||||
*/
|
get() =
|
||||||
@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
|
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
package dev.inmo.micro_utils.common
|
||||||
|
|
||||||
|
import kotlinx.io.RawSource
|
|
@ -18,20 +18,4 @@ actual val MPPFile.filename: FileName
|
||||||
* @suppress
|
* @suppress
|
||||||
*/
|
*/
|
||||||
actual val MPPFile.filesize: Long
|
actual val MPPFile.filesize: Long
|
||||||
get() = length()
|
get() = if (exists()) length() else -1L
|
||||||
/**
|
|
||||||
* @suppress
|
|
||||||
*/
|
|
||||||
actual val MPPFile.bytesAllocatorSync: ByteArrayAllocator
|
|
||||||
get() = ::readBytes
|
|
||||||
/**
|
|
||||||
* @suppress
|
|
||||||
*/
|
|
||||||
actual val MPPFile.bytesAllocator: SuspendByteArrayAllocator
|
|
||||||
get() = {
|
|
||||||
doInIO {
|
|
||||||
doOutsideOfCoroutine {
|
|
||||||
readBytes()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
package dev.inmo.micro_utils.common
|
||||||
|
|
||||||
|
import kotlinx.io.RawSource
|
||||||
|
|
||||||
|
actual val MPPFile.rawSource: RawSource
|
||||||
|
get() = TODO("Not yet implemented")
|
|
@ -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()
|
|
||||||
}
|
|
|
@ -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()
|
|
||||||
}
|
|
|
@ -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<ByteVar>(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<ByteVar>(Double.SIZE_BYTES * 2)
|
|
||||||
|
|
||||||
sprintf(buff, "%.${signs}f", this@fixed)
|
|
||||||
buff.toKString().toDouble()
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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()
|
|
||||||
}
|
|
|
@ -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<ByteVar>(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<ByteVar>(Double.SIZE_BYTES * 2)
|
|
||||||
|
|
||||||
sprintf(buff, "%.${signs}f", this@fixed)
|
|
||||||
buff.toKString().toDouble()
|
|
||||||
}
|
|
||||||
}
|
|
22
common/src/nativeMain/kotlin/ActualMPPFile.kt
Normal file
22
common/src/nativeMain/kotlin/ActualMPPFile.kt
Normal file
|
@ -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
|
|
@ -3,6 +3,7 @@
|
||||||
kt = "1.9.23"
|
kt = "1.9.23"
|
||||||
kt-serialization = "1.6.3"
|
kt-serialization = "1.6.3"
|
||||||
kt-coroutines = "1.8.0"
|
kt-coroutines = "1.8.0"
|
||||||
|
kt-io = "0.3.3"
|
||||||
|
|
||||||
kslog = "1.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-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-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-io = { module = "io.ktor:ktor-io", version.ref = "ktor" }
|
||||||
ktor-serialization-kotlinx-json = { module = "io.ktor:ktor-serialization-kotlinx-json", version.ref = "ktor" }
|
ktor-serialization-kotlinx-json = { module = "io.ktor:ktor-serialization-kotlinx-json", version.ref = "ktor" }
|
||||||
|
|
Loading…
Reference in New Issue
Block a user