Implement streaming API
This commit is contained in:
46
src/commonMain/kotlin/com/infendro/hash/util/Buffer.kt
Normal file
46
src/commonMain/kotlin/com/infendro/hash/util/Buffer.kt
Normal 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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user