Compare commits

...

47 Commits

Author SHA1 Message Date
3127b6c066 Update README.md 2026-04-16 16:10:52 +00:00
e8baac9ba3 Bump version to 1.4.2
All checks were successful
/ test (pull_request) Successful in 27s
2026-03-11 23:17:32 +01:00
d3a67c622c Fix default parameter 2026-03-11 23:12:41 +01:00
c80040466b Bump version to 1.4.1
All checks were successful
/ test (pull_request) Successful in 2m17s
2026-03-11 22:16:30 +01:00
20c2ba4c24 Improve shorthand bytearray operator functions 2026-03-11 22:15:57 +01:00
47666a1e1a Merge pull request '1.4.0 release' (#10) from develop into main
All checks were successful
/ publish (push) Successful in 3m19s
Reviewed-on: Infendro/bytes-kt#10
2026-02-13 23:08:05 +00:00
0129039df9 improve test coverage
All checks were successful
/ test (pull_request) Successful in 2m27s
2026-02-14 00:05:06 +01:00
3801c5759f bump version to 1.4.0
All checks were successful
/ test (pull_request) Successful in 41s
2026-02-05 14:29:19 +01:00
03ec68cc40 remove unsigned support 2026-02-05 14:19:51 +01:00
421beeed1e Merge pull request '1.3.4 release' (#9) from develop into main
All checks were successful
/ publish (push) Successful in 2m48s
Reviewed-on: Infendro/bytes-kt#9
2026-02-03 22:06:17 +00:00
19b2fb36fd bump version to 1.3.4
All checks were successful
/ test (pull_request) Successful in 1m44s
2026-02-03 23:02:24 +01:00
f18adc08b9 implement shorthand bytearray operations 2026-02-03 23:01:19 +01:00
85f10be1d2 Merge pull request '1.3.3 release' (#8) from develop into main
All checks were successful
/ publish (push) Successful in 1m22s
Reviewed-on: Infendro/bytes-kt#8
2026-01-22 10:59:48 +00:00
345be1cc6f bump version to 1.3.3
All checks were successful
/ test (pull_request) Successful in 28s
2026-01-22 11:58:36 +01:00
d7d4e0c635 implement bytearray operators with byte 2026-01-22 11:35:09 +01:00
3e769fe4d0 small refactor to ci caching 2026-01-17 12:38:20 +01:00
17ff49ce0c Merge pull request '1.3.2 release' (#7) from develop into main
All checks were successful
/ publish (push) Successful in 3m26s
Reviewed-on: Infendro/bytes-kt#7
2026-01-15 15:23:32 +00:00
d7e65ccc32 fix ByteArray to numeric conversion
All checks were successful
/ test (pull_request) Successful in 2m49s
2026-01-15 16:18:44 +01:00
dddf2bab93 add return types 2026-01-15 14:56:46 +01:00
d55ec62be9 simplify test workflow to JVM only 2026-01-15 14:45:31 +01:00
9c7a9e4926 Merge pull request '1.3.1 release' (#6) from develop into main
All checks were successful
/ publish (push) Successful in 2m14s
Reviewed-on: Infendro/bytes-kt#6
2026-01-15 13:18:42 +00:00
ff0e17b5ad switch to signed-first approach
All checks were successful
/ test (pull_request) Successful in 2m47s
It turns out signed math is more performant
2026-01-15 14:10:14 +01:00
d06e523914 fix REAMDE 2026-01-14 16:18:57 +01:00
afe35355a8 Merge pull request 'optimizations' (#5) from develop into main
All checks were successful
/ publish (push) Successful in 1m29s
Reviewed-on: Infendro/bytes-kt#5
2026-01-14 15:12:42 +00:00
947ef907a5 more optimizations, split ci
All checks were successful
/ test (pull_request) Successful in 6m14s
2026-01-14 16:03:16 +01:00
23b565ca0a upgrade
Some checks failed
/ test (push) Failing after 2m16s
/ publish (push) Has been skipped
2026-01-14 15:47:39 +01:00
cc9961084c implement optimizations 2026-01-14 15:36:50 +01:00
9391009403 improve ci
All checks were successful
/ test (push) Successful in 3m25s
/ publish (push) Has been skipped
2026-01-13 15:58:00 +01:00
1195ad5acc replace IllegalArgumentException with require 2025-12-14 00:12:03 +01:00
9b7335ead2 update README to match wiki 2025-12-13 15:23:18 +01:00
f5d559e8fe refactor lib structure 2025-12-13 14:49:00 +01:00
3ab2bea4f6 improve test coverage 2025-12-13 11:35:11 +01:00
a9f77d6211 fix workflow 2025-12-13 01:30:31 +01:00
ecbeab8531 small refactor 2025-12-13 01:09:41 +01:00
b8639c7cf0 refactor build.gradle.kts 2025-12-13 00:59:15 +01:00
300ad7511a use common plugin 2025-12-13 00:54:00 +01:00
22cfdd90ab improve test coverage 2025-12-12 21:21:44 +01:00
01327ef4b2 refactor 2025-12-12 21:20:44 +01:00
be9a8a4369 bump version to 1.2.4
All checks were successful
/ publish (push) Successful in 4m57s
2025-12-12 15:47:47 +01:00
96d2c5785f implement String utilities
UByteArray::decodeToString(): String
String::encodeToUByteArray(): UByteArray
2025-12-12 15:44:39 +01:00
1e04a3aff4 bump version to 1.2.3
All checks were successful
/ publish (push) Successful in 4m7s
2025-12-01 17:53:42 +01:00
401d169cc0 upgrade kotlin version 2025-12-01 17:53:06 +01:00
fa2c6b6621 refactor and simplify functions 2025-12-01 17:48:45 +01:00
4fbadfba1e implement tests 2025-12-01 16:24:58 +01:00
eb2cdbea73 implement typed array to byte array conversions 2025-12-01 16:24:35 +01:00
941a486217 bump version to 1.2.2
All checks were successful
/ publish (push) Successful in 2m32s
2025-11-10 18:08:28 +01:00
3a0a947fb0 fix padding
again
2025-11-10 18:08:21 +01:00
34 changed files with 918 additions and 515 deletions

View File

@@ -1,36 +0,0 @@
on:
push:
branches:
- main
env:
GIT__TOKEN: ${{ secrets.TOKEN }}
jobs:
publish:
runs-on: linux
steps:
- uses: actions/checkout@v4
- uses: actions/cache/restore@v4
with:
key: gradle
path: |
~/.gradle/caches/
~/.gradle/wrapper/
~/.konan/
- uses: actions/setup-java@v4
with:
java-version: '24'
distribution: 'temurin'
- run: ./gradlew publish
- uses: actions/cache/save@v4
with:
key: gradle
path: |
~/.gradle/caches/
~/.gradle/wrapper/
~/.konan/

View File

@@ -0,0 +1,47 @@
on:
push:
branches: [ main ]
jobs:
publish:
runs-on: linux
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Java
uses: actions/setup-java@v4
with:
java-version: '24'
distribution: 'temurin'
- name: Cache Gradle
uses: actions/cache@v4
with:
key: gradle-${{ hashFiles('**/libs.versions.toml') }}
restore-keys: gradle
path: |
~/.gradle/caches/
~/.gradle/wrapper/
- name: Cache Konan
uses: actions/cache@v4
with:
key: konan-${{ hashFiles('**/libs.versions.toml') }}
restore-keys: konan
path: |
~/.konan/
- name: Cache Node
uses: actions/cache@v4
with:
key: node-${{ hashFiles('**/libs.versions.toml') }}
restore-keys: node
path: |
~/.gradle/nodejs
~/.gradle/yarn
- name: Publish
run: ./gradlew publish
env:
INFENDRO__TOKEN: ${{ secrets.TOKEN }}

View File

@@ -0,0 +1,28 @@
on:
pull_request:
branches: [ main ]
jobs:
test:
runs-on: linux
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Java
uses: actions/setup-java@v4
with:
java-version: '24'
distribution: 'temurin'
- name: Cache Gradle
uses: actions/cache@v4
with:
key: gradle-${{ hashFiles('**/libs.versions.toml') }}
restore-keys: gradle
path: |
~/.gradle/caches/
~/.gradle/wrapper/
- name: Test
run: ./gradlew jvmTest

View File

@@ -1,13 +1,6 @@
# Kotlin ByteArray Utilities # bytes.kt
This library provides various utilities for working with ByteArray in Kotlin Multiplatform. `bytes.kt` is a Kotlin Multiplatform library that provides helper functions for working with bytes.
## Features
* Multiplatform support
* JVM
* JavaScript
* Native (Linux)
## Installation ## Installation
@@ -18,7 +11,7 @@ repositories {
maven("https://git.infendro.com/api/packages/Infendro/maven") maven("https://git.infendro.com/api/packages/Infendro/maven")
} }
dependencies { commonMain.dependencies {
implementation("com.infendro:bytearray:1.1.1") implementation("com.infendro:bytes:1.4.2")
} }
``` ```

View File

@@ -1,52 +1,40 @@
group = "com.infendro" @file:OptIn(ExperimentalWasmDsl::class)
version = "1.2.1"
repositories { import com.infendro.plugin.infendro
mavenCentral() import com.infendro.plugin.token
} import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
group = "com.infendro"
version = "1.4.2"
plugins { plugins {
alias(libs.plugins.infendro)
alias(libs.plugins.multiplatform) alias(libs.plugins.multiplatform)
id("maven-publish") id("maven-publish")
} }
kotlin { kotlin {
jvm() jvm()
js {
nodejs()
}
linuxX64() linuxX64()
linuxArm64()
mingwX64()
macosX64()
macosArm64()
js { nodejs() }
wasmJs { nodejs() }
wasmWasi { nodejs() }
compilerOptions {
freeCompilerArgs.add("-opt-in=kotlin.ExperimentalUnsignedTypes")
}
}
publishing {
repositories { repositories {
maven { mavenCentral()
url = uri("https://git.infendro.com/api/packages/Infendro/maven")
authentication.create("header", HttpHeaderAuthentication::class)
credentials(HttpHeaderCredentials::class) {
val token = secret("git.token") ?: throw GradleException()
name = "Authorization"
value = "token $token"
} }
sourceSets {
commonTest.dependencies {
implementation(libs.test)
} }
} }
} }
fun secret( publishing.repositories {
key: String, infendro(token)
): String? {
val property = key
val environment = key
.uppercase()
.replace(".", "__")
val prop = properties[property] as String?
val env = System.getenv(environment)
return prop ?: env
} }

View File

@@ -1,5 +1,10 @@
[versions] [versions]
kotlin = "2.1.20" infendro = "1.1.0"
kotlin = "2.3.0"
[plugins] [plugins]
infendro = { id = "com.infendro.plugin", version.ref = "infendro" }
multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" } multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
[libraries]
test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" }

View File

@@ -1 +1,6 @@
rootProject.name = "bytearray" rootProject.name = "bytes"
pluginManagement.repositories {
maven("https://git.infendro.com/api/packages/Infendro/maven")
mavenCentral()
}

View File

@@ -1,148 +0,0 @@
package com.infendro.bytearray
import com.infendro.bytearray.ByteOrder.BIG_ENDIAN
import kotlin.experimental.and
import kotlin.experimental.or
import kotlin.experimental.xor
infix fun ByteArray.and(
that: ByteArray,
): ByteArray {
if (this.size != that.size) throw Exception()
return ByteArray(size) { i ->
this[i] and that[i]
}
}
infix fun ByteArray.or(
that: ByteArray,
): ByteArray {
if (this.size != that.size) throw Exception()
return ByteArray(size) { i ->
this[i] or that[i]
}
}
infix fun ByteArray.xor(
that: ByteArray,
): ByteArray {
if (this.size != that.size) throw Exception()
return ByteArray(size) { i ->
this[i] xor that[i]
}
}
fun ByteArray.padStart(
length: Int,
padByte: Byte,
): ByteArray {
return buildList {
repeat(length - size) {
add(padByte)
}
addAll(this)
}.toByteArray()
}
fun ByteArray.padEnd(
length: Int,
padByte: Byte,
): ByteArray {
return buildList {
addAll(this)
repeat(length - size) {
add(padByte)
}
}.toByteArray()
}
fun ByteArray.toInt(
order: ByteOrder = BIG_ENDIAN,
): Int {
if (size != 4) throw Exception()
val bytes = order.normalize(this)
return bytes.fold(0) { acc, byte ->
(acc shl 8) or (byte.toInt() and 0xFF)
}
}
fun ByteArray.toIntArray(
order: ByteOrder = BIG_ENDIAN,
): IntArray {
if (size % 4 != 0) throw Exception()
return toList()
.chunked(4)
.map { it.toByteArray().toInt(order) }
.toIntArray()
}
fun ByteArray.toLong(
order: ByteOrder = BIG_ENDIAN,
): Long {
if (size != 8) throw Exception()
val bytes = order.normalize(this)
return bytes.fold(0L) { acc, byte ->
(acc shl 8) or (byte.toLong() and 0xFFL)
}
}
fun ByteArray.toLongArray(
order: ByteOrder = BIG_ENDIAN,
): LongArray {
if (size % 8 != 0) throw Exception()
return toList()
.chunked(8)
.map { it.toByteArray().toLong(order) }
.toLongArray()
}
fun ByteArray.toUInt(
order: ByteOrder = BIG_ENDIAN,
): UInt {
if (size != 4) throw Exception()
val bytes = order.normalize(this)
return bytes.fold(0U) { acc, byte ->
(acc shl 8) or (byte.toUInt() and 0xFFU)
}
}
fun ByteArray.toUIntArray(
order: ByteOrder = BIG_ENDIAN,
): UIntArray {
if (size % 4 != 0) throw Exception()
return toList()
.chunked(4)
.map { it.toByteArray().toUInt(order) }
.toUIntArray()
}
fun ByteArray.toULong(
order: ByteOrder = BIG_ENDIAN,
): ULong {
if (size != 8) throw Exception()
val bytes = order.normalize(this)
return bytes.fold(0UL) { acc, byte ->
(acc shl 8) or (byte.toULong() and 0xFFUL)
}
}
fun ByteArray.toULongArray(
order: ByteOrder = BIG_ENDIAN,
): ULongArray {
if (size % 8 != 0) throw Exception()
return toList()
.chunked(8)
.map { it.toByteArray().toULong(order) }
.toULongArray()
}

View File

@@ -1,38 +0,0 @@
package com.infendro.bytearray
enum class ByteOrder {
BIG_ENDIAN {
override fun normalize(
bytes: ByteArray,
): ByteArray {
return bytes
}
override fun normalize(
bytes: UByteArray,
): UByteArray {
return bytes
}
},
LITTLE_ENDIAN {
override fun normalize(
bytes: ByteArray,
): ByteArray {
return bytes.reversedArray()
}
override fun normalize(
bytes: UByteArray,
): UByteArray {
return bytes.reversedArray()
}
};
internal abstract fun normalize(
bytes: ByteArray,
): ByteArray
internal abstract fun normalize(
bytes: UByteArray,
): UByteArray
}

View File

@@ -1,9 +0,0 @@
package com.infendro.bytearray
fun MutableCollection<Byte>.addAll(
bytes: ByteArray,
) = addAll(bytes.toList())
fun MutableCollection<UByte>.addAll(
bytes: UByteArray,
) = addAll(bytes.toList())

View File

@@ -1,23 +0,0 @@
package com.infendro.bytearray
import com.infendro.bytearray.ByteOrder.BIG_ENDIAN
fun Int.toByteArray(
order: ByteOrder = BIG_ENDIAN,
): ByteArray {
val bytes = ByteArray(4) { i ->
val offset = (3 - i) * 8
(this ushr offset).toByte()
}
return order.normalize(bytes)
}
fun Int.toUByteArray(
order: ByteOrder = BIG_ENDIAN,
): UByteArray {
val bytes = UByteArray(4) { i ->
val offset = (3 - i) * 8
(this ushr offset).toUByte()
}
return order.normalize(bytes)
}

View File

@@ -1,23 +0,0 @@
package com.infendro.bytearray
import com.infendro.bytearray.ByteOrder.BIG_ENDIAN
fun Long.toByteArray(
order: ByteOrder = BIG_ENDIAN,
): ByteArray {
val bytes = ByteArray(8) { i ->
val offset = (7 - i) * 8
(this ushr offset).toByte()
}
return order.normalize(bytes)
}
fun Long.toUByteArray(
order: ByteOrder = BIG_ENDIAN,
): UByteArray {
val bytes = UByteArray(8) { i ->
val offset = (7 - i) * 8
(this ushr offset).toUByte()
}
return order.normalize(bytes)
}

View File

@@ -1,145 +0,0 @@
package com.infendro.bytearray
import com.infendro.bytearray.ByteOrder.BIG_ENDIAN
infix fun UByteArray.and(
that: UByteArray,
): UByteArray {
if (this.size != that.size) throw Exception()
return UByteArray(size) { i ->
this[i] and that[i]
}
}
infix fun UByteArray.or(
that: UByteArray,
): UByteArray {
if (this.size != that.size) throw Exception()
return UByteArray(size) { i ->
this[i] or that[i]
}
}
infix fun UByteArray.xor(
that: UByteArray,
): UByteArray {
if (this.size != that.size) throw Exception()
return UByteArray(size) { i ->
this[i] xor that[i]
}
}
fun UByteArray.padStart(
length: Int,
padByte: UByte,
): UByteArray {
return buildList {
repeat(length - size) {
add(padByte)
}
addAll(this)
}.toUByteArray()
}
fun UByteArray.padEnd(
length: Int,
padByte: UByte,
): UByteArray {
return buildList {
addAll(this)
repeat(length - size) {
add(padByte)
}
}.toUByteArray()
}
fun UByteArray.toInt(
order: ByteOrder = BIG_ENDIAN,
): Int {
if (size != 4) throw Exception()
val bytes = order.normalize(this)
return bytes.fold(0) { acc, byte ->
(acc shl 8) or (byte.toInt() and 0xFF)
}
}
fun UByteArray.toIntArray(
order: ByteOrder = BIG_ENDIAN,
): IntArray {
if (size % 4 != 0) throw Exception()
return toList()
.chunked(4)
.map { it.toUByteArray().toInt(order) }
.toIntArray()
}
fun UByteArray.toLong(
order: ByteOrder = BIG_ENDIAN,
): Long {
if (size != 8) throw Exception()
val bytes = order.normalize(this)
return bytes.fold(0L) { acc, byte ->
(acc shl 8) or (byte.toLong() and 0xFFL)
}
}
fun UByteArray.toLongArray(
order: ByteOrder = BIG_ENDIAN,
): LongArray {
if (size % 8 != 0) throw Exception()
return toList()
.chunked(8)
.map { it.toUByteArray().toLong(order) }
.toLongArray()
}
fun UByteArray.toUInt(
order: ByteOrder = BIG_ENDIAN,
): UInt {
if (size != 4) throw Exception()
val bytes = order.normalize(this)
return bytes.fold(0U) { acc, byte ->
(acc shl 8) or (byte.toUInt() and 0xFFU)
}
}
fun UByteArray.toUIntArray(
order: ByteOrder = BIG_ENDIAN,
): UIntArray {
if (size % 4 != 0) throw Exception()
return toList()
.chunked(4)
.map { it.toUByteArray().toUInt(order) }
.toUIntArray()
}
fun UByteArray.toULong(
order: ByteOrder = BIG_ENDIAN,
): ULong {
if (size != 8) throw Exception()
val bytes = order.normalize(this)
return bytes.fold(0UL) { acc, byte ->
(acc shl 8) or (byte.toULong() and 0xFFUL)
}
}
fun UByteArray.toULongArray(
order: ByteOrder = BIG_ENDIAN,
): ULongArray {
if (size % 8 != 0) throw Exception()
return toList()
.chunked(8)
.map { it.toUByteArray().toULong(order) }
.toULongArray()
}

View File

@@ -1,23 +0,0 @@
package com.infendro.bytearray
import com.infendro.bytearray.ByteOrder.BIG_ENDIAN
fun UInt.toByteArray(
order: ByteOrder = BIG_ENDIAN,
): ByteArray {
val bytes = ByteArray(4) { i ->
val offset = (3 - i) * 8
(this shr offset).toByte()
}
return order.normalize(bytes)
}
fun UInt.toUByteArray(
order: ByteOrder = BIG_ENDIAN,
): UByteArray {
val bytes = UByteArray(4) { i ->
val offset = (3 - i) * 8
(this shr offset).toUByte()
}
return order.normalize(bytes)
}

View File

@@ -1,23 +0,0 @@
package com.infendro.bytearray
import com.infendro.bytearray.ByteOrder.BIG_ENDIAN
fun ULong.toByteArray(
order: ByteOrder = BIG_ENDIAN,
): ByteArray {
val bytes = ByteArray(8) { i ->
val offset = (7 - i) * 8
(this shr offset).toByte()
}
return order.normalize(bytes)
}
fun ULong.toUByteArray(
order: ByteOrder = BIG_ENDIAN,
): UByteArray {
val bytes = UByteArray(8) { i ->
val offset = (7 - i) * 8
(this shr offset).toUByte()
}
return order.normalize(bytes)
}

View File

@@ -0,0 +1,5 @@
package com.infendro.bytes
enum class ByteOrder {
BIG_ENDIAN, LITTLE_ENDIAN;
}

View File

@@ -0,0 +1,7 @@
package com.infendro.bytes.byte
import kotlin.experimental.inv
operator fun Byte.not(): Byte {
return inv()
}

View File

@@ -0,0 +1,145 @@
package com.infendro.bytes.bytearray
import com.infendro.bytes.ByteOrder
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
fun ByteArray.toInt(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN): Int {
require(startIndex in 0..(size - 4))
return when (order) {
BIG_ENDIAN -> {
((this[startIndex].toInt() and 0xFF) shl 24) or
((this[startIndex + 1].toInt() and 0xFF) shl 16) or
((this[startIndex + 2].toInt() and 0xFF) shl 8) or
(this[startIndex + 3].toInt() and 0xFF)
}
LITTLE_ENDIAN -> {
(this[startIndex].toInt() and 0xFF) or
((this[startIndex + 1].toInt() and 0xFF) shl 8) or
((this[startIndex + 2].toInt() and 0xFF) shl 16) or
((this[startIndex + 3].toInt() and 0xFF) shl 24)
}
}
}
fun ByteArray.toLong(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN): Long {
require(startIndex in 0..(size - 8))
return when (order) {
BIG_ENDIAN -> {
((this[startIndex].toLong() and 0xFFL) shl 56) or
((this[startIndex + 1].toLong() and 0xFFL) shl 48) or
((this[startIndex + 2].toLong() and 0xFFL) shl 40) or
((this[startIndex + 3].toLong() and 0xFFL) shl 32) or
((this[startIndex + 4].toLong() and 0xFFL) shl 24) or
((this[startIndex + 5].toLong() and 0xFFL) shl 16) or
((this[startIndex + 6].toLong() and 0xFFL) shl 8) or
(this[startIndex + 7].toLong() and 0xFFL)
}
LITTLE_ENDIAN -> {
(this[startIndex].toLong() and 0xFFL) or
((this[startIndex + 1].toLong() and 0xFFL) shl 8) or
((this[startIndex + 2].toLong() and 0xFFL) shl 16) or
((this[startIndex + 3].toLong() and 0xFFL) shl 24) or
((this[startIndex + 4].toLong() and 0xFFL) shl 32) or
((this[startIndex + 5].toLong() and 0xFFL) shl 40) or
((this[startIndex + 6].toLong() and 0xFFL) shl 48) or
((this[startIndex + 7].toLong() and 0xFFL) shl 56)
}
}
}
fun ByteArray.copyInto(
destination: IntArray,
destinationOffset: Int = 0,
startIndex: Int = 0,
endIndex: Int = size,
order: ByteOrder = BIG_ENDIAN,
) {
val length = endIndex - startIndex
require(length >= 0 && length % 4 == 0)
require(startIndex in 0..(size - length))
require(destinationOffset in 0..(destination.size - (length / 4)))
when (order) {
BIG_ENDIAN -> {
repeat(length / 4) { i ->
val offset = startIndex + (i * 4)
destination[destinationOffset + i] =
((this[offset].toInt() and 0xFF) shl 24) or
((this[offset + 1].toInt() and 0xFF) shl 16) or
((this[offset + 2].toInt() and 0xFF) shl 8) or
(this[offset + 3].toInt() and 0xFF)
}
}
LITTLE_ENDIAN -> {
repeat(length / 4) { i ->
val offset = startIndex + (i * 4)
destination[destinationOffset + i] =
(this[offset].toInt() and 0xFF) or
((this[offset + 1].toInt() and 0xFF) shl 8) or
((this[offset + 2].toInt() and 0xFF) shl 16) or
((this[offset + 3].toInt() and 0xFF) shl 24)
}
}
}
}
fun ByteArray.toIntArray(startIndex: Int = 0, endIndex: Int = size, order: ByteOrder = BIG_ENDIAN): IntArray {
val length = endIndex - startIndex
val result = IntArray(length / 4)
copyInto(result, startIndex = startIndex, endIndex = endIndex, order = order)
return result
}
fun ByteArray.copyInto(
destination: LongArray,
destinationOffset: Int = 0,
startIndex: Int = 0,
endIndex: Int = size,
order: ByteOrder = BIG_ENDIAN,
) {
val length = endIndex - startIndex
require(length >= 0 && length % 8 == 0)
require(startIndex in 0..(size - length))
require(destinationOffset in 0..(destination.size - (length / 8)))
when (order) {
BIG_ENDIAN -> {
repeat(length / 8) { i ->
val offset = startIndex + (i * 8)
destination[destinationOffset + i] =
((this[offset].toLong() and 0xFFL) shl 56) or
((this[offset + 1].toLong() and 0xFFL) shl 48) or
((this[offset + 2].toLong() and 0xFFL) shl 40) or
((this[offset + 3].toLong() and 0xFFL) shl 32) or
((this[offset + 4].toLong() and 0xFFL) shl 24) or
((this[offset + 5].toLong() and 0xFFL) shl 16) or
((this[offset + 6].toLong() and 0xFFL) shl 8) or
(this[offset + 7].toLong() and 0xFFL)
}
}
LITTLE_ENDIAN -> {
repeat(length / 8) { i ->
val offset = startIndex + (i * 8)
destination[destinationOffset + i] =
(this[offset].toLong() and 0xFFL) or
((this[offset + 1].toLong() and 0xFFL) shl 8) or
((this[offset + 2].toLong() and 0xFFL) shl 16) or
((this[offset + 3].toLong() and 0xFFL) shl 24) or
((this[offset + 4].toLong() and 0xFFL) shl 32) or
((this[offset + 5].toLong() and 0xFFL) shl 40) or
((this[offset + 6].toLong() and 0xFFL) shl 48) or
((this[offset + 7].toLong() and 0xFFL) shl 56)
}
}
}
}
fun ByteArray.toLongArray(startIndex: Int = 0, endIndex: Int = size, order: ByteOrder = BIG_ENDIAN): LongArray {
val length = endIndex - startIndex
val result = LongArray(length / 8)
copyInto(result, startIndex = startIndex, endIndex = endIndex, order = order)
return result
}

View File

@@ -0,0 +1,188 @@
package com.infendro.bytes.bytearray
import com.infendro.bytes.byte.not
import kotlin.experimental.and
import kotlin.experimental.or
import kotlin.experimental.xor
fun ByteArray.andInto(
that: ByteArray,
destination: ByteArray,
destinationOffset: Int = 0,
startIndex: Int = 0,
endIndex: Int = size,
thatStartIndex: Int = 0,
) {
val length = endIndex - startIndex
require(length >= 0)
require(startIndex in 0..(size - length))
require(thatStartIndex in 0..(that.size - length))
require(destinationOffset in 0..(destination.size - length))
for (i in 0..<length) {
destination[destinationOffset + i] = this[startIndex + i] and that[thatStartIndex + i]
}
}
fun ByteArray.andInto(
that: Byte,
destination: ByteArray,
destinationOffset: Int = 0,
startIndex: Int = 0,
endIndex: Int = size,
) {
val length = endIndex - startIndex
require(length >= 0)
require(startIndex in 0..(size - length))
require(destinationOffset in 0..(destination.size - length))
for (i in 0..<length) {
destination[destinationOffset + i] = this[startIndex + i] and that
}
}
fun ByteArray.andWith(that: ByteArray, startIndex: Int = 0, endIndex: Int = size, thatStartIndex: Int = 0) =
andInto(that, this, startIndex, startIndex, endIndex, thatStartIndex)
fun ByteArray.andWith(that: Byte, startIndex: Int = 0, endIndex: Int = size) =
andInto(that, this, startIndex, startIndex, endIndex)
infix fun ByteArray.and(that: ByteArray): ByteArray {
val result = ByteArray(size)
andInto(that, result)
return result
}
infix fun ByteArray.and(that: Byte): ByteArray {
val result = ByteArray(size)
andInto(that, result)
return result
}
fun ByteArray.orInto(
that: ByteArray,
destination: ByteArray,
destinationOffset: Int = 0,
startIndex: Int = 0,
endIndex: Int = size,
thatStartIndex: Int = 0,
) {
val length = endIndex - startIndex
require(length >= 0)
require(startIndex in 0..(size - length))
require(thatStartIndex in 0..(that.size - length))
require(destinationOffset in 0..(destination.size - length))
for (i in 0..<length) {
destination[destinationOffset + i] = this[startIndex + i] or that[thatStartIndex + i]
}
}
fun ByteArray.orInto(
that: Byte,
destination: ByteArray,
destinationOffset: Int = 0,
startIndex: Int = 0,
endIndex: Int = size,
) {
val length = endIndex - startIndex
require(length >= 0)
require(startIndex in 0..(size - length))
require(destinationOffset in 0..(destination.size - length))
for (i in 0..<length) {
destination[destinationOffset + i] = this[startIndex + i] or that
}
}
fun ByteArray.orWith(that: ByteArray, startIndex: Int = 0, endIndex: Int = size, thatStartIndex: Int = 0) =
orInto(that, this, startIndex, startIndex, endIndex, thatStartIndex)
fun ByteArray.orWith(that: Byte, startIndex: Int = 0, endIndex: Int = size) =
orInto(that, this, startIndex, startIndex, endIndex)
infix fun ByteArray.or(that: ByteArray): ByteArray {
val result = ByteArray(size)
orInto(that, result)
return result
}
infix fun ByteArray.or(that: Byte): ByteArray {
val result = ByteArray(size)
orInto(that, result)
return result
}
fun ByteArray.xorInto(
that: ByteArray,
destination: ByteArray,
destinationOffset: Int = 0,
startIndex: Int = 0,
endIndex: Int = size,
thatStartIndex: Int = 0,
) {
val length = endIndex - startIndex
require(length >= 0)
require(startIndex in 0..(size - length))
require(thatStartIndex in 0..(that.size - length))
require(destinationOffset in 0..(destination.size - length))
for (i in 0..<length) {
destination[destinationOffset + i] = this[startIndex + i] xor that[thatStartIndex + i]
}
}
fun ByteArray.xorInto(
that: Byte,
destination: ByteArray,
destinationOffset: Int = 0,
startIndex: Int = 0,
endIndex: Int = size,
) {
val length = endIndex - startIndex
require(length >= 0)
require(startIndex in 0..(size - length))
require(destinationOffset in 0..(destination.size - length))
for (i in 0..<length) {
destination[destinationOffset + i] = this[startIndex + i] xor that
}
}
fun ByteArray.xorWith(that: ByteArray, startIndex: Int = 0, endIndex: Int = size, thatStartIndex: Int = 0) =
xorInto(that, this, startIndex, startIndex, endIndex, thatStartIndex)
fun ByteArray.xorWith(that: Byte, startIndex: Int = 0, endIndex: Int = size) =
xorInto(that, this, startIndex, startIndex, endIndex)
infix fun ByteArray.xor(that: ByteArray): ByteArray {
val result = ByteArray(size)
xorInto(that, result)
return result
}
infix fun ByteArray.xor(that: Byte): ByteArray {
val result = ByteArray(size)
xorInto(that, result)
return result
}
fun ByteArray.invInto(destination: ByteArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size) {
val length = endIndex - startIndex
require(length >= 0)
require(startIndex in 0..(size - length))
require(destinationOffset in 0..(destination.size - length))
for (i in 0..<length) {
destination[destinationOffset + i] = !this[startIndex + i]
}
}
fun ByteArray.inv(startIndex: Int, endIndex: Int) =
invInto(this, startIndex, startIndex, endIndex)
operator fun ByteArray.not(): ByteArray {
val result = ByteArray(size)
invInto(result)
return result
}

View File

@@ -0,0 +1,30 @@
package com.infendro.bytes.int
import com.infendro.bytes.ByteOrder
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
fun Int.copyInto(destination: ByteArray, destinationOffset: Int = 0, order: ByteOrder = BIG_ENDIAN) {
require(destinationOffset in 0..(destination.size - 4))
when (order) {
BIG_ENDIAN -> {
destination[destinationOffset] = (this shr 24).toByte()
destination[destinationOffset + 1] = (this shr 16).toByte()
destination[destinationOffset + 2] = (this shr 8).toByte()
destination[destinationOffset + 3] = this.toByte()
}
LITTLE_ENDIAN -> {
destination[destinationOffset] = this.toByte()
destination[destinationOffset + 1] = (this shr 8).toByte()
destination[destinationOffset + 2] = (this shr 16).toByte()
destination[destinationOffset + 3] = (this shr 24).toByte()
}
}
}
fun Int.toByteArray(order: ByteOrder = BIG_ENDIAN): ByteArray {
val result = ByteArray(4)
copyInto(result, order = order)
return result
}

View File

@@ -0,0 +1,5 @@
package com.infendro.bytes.int
operator fun Int.not(): Int {
return inv()
}

View File

@@ -0,0 +1,48 @@
package com.infendro.bytes.intarray
import com.infendro.bytes.ByteOrder
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
fun IntArray.copyInto(
destination: ByteArray,
destinationOffset: Int = 0,
startIndex: Int = 0,
endIndex: Int = size,
order: ByteOrder = BIG_ENDIAN,
) {
val length = endIndex - startIndex
require(length >= 0)
require(startIndex in 0..(size - length))
require(destinationOffset in 0..(destination.size - (length * 4)))
when (order) {
BIG_ENDIAN -> {
repeat(length) { i ->
val offset = destinationOffset + (i * 4)
val value = this[startIndex + i]
destination[offset] = (value shr 24).toByte()
destination[offset + 1] = (value shr 16).toByte()
destination[offset + 2] = (value shr 8).toByte()
destination[offset + 3] = value.toByte()
}
}
LITTLE_ENDIAN -> {
repeat(length) { i ->
val offset = destinationOffset + (i * 4)
val value = this[startIndex + i]
destination[offset] = value.toByte()
destination[offset + 1] = (value shr 8).toByte()
destination[offset + 2] = (value shr 16).toByte()
destination[offset + 3] = (value shr 24).toByte()
}
}
}
}
fun IntArray.toByteArray(startIndex: Int = 0, endIndex: Int = size, order: ByteOrder = BIG_ENDIAN): ByteArray {
val length = endIndex - startIndex
val result = ByteArray(length * 4)
copyInto(result, startIndex = startIndex, endIndex = endIndex, order = order)
return result
}

View File

@@ -0,0 +1,38 @@
package com.infendro.bytes.long
import com.infendro.bytes.ByteOrder
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
fun Long.copyInto(destination: ByteArray, destinationOffset: Int = 0, order: ByteOrder = BIG_ENDIAN) {
require(destinationOffset in 0..(destination.size - 8))
when (order) {
BIG_ENDIAN -> {
destination[destinationOffset] = (this shr 56).toByte()
destination[destinationOffset + 1] = (this shr 48).toByte()
destination[destinationOffset + 2] = (this shr 40).toByte()
destination[destinationOffset + 3] = (this shr 32).toByte()
destination[destinationOffset + 4] = (this shr 24).toByte()
destination[destinationOffset + 5] = (this shr 16).toByte()
destination[destinationOffset + 6] = (this shr 8).toByte()
destination[destinationOffset + 7] = this.toByte()
}
LITTLE_ENDIAN -> {
destination[destinationOffset] = this.toByte()
destination[destinationOffset + 1] = (this shr 8).toByte()
destination[destinationOffset + 2] = (this shr 16).toByte()
destination[destinationOffset + 3] = (this shr 24).toByte()
destination[destinationOffset + 4] = (this shr 32).toByte()
destination[destinationOffset + 5] = (this shr 40).toByte()
destination[destinationOffset + 6] = (this shr 48).toByte()
destination[destinationOffset + 7] = (this shr 56).toByte()
}
}
}
fun Long.toByteArray(order: ByteOrder = BIG_ENDIAN): ByteArray {
val result = ByteArray(8)
copyInto(result, order = order)
return result
}

View File

@@ -0,0 +1,5 @@
package com.infendro.bytes.long
operator fun Long.not(): Long {
return inv()
}

View File

@@ -0,0 +1,56 @@
package com.infendro.bytes.longarray
import com.infendro.bytes.ByteOrder
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
fun LongArray.copyInto(
destination: ByteArray,
destinationOffset: Int = 0,
startIndex: Int = 0,
endIndex: Int = size,
order: ByteOrder = BIG_ENDIAN,
) {
val length = endIndex - startIndex
require(length >= 0)
require(startIndex in 0..(size - length))
require(destinationOffset in 0..(destination.size - (length * 8)))
when (order) {
BIG_ENDIAN -> {
repeat(length) { i ->
val offset = destinationOffset + (i * 8)
val value = this[startIndex + i]
destination[offset] = (value shr 56).toByte()
destination[offset + 1] = (value shr 48).toByte()
destination[offset + 2] = (value shr 40).toByte()
destination[offset + 3] = (value shr 32).toByte()
destination[offset + 4] = (value shr 24).toByte()
destination[offset + 5] = (value shr 16).toByte()
destination[offset + 6] = (value shr 8).toByte()
destination[offset + 7] = value.toByte()
}
}
LITTLE_ENDIAN -> {
repeat(length) { i ->
val offset = destinationOffset + (i * 8)
val value = this[startIndex + i]
destination[offset] = value.toByte()
destination[offset + 1] = (value shr 8).toByte()
destination[offset + 2] = (value shr 16).toByte()
destination[offset + 3] = (value shr 24).toByte()
destination[offset + 4] = (value shr 32).toByte()
destination[offset + 5] = (value shr 40).toByte()
destination[offset + 6] = (value shr 48).toByte()
destination[offset + 7] = (value shr 56).toByte()
}
}
}
}
fun LongArray.toByteArray(startIndex: Int = 0, endIndex: Int = size, order: ByteOrder = BIG_ENDIAN): ByteArray {
val length = endIndex - startIndex
val result = ByteArray(length * 8)
copyInto(result, startIndex = startIndex, endIndex = endIndex, order = order)
return result
}

View File

@@ -0,0 +1,13 @@
package com.infendro.bytes.byte
import kotlin.test.Test
import kotlin.test.assertEquals
class ByteOperatorTest {
@Test
fun not() {
val expected = (-0x01).toByte()
val actual = !0x00.toByte()
assertEquals(expected, actual)
}
}

View File

@@ -0,0 +1,98 @@
package com.infendro.bytes.bytearray
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
import kotlin.test.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals
class ByteArrayConversionTest {
@Test
fun toInt() {
val expected = 0x01234567
run {
val actual = byteArrayOf(0x01, 0x23, 0x45, 0x67).toInt()
assertEquals(expected, actual)
}
run {
val actual = byteArrayOf(0x67, 0x45, 0x23, 0x01).toInt(order = LITTLE_ENDIAN)
assertEquals(expected, actual)
}
}
@Test
fun toLong() {
val expected = 0x0123456789abcdefL
run {
val actual = byteArrayOf(0x01, 0x23, 0x45, 0x67, -0x77, -0x55, -0x33, -0x11).toLong()
assertEquals(expected, actual)
}
run {
val actual = byteArrayOf(-0x11, -0x33, -0x55, -0x77, 0x67, 0x45, 0x23, 0x01).toLong(order = LITTLE_ENDIAN)
assertEquals(expected, actual)
}
}
@Test
fun copyIntoIntArray() {
val expected = intArrayOf(0, 0x01234567, 0x01234567)
val actual = IntArray(3).also { array ->
val bytes = byteArrayOf(
0x01, 0x23, 0x45, 0x67,
0x01, 0x23, 0x45, 0x67,
)
bytes.copyInto(array, 1)
}
assertContentEquals(expected, actual)
}
@Test
fun toIntArray() {
val expected = intArrayOf(0x01234567, 0x67452301)
run {
val actual = byteArrayOf(
0x01, 0x23, 0x45, 0x67,
0x67, 0x45, 0x23, 0x01,
).toIntArray()
assertContentEquals(expected, actual)
}
run {
val actual = byteArrayOf(
0x67, 0x45, 0x23, 0x01,
0x01, 0x23, 0x45, 0x67,
).toIntArray(order = LITTLE_ENDIAN)
assertContentEquals(expected, actual)
}
}
@Test
fun copyIntoLongArray() {
val expected = longArrayOf(0, 0x0123456789abcdefL, 0x0123456789abcdefL)
val actual = LongArray(3).also { array ->
val bytes = byteArrayOf(
0x01, 0x23, 0x45, 0x67, -0x77, -0x55, -0x33, -0x11,
0x01, 0x23, 0x45, 0x67, -0x77, -0x55, -0x33, -0x11,
)
bytes.copyInto(array, 1)
}
assertContentEquals(expected, actual)
}
@Test
fun toLongArray() {
val expected = longArrayOf(0x0123456789abcdefL, -0x1032547698badcffL)
run {
val actual = byteArrayOf(
0x01, 0x23, 0x45, 0x67, -0x77, -0x55, -0x33, -0x11,
-0x11, -0x33, -0x55, -0x77, 0x67, 0x45, 0x23, 0x01,
).toLongArray()
assertContentEquals(expected, actual)
}
run {
val actual = byteArrayOf(
-0x11, -0x33, -0x55, -0x77, 0x67, 0x45, 0x23, 0x01,
0x01, 0x23, 0x45, 0x67, -0x77, -0x55, -0x33, -0x11,
).toLongArray(order = LITTLE_ENDIAN)
assertContentEquals(expected, actual)
}
}
}

View File

@@ -0,0 +1,55 @@
package com.infendro.bytes.bytearray
import kotlin.test.Test
import kotlin.test.assertContentEquals
class ByteArrayOperatorTest {
@Test
fun and() {
val expected = byteArrayOf(0x18, 0x00)
val actual = byteArrayOf(0x5C, 0x70) and byteArrayOf(0x3A, 0x0F)
assertContentEquals(expected, actual)
}
@Test
fun andByte() {
val expected = byteArrayOf(0x0C, 0x00)
val actual = byteArrayOf(0x5C, 0x70) and 0x0F
assertContentEquals(expected, actual)
}
@Test
fun or() {
val expected = byteArrayOf(0x7E, 0x7F)
val actual = byteArrayOf(0x5C, 0x70) or byteArrayOf(0x3A, 0x0F)
assertContentEquals(expected, actual)
}
@Test
fun orByte() {
val expected = byteArrayOf(0x5F, 0x7F)
val actual = byteArrayOf(0x5C, 0x70) or 0x0F
assertContentEquals(expected, actual)
}
@Test
fun xor() {
val expected = byteArrayOf(0x66, 0x7F)
val actual = byteArrayOf(0x5C, 0x70) xor byteArrayOf(0x3A, 0x0F)
assertContentEquals(expected, actual)
}
@Test
fun xorByte() {
val expected = byteArrayOf(0x53, 0x7F)
val actual = byteArrayOf(0x5C, 0x70) xor 0x0F
assertContentEquals(expected, actual)
}
@Test
fun not() {
val expected = byteArrayOf(-0x01, -0x20)
val actual = !byteArrayOf(0x00, 0x1F)
assertContentEquals(expected, actual)
}
}

View File

@@ -0,0 +1,20 @@
package com.infendro.bytes.int
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
import kotlin.test.Test
import kotlin.test.assertContentEquals
class IntConversionTest {
@Test
fun toByteArray() {
val expected = byteArrayOf(0x01, 0x23, 0x45, 0x67)
run {
val actual = 0x01234567.toByteArray()
assertContentEquals(expected, actual)
}
run {
val actual = 0x67452301.toByteArray(LITTLE_ENDIAN)
assertContentEquals(expected, actual)
}
}
}

View File

@@ -0,0 +1,13 @@
package com.infendro.bytes.int
import kotlin.test.Test
import kotlin.test.assertEquals
class IntOperatorTest {
@Test
fun not() {
val expected = -0x00000001
val actual = !0x00000000
assertEquals(expected, actual)
}
}

View File

@@ -0,0 +1,23 @@
package com.infendro.bytes.intarray
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
import kotlin.test.Test
import kotlin.test.assertContentEquals
class IntArrayConversionTest {
@Test
fun toByteArray() {
val expected = byteArrayOf(
0x01, 0x23, 0x45, 0x67,
0x67, 0x45, 0x23, 0x01,
)
run {
val actual = intArrayOf(0x01234567, 0x67452301).toByteArray()
assertContentEquals(expected, actual)
}
run {
val actual = intArrayOf(0x67452301, 0x01234567).toByteArray(order = LITTLE_ENDIAN)
assertContentEquals(expected, actual)
}
}
}

View File

@@ -0,0 +1,20 @@
package com.infendro.bytes.long
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
import kotlin.test.Test
import kotlin.test.assertContentEquals
class LongConversionTest {
@Test
fun toByteArray() {
val expected = byteArrayOf(0x01, 0x23, 0x45, 0x67, -0x77, -0x55, -0x33, -0x11)
run {
val actual = 0x0123456789abcdefL.toByteArray()
assertContentEquals(expected, actual)
}
run {
val actual = (-0x1032547698badcffL).toByteArray(LITTLE_ENDIAN)
assertContentEquals(expected, actual)
}
}
}

View File

@@ -0,0 +1,13 @@
package com.infendro.bytes.long
import kotlin.test.Test
import kotlin.test.assertEquals
class LongOperatorTest {
@Test
fun not() {
val expected = -0x0000000000000001L
val actual = !0x0000000000000000L
assertEquals(expected, actual)
}
}

View File

@@ -0,0 +1,23 @@
package com.infendro.bytes.longarray
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
import kotlin.test.Test
import kotlin.test.assertContentEquals
class LongArrayConversionTest {
@Test
fun toByteArray() {
val expected = byteArrayOf(
0x01, 0x23, 0x45, 0x67, -0x77, -0x55, -0x33, -0x11,
-0x11, -0x33, -0x55, -0x77, 0x67, 0x45, 0x23, 0x01,
)
run {
val actual = longArrayOf(0x0123456789abcdefL, -0x1032547698badcffL).toByteArray()
assertContentEquals(expected, actual)
}
run {
val actual = longArrayOf(-0x1032547698badcffL, 0x0123456789abcdefL).toByteArray(order = LITTLE_ENDIAN)
assertContentEquals(expected, actual)
}
}
}