Implement streaming API

This commit is contained in:
2026-03-11 18:53:00 +01:00
parent cb3ff7ec6c
commit 328d4b957e
34 changed files with 936 additions and 593 deletions

View File

@@ -0,0 +1,46 @@
package com.infendro.hash.util
import com.infendro.bytes.ByteOrder
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
import com.infendro.bytes.int.copyInto
import com.infendro.bytes.long.copyInto
internal class Buffer(val size: Int) {
val value = ByteArray(size)
var offset = 0
val free: Int
get() = size - offset
fun isEmpty(): Boolean = offset == 0
fun isNotEmpty(): Boolean = !isEmpty()
fun isFull(): Boolean = offset == size
fun reset() {
value.fill(0x00)
offset = 0
}
fun jumpTo(offset: Int) {
this.offset = offset
}
fun append(byte: Byte) {
value[offset++] = byte
}
fun append(bytes: ByteArray, startIndex: Int = 0, endIndex: Int = bytes.size) {
bytes.copyInto(value, offset, startIndex, endIndex)
offset += (endIndex - startIndex)
}
fun append(n: Int, order: ByteOrder = BIG_ENDIAN) {
n.copyInto(value, offset, order = order)
offset += 4
}
fun append(n: Long, order: ByteOrder = BIG_ENDIAN) {
n.copyInto(value, offset, order = order)
offset += 8
}
}