Is there a multiplatform way in Kotlin to figure o...
# multiplatform
s
Is there a multiplatform way in Kotlin to figure out the endianness of bytes? I have to know to reverse the order if ncessary.
i
you mean endianness of the host system?
s
Yep, that's what I meant.
i
seems an approach like this would be sufficient https://stackoverflow.com/a/3877387/1286667
s
I guess it would work. Too bad it isn't built-in like in C#. Thank you Ian!
i
I feel like I remember seeing this talked about so it may exist
s
Mmm, maybe I could try it. How do I use ktor? I need to use a specific plugin in build.gradle I imagine?
i
no extra plugin it's just a dependency I think all you really need is the io library rather than full ktor https://github.com/Kotlin/kotlinx-io that has buffers and functions you'll probably want in regards to dealing with bytes
s
So I would only have to add something like this in my build.gradle?
implementation kotlin('kotlinx-io')
i
yeah you should be able to add that to your commonMain sourceset and then assuming you have enabled metadata it should pick up the platform specific dependencies automagically
s
You mean this in settings.gradle?
enableFeaturePreview('GRADLE_METADATA')
i
aye, that's the one
r
Authors say kotlinx-io is quite unstable for general public use, so take it at your own risk. Maybe for the task of checking host endianness it's simpler to have own implementations for each platform? You can probably copy them from the library if you found it there already.
s
Mmm. I think I'll use the workaround suggested by @ian.shaun.thomas (stackoverflow link) and go from there. It seems simple enough to implement and I only need to have the info at runtime anyway. Thanks for the input Andrew.
r
Oh, yeah, I've opened the link. Sure, that's the way to go :D
s
Well, the example is simple when written in C/C++. Not that simple when you're not very familiar with the bytes/bits manipulation functions in Kotlin, he he, 🙄
r
Yeah, I forgot that reinterpretation from integer to byte array is platform-specific in Kotlin. Here are relevant parts of kotlinx-io, which you can copy: common simply has global variable: https://github.com/Kotlin/kotlinx-io/blob/master/core/commonMain/src/kotlinx/io/ByteOrder.kt jvm uses java.nio functionality: https://github.com/Kotlin/kotlinx-io/blob/master/core/jvmMain/src/kotlinx/io/JvmByteOrder.kt native does reinterpretation and checks manually: https://github.com/Kotlin/kotlinx-io/blob/master/core/nativeMain/src/kotlinx/io/NativeByteOrder.kt js uses it's own arrays which also allow to view memory block as different primitive arrays: https://github.com/Kotlin/kotlinx-io/blob/master/core/jsMain/src/kotlinx/io/JsByteOrder.kt
s
Well, in that case, I might try to use the library as is, even if it's experimental (so maybe not very stable). However, after adding
implementation kotlin('kotlinx-io')
to my gradle file, I got this error when rebuilding my root project:
Copy code
Could not resolve all files for configuration ':jvmCompileClasspath'.
 > Could not find org.jetbrains.kotlin:kotlin-kotlinx-io:1.3.60.
Here is my whole build.gradle file. The only line that I added is line 23.
r
I'm actually not sure there are any publicly available artifacts for it.
s
I'm not sure I understand what you're saying. 🤔
r
I mean, there is high chance that it's not even compiled and published to use from other projects in any public repository yet.
👍 1
d
org.jetbrains.kotlinkotlin kotlinx io1.3.60 This is an incorrect reference. Please google the library and use the correct groupId & artifactId.
s
So if I understand well, simply adding
implementation kotlin('kotlinx-io')
won't cut it because it seems that under the hood it appends the same version to this library as the one for the 'kotlin multiplatform plugin' (which is 1.3.60 in my case)?
Well, I googled it and tried a few things with
org.jetbrains.kotlinx.kotlinx-io
and
version '0.1.16'
but it doesn't seem to work. Does someone know the correct groupId & artifactId for kotlinx-io? Also, I'm not sure if I have to 1. declare a
plugin
for this specific library 2. add a specific
classpath
for this library 3. use
implementation
for this library 4. use
compile
for this library or a subset of these options.
d
The notation is
groupId:artifactId:version
. Once you use the notation correctly, take a look at the page where you found the 3 values to see on what maven repository it is hosted. Make sure the repository URL is known by your build by registering it under
repositories {}
You should use
api
instead of
implementation
if you're writing a library and consumers of its api should have the dependency too.
Although I personally still use the old
compile
instead which is the predecessor to
api