Add length param

This commit is contained in:
Him188 2020-01-30 21:11:17 +08:00
parent db4954bae1
commit c378dcb0f4
2 changed files with 12 additions and 8 deletions

View File

@ -754,8 +754,8 @@ class Jce private constructor(private val charset: JceCharset, context: SerialMo
return dumpAsPacket(serializer, obj).readBytes()
}
fun <T> load(deserializer: DeserializationStrategy<T>, packet: ByteReadPacket): T {
packet.readIoBuffer().withUse {
fun <T> load(deserializer: DeserializationStrategy<T>, packet: ByteReadPacket, length: Int = packet.remaining.toInt()): T {
packet.readIoBuffer(n = length).withUse {
val decoder = JceDecoder(JceInput(this))
return decoder.decode(deserializer)
}

View File

@ -22,8 +22,12 @@ fun <T : JceStruct> BytePacketBuilder.writeJceStruct(serializer: SerializationSt
this.writePacket(Jce.byCharSet(charset).dumpAsPacket(serializer, struct))
}
fun <T : JceStruct> ByteReadPacket.readRemainingAsJceStruct(serializer: DeserializationStrategy<T>, charset: JceCharset = JceCharset.UTF8): T {
return Jce.byCharSet(charset).load(serializer, this)
fun <T : JceStruct> ByteReadPacket.readRemainingAsJceStruct(
serializer: DeserializationStrategy<T>,
charset: JceCharset = JceCharset.UTF8,
length: Int = this.remaining.toInt()
): T {
return Jce.byCharSet(charset).load(serializer, this, length)
}
/**
@ -33,7 +37,7 @@ fun <T : JceStruct> ByteReadPacket.decodeUniPacket(deserializer: Deserialization
return decodeUniRequestPacketAndDeserialize(name) {
it.read {
discardExact(1)
this.readRemainingAsJceStruct(deserializer)
this.readRemainingAsJceStruct(deserializer, length = (this.remaining - 1).toInt())
}
}
}
@ -45,7 +49,7 @@ fun <T : ProtoBuf> ByteReadPacket.decodeUniPacket(deserializer: DeserializationS
return decodeUniRequestPacketAndDeserialize(name) {
it.read {
discardExact(1)
this.readRemainingAsProtoBuf(deserializer)
this.readRemainingAsProtoBuf(deserializer, (this.remaining - 1).toInt())
}
}
}
@ -89,8 +93,8 @@ fun <T : ProtoBuf> ByteArray.loadAs(deserializer: DeserializationStrategy<T>): T
/**
* load
*/
fun <T : ProtoBuf> Input.readRemainingAsProtoBuf(serializer: DeserializationStrategy<T>): T {
return ProtoBufWithNullableSupport.load(serializer, this.readBytes())
fun <T : ProtoBuf> ByteReadPacket.readRemainingAsProtoBuf(serializer: DeserializationStrategy<T>, length: Int = this.remaining.toInt()): T {
return ProtoBufWithNullableSupport.load(serializer, this.readBytes(length))
}
/**