Use buffer functions to read parts of imprecise fraction

This commit is contained in:
DBotThePony 2022-08-17 17:04:38 +07:00
parent 51fa3a012c
commit a5ca7af63e
Signed by: DBot
GPG Key ID: DCC23B5715498507

View File

@ -500,9 +500,8 @@ class ImpreciseFraction @JvmOverloads constructor(whole: BigInteger, decimal: Do
fun write(buff: FriendlyByteBuf) { fun write(buff: FriendlyByteBuf) {
val whole = whole.toByteArray() val whole = whole.toByteArray()
buff.writeByte(whole.size) buff.writeByteArray(whole)
buff.writeBytes(whole) buff.writeDouble(decimal)
buff.writeBytes(longToBytesBE(decimal.toBits()))
} }
override fun toFloat(): Float { override fun toFloat(): Float {
@ -635,14 +634,7 @@ class ImpreciseFraction @JvmOverloads constructor(whole: BigInteger, decimal: Do
@JvmStatic @JvmStatic
fun read(buff: FriendlyByteBuf): ImpreciseFraction { fun read(buff: FriendlyByteBuf): ImpreciseFraction {
val len = unsignedInt(buff.readByte()) return ImpreciseFraction(BigInteger(buff.readByteArray()), buff.readDouble())
val slice = ByteArray(len)
for (i in 0 until len)
slice[i] = buff.readByte()
val bits = bytesToLongBE(buff.readByte(), buff.readByte(), buff.readByte(), buff.readByte(), buff.readByte(), buff.readByte(), buff.readByte(), buff.readByte())
return ImpreciseFraction(BigInteger(slice), Double.fromBits(bits))
} }
} }
} }