Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
philips77 committed Mar 8, 2024
0 parents commit ae991b4
Show file tree
Hide file tree
Showing 30 changed files with 1,952 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
*.iml
.gradle
/local.properties
/.idea/caches
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
.DS_Store
/build
/captures
.externalNativeBuild
.cxx
local.properties
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions .idea/appInsightsSettings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/deploymentTargetDropDown.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/migrations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Copyright (c) 2024, Nordic Semiconductor
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
75 changes: 75 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Kotlin Util Library

This repository contains set of Kotlin-only (no Android dependency) libraries for various purposes.

## Data

Kotlin library for data manipulation.

It contains extension methods for `ByteArray` allowing to read numbers in big-endian and little-endian
order in various formats, bit operators for `Byte` and `Short` and methods for converting UUIDs to
and from `ByteArray`.

#### Setting up

Add the following to your `build.gradle` file:

```groovy
implementation "no.nordicsemi.kotlin:data:1.0.0"
```

### Reading Integers

```kotlin
val array = byteArrayOf(0x87.toByte(), 0x65, 0x43, 0x21, 0x21, 0x43, 0x65, 0x87.toByte())
val int1 = array.getInt(offset = 0, format = IntFormat.INT32, order = ByteOrder.BIG_ENDIAN)
val int2 = array.getInt(offset = 4, format = IntFormat.INT32, order = ByteOrder.LITTLE_ENDIAN)
assertEquals(-2023406815, int1)
assertEquals(-2023406815, int2)
```

### Reading Floats

Supported types include:
* IEEE 754 single and double precision
* IEEE 11073 32-bit and 16-bit

```kotlin
val array = byteArrayOf(0xFF.toByte(), 0x00, 0x01, 0x6C, 0x97.toByte(), 0x0D, 0x00, 0xFE.toByte())
val float1 = array.getFloat(0, FloatFormat.IEEE_11073_32_BIT, ByteOrder.BIG_ENDIAN)
val float2 = array.getFloat(4, FloatFormat.IEEE_11073_32_BIT, ByteOrder.LITTLE_ENDIAN)
assertEquals(36.4f, float1)
assertEquals(34.79f, float2)
```

### Bit operators

The library also contains extension methods for `Byte` allowing to perform bitwise operations.

```kotlin
val value = 0b1010_1001.toByte()
assertTrue(value hasAllBitsSet 0b1000_0001)
```

```kotlin
val value = 0b1010_1001.toByte()
assertTrue(value hasBitSet 0)
assertFalse(value hasBitCleared 0)
```

```kotlin
val value = 0b1010_1001_1010_1001.toShort()
assertEquals(0b1101_0100_1101_0100.toShort(), value shr 1)
assertEquals(0b1111_1010_1001_1010.toShort(), value shr 4)
```

### UUID

The library also contains extension methods for `ByteArray` allowing to convert UUIDs to and from `ByteArray`.

```kotlin
val uuid1 = UUID.randomUUID()
val array = uuid1.toByteArray(order = ByteOrder.BIG_ENDIAN)
val uuid2 = array.toUuid(order = ByteOrder.BIG_ENDIAN)
assertEquals(uuid, uuid2)
```
4 changes: 4 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
alias(libs.plugins.kotlin.jvm) apply false
}
1 change: 1 addition & 0 deletions data/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
52 changes: 52 additions & 0 deletions data/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2024, Nordic Semiconductor
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be
* used to endorse or promote products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

plugins {
alias(libs.plugins.kotlin.jvm)
alias(libs.plugins.nordic.nexus.kotlin)
}

group = "no.nordicsemi.kotlin"

nordicNexusPublishing {
POM_ARTIFACT_ID = "data"
POM_NAME = "Kotlin data utils."

POM_DESCRIPTION = "Set of extension methods for writing and reading bytes to and from a ByteArray."
POM_URL = "https://github.com/NordicSemiconductor/Kotlin-Util-Library"
POM_SCM_URL = "https://github.com/NordicSemiconductor/Kotlin-Util-Library"
POM_SCM_CONNECTION = "scm:[email protected]:NordicSemiconductor/Kotlin-Util-Library.git"
POM_SCM_DEV_CONNECTION = "scm:[email protected]:NordicSemiconductor/Kotlin-Util-Library.git"
}

dependencies {
testImplementation(libs.kotlin.junit)
}
17 changes: 17 additions & 0 deletions data/module-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in C:/Users/alno/AppData/Local/Android/sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.kts.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
Loading

0 comments on commit ae991b4

Please sign in to comment.