Use variable length ints and longs
This commit is contained in:
parent
db6e1293d5
commit
9a7e9150f9
@ -51,6 +51,7 @@ val ImpreciseFractionValueCodec = StreamCodec(DataInputStream::readImpreciseFrac
|
||||
val BigDecimalValueCodec = StreamCodec(DataInputStream::readBigDecimal, DataOutputStream::writeBigDecimal)
|
||||
val UUIDValueCodec = StreamCodec({ s -> UUID(s.readLong(), s.readLong()) }, { s, v -> s.writeLong(v.mostSignificantBits); s.writeLong(v.leastSignificantBits) })
|
||||
val VarIntValueCodec = StreamCodec(DataInputStream::readVarIntLE, DataOutputStream::writeVarIntLE)
|
||||
val VarLongValueCodec = StreamCodec(DataInputStream::readVarLongLE, DataOutputStream::writeVarLongLE)
|
||||
|
||||
class EnumValueCodec<V : Enum<V>>(private val clazz: Class<out V>) : IStreamCodec<V> {
|
||||
private val values = clazz.enumConstants
|
||||
@ -131,6 +132,7 @@ fun OutputStream.writeFloat(value: Float) = writeInt(value.toBits())
|
||||
fun InputStream.readFloat() = Float.fromBits(readInt())
|
||||
fun OutputStream.writeDouble(value: Double) = writeLong(value.toBits())
|
||||
fun InputStream.readDouble() = Double.fromBits(readLong())
|
||||
|
||||
fun InputStream.readVarIntLE(): Int {
|
||||
var result = 0
|
||||
var read = read()
|
||||
@ -159,6 +161,34 @@ fun OutputStream.writeVarIntLE(value: Int) {
|
||||
write(written)
|
||||
}
|
||||
|
||||
fun InputStream.readVarLongLE(): Long {
|
||||
var result = 0L
|
||||
var read = read()
|
||||
var i = 0
|
||||
|
||||
while (read and 128 != 0) {
|
||||
result = result or ((read and 127) shl i).toLong()
|
||||
read = read()
|
||||
i += 7
|
||||
}
|
||||
|
||||
result = result or ((read and 127) shl i).toLong()
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
fun OutputStream.writeVarLongLE(value: Long) {
|
||||
require(value >= 0L) { "Negative number provided: $value" }
|
||||
var written = value
|
||||
|
||||
while (written >= 128) {
|
||||
write(((written and 127) or 128).toInt())
|
||||
written = written ushr 7
|
||||
}
|
||||
|
||||
write(written.toInt())
|
||||
}
|
||||
|
||||
private data class IndexedStreamCodec<T>(
|
||||
val condition: Predicate<Any?>,
|
||||
val id: Int,
|
||||
@ -185,8 +215,8 @@ private val codecs: List<IndexedStreamCodec<*>> = com.google.common.collect.Immu
|
||||
it.add(IndexedStreamCodec(codecID++, BooleanValueCodec))
|
||||
it.add(IndexedStreamCodec(codecID++, ByteValueCodec))
|
||||
it.add(IndexedStreamCodec(codecID++, ShortValueCodec))
|
||||
it.add(IndexedStreamCodec(codecID++, IntValueCodec))
|
||||
it.add(IndexedStreamCodec(codecID++, LongValueCodec))
|
||||
it.add(IndexedStreamCodec(codecID++, VarIntValueCodec))
|
||||
it.add(IndexedStreamCodec(codecID++, VarLongValueCodec))
|
||||
it.add(IndexedStreamCodec(codecID++, FloatValueCodec))
|
||||
it.add(IndexedStreamCodec(codecID++, DoubleValueCodec))
|
||||
it.add(IndexedStreamCodec(codecID++, ItemStackValueCodec))
|
||||
|
@ -98,7 +98,7 @@ class FieldSynchronizer {
|
||||
getter: FieldGetter<Long>? = null,
|
||||
setter: FieldSetter<Long>? = null,
|
||||
): Field<Long> {
|
||||
return Field(value, LongValueCodec, getter, setter)
|
||||
return Field(value, VarLongValueCodec, getter, setter)
|
||||
}
|
||||
|
||||
fun float(
|
||||
@ -129,14 +129,6 @@ class FieldSynchronizer {
|
||||
value: Int = 0,
|
||||
getter: FieldGetter<Int>? = null,
|
||||
setter: FieldSetter<Int>? = null,
|
||||
): Field<Int> {
|
||||
return Field(value, IntValueCodec, getter, setter)
|
||||
}
|
||||
|
||||
fun varInt(
|
||||
value: Int = 0,
|
||||
getter: FieldGetter<Int>? = null,
|
||||
setter: FieldSetter<Int>? = null,
|
||||
): Field<Int> {
|
||||
return Field(value, VarIntValueCodec, getter, setter)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user