Author: Adam <git@apiote.xyz>
fix decoding values from BARE which resulted in wrong vehicles’ speed
app/src/main/java/ml/adamsprogs/bimba/departures/Departures.kt | 2 app/src/main/java/xyz/apiote/fruchtfleisch/Reader.kt | 53 ++-
diff --git a/app/src/main/java/ml/adamsprogs/bimba/departures/Departures.kt b/app/src/main/java/ml/adamsprogs/bimba/departures/Departures.kt index e58b4da46855d4bc1a66949f2679fab514847144..328a041df1a7b0809268d5cd76c994b4253aa42c 100644 --- a/app/src/main/java/ml/adamsprogs/bimba/departures/Departures.kt +++ b/app/src/main/java/ml/adamsprogs/bimba/departures/Departures.kt @@ -3,7 +3,6 @@ import android.annotation.SuppressLint import android.content.Context import android.os.Bundle -import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -158,7 +157,6 @@ } } findViewById<TextView>(R.id.speed_text).apply { // todo units - // fixme(shchanyetskiey?) val speed = departure.vehicle.Speed * 1.703 text = "%.3f Vl".format(speed) } diff --git a/app/src/main/java/xyz/apiote/fruchtfleisch/Reader.kt b/app/src/main/java/xyz/apiote/fruchtfleisch/Reader.kt index d6246c44ab04f79f66dccf7913a02bdc2ad3a665..385a0005f01e6003f91deca497a3f10a63a15c2d 100644 --- a/app/src/main/java/xyz/apiote/fruchtfleisch/Reader.kt +++ b/app/src/main/java/xyz/apiote/fruchtfleisch/Reader.kt @@ -1,8 +1,9 @@ package xyz.apiote.fruchtfleisch -import java.io.DataInputStream import java.io.EOFException import java.io.InputStream +import java.lang.Double.longBitsToDouble +import java.lang.Float.intBitsToFloat class Reader(private val stream: InputStream) { fun readUInt(): ULong { @@ -45,39 +46,53 @@ return b.toUByte() } fun readU16(): UShort { - var result = 0U - for (i in 0 until 2) { - result = result or (readU8().toUInt().shl(2U.shl(i).toInt())) - } - return result.toUShort() + val b1 = readU8() + val b2 = readU8() + return (b2.toUInt().shl(8) or b1.toUInt()).toUShort() } fun readU32(): UInt { - var result = 0U - for (i in 0 until 4) { - result = result or (readU8().toUInt().shl(2U.shl(i).toInt())) - } - return result + val b1 = readU8() + val b2 = readU8() + val b3 = readU8() + val b4 = readU8() + return b4.toUInt().shl(24) or b3.toUInt().shl(16) or b2.toUInt().shl(8) or b1.toUInt() } - fun readU64(): UShort { - var result = 0U - for (i in 0 until 8) { - result = result or (readU8().toUInt().shl(2U.shl(i).toInt())) - } - return result.toUShort() + fun readU64(): ULong { + val b1 = readU8() + val b2 = readU8() + val b3 = readU8() + val b4 = readU8() + val b5 = readU8() + val b6 = readU8() + val b7 = readU8() + val b8 = readU8() + return b8.toULong().shl(56) or b7.toULong().shl(48) or b6.toULong().shl(40) or b5.toULong().shl(32) or b4.toULong().shl(24) or b3.toULong().shl(16) or b2.toULong().shl(8) or b1.toULong() } fun readI8(): Byte { return readU8().toByte() } + fun readI16(): Short { + return readU16().toShort() + } + + fun readI32(): Int { + return readU32().toInt() + } + + fun readI64(): Long { + return readU64().toLong() + } + fun readFloat32(): Float { - return DataInputStream(stream).readFloat() + return intBitsToFloat(readI32()) } fun readFloat64(): Double { - return DataInputStream(stream).readDouble() + return longBitsToDouble(readI64()) } fun readData(length: UByte): ByteArray {