more support for ubytearrays
This commit is contained in:
1 parent
b5db8bb8cc
commit
30bf5fefe9
7 files changed
+42
-3
No files matched your search
@@ -6,6 +6,7 @@ package net.sergeych.bintools
|
||||
* like multiplatform version of DataInput
|
||||
*
|
||||
*/
|
||||
@Suppress("unused")
|
||||
interface DataSource {
|
||||
|
||||
/**
|
||||
@@ -33,7 +34,7 @@ interface DataSource {
|
||||
|
||||
fun readDouble() = Double.fromBits(readI64())
|
||||
|
||||
fun readFloat() = Float.fromBits(readI32()).toFloat()
|
||||
fun readFloat() = Float.fromBits(readI32())
|
||||
|
||||
fun readSmartUInt(): UInt = Smartint.decodeUnsigned(this).toUInt()
|
||||
fun readSmartInt(): Int = Smartint.decodeSigned(this).toInt()
|
||||
@@ -44,7 +45,6 @@ interface DataSource {
|
||||
|
||||
|
||||
}
|
||||
|
||||
fun ByteArray.toDataSource(): DataSource =
|
||||
object : DataSource {
|
||||
var position = 0
|
||||
@@ -59,5 +59,19 @@ fun ByteArray.toDataSource(): DataSource =
|
||||
}
|
||||
}
|
||||
|
||||
fun UByteArray.toDataSource(): DataSource =
|
||||
object : DataSource {
|
||||
var position = 0
|
||||
private set
|
||||
|
||||
override fun readByte(): Byte =
|
||||
if (position < size) this@toDataSource[position++].toByte()
|
||||
else throw DataSource.EndOfData()
|
||||
|
||||
override fun toString(): String {
|
||||
return "ASrc[$position]: ${encodeToHex()}"
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <reified T : Any> DataSource.readNumber(): T = Smartint.decode(this) as T
|
||||
|
||||
Reference in new issue
Block a user