KStarbound/src/main/kotlin/ru/dbotthepony/kstarbound/util/LEStream.kt
2022-09-11 01:10:40 +07:00

40 lines
970 B
Kotlin

package ru.dbotthepony.kstarbound.util
import java.io.InputStream
import java.io.OutputStream
fun OutputStream.writeLEInt(value: Int) {
write(value)
write(value ushr 8)
write(value ushr 16)
write(value ushr 24)
}
fun OutputStream.writeLEShort(value: Int) {
write(value)
write(value ushr 8)
}
fun OutputStream.writeLELong(value: Long) {
writeLEInt(value.toInt())
writeLEInt((value ushr 32).toInt())
}
fun OutputStream.writeLEFloat(value: Float) = writeLEInt(value.toBits())
fun OutputStream.writeLEDouble(value: Double) = writeLELong(value.toBits())
fun InputStream.readLEShort(): Int {
return read() or (read() shl 8)
}
fun InputStream.readLEInt(): Int {
return read() or (read() shl 8) or (read() shl 16) or (read() shl 24)
}
fun InputStream.readLELong(): Long {
return readLEInt().toLong() or (readLEInt().toLong() shl 32)
}
fun InputStream.readLEFloat() = Float.fromBits(readLEInt())
fun InputStream.readLEDouble() = Double.fromBits(readLELong())