refactored int codecs to DRY and easy support signed/unsigned values

This commit is contained in:
sergeych committed 2023-03-11 14:35:37 +01:00
1 parent 0f9702b1dc
commit 8fb052c4f9
9 files changed
+148 -108

No files matched your search

@@ -6,16 +6,16 @@ package net.sergeych.bintools
* like multiplatform version of DataInput
*
*/
abstract class DataSource {
interface DataSource {
abstract fun readByte(): Byte
fun readByte(): Byte
abstract val position: Int
val position: Int
open fun readUByte() = readByte().toUByte()
fun readUByte() = readByte().toUByte()
@Suppress("unused")
open fun readBytes(size: Int): ByteArray =
fun readBytes(size: Int): ByteArray =
ByteArray(size).also { a ->
for( i in 0..size)
a[i] = readByte()
@@ -23,7 +23,7 @@ abstract class DataSource {
}
fun ByteArray.toDataSource(): DataSource =
object : DataSource() {
object : DataSource {
override var position = 0
override fun readByte(): Byte = this@toDataSource[position++]