diff options
author | Edoardo La Greca | 2025-08-07 14:49:46 +0200 |
---|---|---|
committer | Edoardo La Greca | 2025-08-07 14:49:46 +0200 |
commit | 76ccef2cb3fbe8c991f3db6632e99598983e313b (patch) | |
tree | f3b655e6e96d541cd4d438d86bc5ac00503cd127 /src | |
parent | aa40ce28f6001947208d308c9fcb96940788a243 (diff) |
move writeInteger and writeString to OutMessage companion object
Diffstat (limited to 'src')
-rw-r--r-- | src/main/kotlin/OutMessage.kt | 64 |
1 files changed, 33 insertions, 31 deletions
diff --git a/src/main/kotlin/OutMessage.kt b/src/main/kotlin/OutMessage.kt index a792224..bc05784 100644 --- a/src/main/kotlin/OutMessage.kt +++ b/src/main/kotlin/OutMessage.kt @@ -61,37 +61,6 @@ class OutMessage(val type: NinePMessageType, val tag: UShort, val fieldNames: Li } } - // TODO: Add size that the value is required to fit in - /** - * Write an integer number to the connection. - * - * In 9P, binary numbers (non-textual) are specified in little-endian order (least significant byte first). - * - * @param tl The networking API. - * @param value The number's value. - */ - private fun writeInteger(tl: TransportLayer, value: BigInteger) { - val bytes = value.toByteArray() - tl.transmit(Array(bytes.size) { i -> bytes[i].toUByte() }) - } - - /** - * Write a string to the connection. - * - * In 9P, strings are represented as a 2-byte integer (the string's size) followed by the actual UTF-8 string. The - * null terminator is forbidden in 9P messages. - * - * @param tl The networking API. - * @param value The string. - * @throws IllegalArgumentException if the value of the string's size does not fit into 2 bytes. - */ - private fun writeString(tl: TransportLayer, value: String) { - require(value.length <= 2.0.pow(16.0) - 1) - writeInteger(tl, value.length.toBigInteger()) - val bytes = value.toByteArray() - tl.transmit(Array(bytes.size) { i -> bytes[i].toUByte() }) - } - /** * Write the message size and type. * @@ -110,4 +79,37 @@ class OutMessage(val type: NinePMessageType, val tag: UShort, val fieldNames: Li fun size(): UInt { return 4u + 1u + 2u + this.insecInts.sumOf { this.fieldValuesInt[it]!!.bitLength().toUInt() } + this.insecStrs.sumOf { 2u + this.fieldValuesStr[it]!!.length.toUInt() } + this.insecRaws.sumOf { this.fieldValuesRaw[it]!!.size.toUInt() } } + + companion object { + // TODO: Add size that the value is required to fit in + /** + * Write an integer number to the connection. + * + * In 9P, binary numbers (non-textual) are specified in little-endian order (least significant byte first). + * + * @param tl The networking API. + * @param value The number's value. + */ + private fun writeInteger(tl: TransportLayer, value: BigInteger) { + val bytes = value.toByteArray() + tl.transmit(Array(bytes.size) { i -> bytes[i].toUByte() }) + } + + /** + * Write a string to the connection. + * + * In 9P, strings are represented as a 2-byte integer (the string's size) followed by the actual UTF-8 string. The + * null terminator is forbidden in 9P messages. + * + * @param tl The networking API. + * @param value The string. + * @throws IllegalArgumentException if the value of the string's size does not fit into 2 bytes. + */ + private fun writeString(tl: TransportLayer, value: String) { + require(value.length <= 2.0.pow(16.0) - 1) + writeInteger(tl, value.length.toBigInteger()) + val bytes = value.toByteArray() + tl.transmit(Array(bytes.size) { i -> bytes[i].toUByte() }) + } + } }
\ No newline at end of file |