This library mimics Dart's ByteData
utility functions and add the following extension
functions:
ByteArray.getByte(byteOffset, value)
ByteArray.setByte(byteOffset, value)
ByteArray.getUByte(byteOffset, value)
ByteArray.setUByte(byteOffset, value)
ByteArray.getShort(byteOffset, value, endian)
ByteArray.setShort(byteOffset, value, endian)
ByteArray.getUShort(byteOffset, value, endian)
ByteArray.setUShort(byteOffset, value, endian)
ByteArray.getInt(byteOffset, value, endian)
ByteArray.setInt(byteOffset, value, endian)
ByteArray.getUInt(byteOffset, value, endian)
ByteArray.setUInt(byteOffset, value, endian)
ByteArray.getLong(byteOffset, value, endian)
ByteArray.setLong(byteOffset, value, endian)
ByteArray.getULong(byteOffset, value, endian)
ByteArray.setULong(byteOffset, value, endian)
The endian
value always defaults to Endian.big
. Supports both Endian.Little
and Endian.Big
.
The library is in Maven Central. The usage is very simple, just add this to your build.gradle
:
repositories {
mavenCentral()
}
dependencies {
implementation("com.github.mvysny.kotlin-unsigned-jvm:kotlin-unsigned-jvm:0.1")
}
Why yet another library when you can use DataInputStream.read*()
and DataOutputStream.write*()
functions?
Two reasons:
Data*Stream
do not support unsigned Kotlin types directly;Data*Stream
always useEndian.Big
, it's not possible to configure them to write inEndian.Little
.
What about Kotlin's built-in ByteArray.setUIntAt()?
- It's only for Kotlin/Native, it's not available in Kotlin/JVM
- The endianness is undocumented, which is a big issue. Could be big, little, or platform-specific.
Why not using Java built-in ByteBuffer
as follows:
ByteBuffer buffer = ByteBuffer.wrap(new byte[10]);
buffer.order(ByteOrder.LITTLE_ENDIAN);
buffer.getLong().toULong();
Yeah.... that also works. Yet, I'd argue that working with ByteArray
and indexes is simpler than with a ByteBuffer
with a pointer.
To release the library to Maven Central:
- Edit
build.gradle.kts
and remove-SNAPSHOT
in theversion=
stanza - Commit with the commit message of simply being the version being released, e.g. "1.2.13"
- git tag the commit with the same tag name as the commit message above, e.g.
1.2.13
git push
,git push --tags
- Run
./gradlew clean build publish
- Continue to the OSSRH Nexus and follow the release procedure.
- Add the
-SNAPSHOT
back to theversion=
while increasing the version to something which will be released in the future, e.g. 1.2.14, then commit with the commit message "1.2.14-SNAPSHOT" and push.
See LICENSE