Skip to content

Instantly share code, notes, and snippets.

View L-Briand's full-sized avatar

Lionel Briand L-Briand

View GitHub Profile
@L-Briand
L-Briand / ChannelInputStream.kt
Created April 28, 2025 08:25
Basic input and output stream from a Channel<ByteArray>
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.channels.ReceiveChannel
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import java.io.InputStream
class ChannelInputStream(val channel: ReceiveChannel<ByteArray>) : InputStream() {
private var buffer: ByteArray? = null
private var bufferIndex = 0
@L-Briand
L-Briand / GsonByteArrayB64TypeAdapter.kt
Last active February 13, 2025 08:24
A Gson TypeAdapter for Kotlin’s ByteArray to and from Base64 String.
import android.util.Base64
import com.google.gson.Gson
import com.google.gson.JsonParseException
import com.google.gson.TypeAdapter
import com.google.gson.TypeAdapterFactory
import com.google.gson.reflect.TypeToken
import com.google.gson.stream.JsonReader
import com.google.gson.stream.JsonToken
import com.google.gson.stream.JsonWriter
/**
* Transform the given dotted string [str] to a sequence of string.
* Example: `"key.value"` -> `["key", "value"]`
*
* - All values are trimmed of whitespaces. `" a. b .c "` -> `["a", "b", "c"]`
* - An empty point `"."`, multiple points `".."` produces nothing. -> `[]`
* - Leading point(s) `".key"`, `"..key"` or trailing point(s) `"key."`, `"key.."` are omitted -> `["key"]`
* - Multipoint between elements `"key..value"` are like single point -> `["key", "value"]`
*
* @param str The dotted string to parse
#!/usr/bin/env sh
usage() {
echo "usage:"
echo " $0 -k <key.pk8> -c <cert.x509.pem> [-o <keystore.jks>] [-s <keystore_password>]"
if [ -n "$1" ] ; then
echo "FILE: \"$1\" NOT FOUND"
fi
exit 1
}
@L-Briand
L-Briand / GiteaInstall.sh
Last active March 20, 2024 09:15
Installation script for Gitea on debian 12 with systemd service.
#!/bin/bash
GITEA_VERSION="1.21.8"
apt update && apt upgrade -y
apt install git openssh-server -y
adduser --system --shell /bin/bash --gecos 'Git Version Control' --group --disabled-password --home /home/git git
wget -O gitea https://dl.gitea.io/gitea/$GITEA_VERSION/gitea-$GITEA_VERSION-linux-amd64
chmod +x gitea
@L-Briand
L-Briand / ResourcePool.kt
Created February 14, 2024 08:33
Resource sharing with semaphore and mutex in kotlin
/**
* Thread safe shared resources.
*
* ```kotlin
* // await 1s with the resource
* fun run(pool: ResourcePool<*>) = launch { pool { delay(1.seconds) } }
*
* // Pool of 2 usable elements at the same time
* val pool = ResourcePool<Unit>(2) { Unit }
* (0 ..< 120).map { run(pool) }.joinAll() // elapsed time ~60s
@L-Briand
L-Briand / LineSequence.kt
Created February 5, 2024 09:55
Read lines as sequence from the given stream or reader.
/**
* Read the [InputStream] and yield a [CharSequence] every time a new line is found.
*
* @receiver The [InputStream] to parse.
* @return a sequence of each line in the stream. (without new lines)
*/
fun InputStream.lineSequence(): Sequence<CharSequence> = lineSequence { read() }
fun Reader.lineSequence(): Sequence<CharSequence> = lineSequence { read() }
/**
@L-Briand
L-Briand / GnuParse.kt
Last active January 24, 2024 13:16
Parse arguments with gnu syntax in Kotlin.
/**
* Parse arguments with gnu syntax.
*
* When parsing, the name given to [onArgument] can be:
* - `null` if found value has no hyphen.
* - A short option if the value starts with a hyphen. (`-s`)
* - A long option if the value starts with a double hyphen. (`--long`)
*
* The `getValue` can be used to get the next non-hyphen value
import kotlinx.coroutines.*
fun CoroutineScope.onMain(action: suspend CoroutineScope.() -> Unit) =
launch(Dispatchers.Main, block = action)
fun CoroutineScope.onIo(action: suspend CoroutineScope.() -> Unit) =
launch(Dispatchers.IO, block = action)
fun CoroutineScope.onDefault(action: suspend CoroutineScope.() -> Unit) =
launch(Dispatchers.Default, block = action)
@L-Briand
L-Briand / HexValue.kt
Last active May 3, 2023 18:09
Helper class to work with hexadecimal strings.
import kotlinx.serialization.Serializable
/**
* Utility class to work with hexadecimal strings.
*
* Two hex values with same [raw] should be equals.
* Even if one was created with lower cased hex and the other with upper cased hex.
*
* It contains :
* - static helpers to encode [fromString] and decode [toString] in hexadecimal strings.