diff options
author | Edoardo La Greca | 2025-06-20 16:56:14 +0200 |
---|---|---|
committer | Edoardo La Greca | 2025-06-20 17:07:40 +0200 |
commit | e0b0f08a52f922fb5d18ee5de3f4c020dc8a6f80 (patch) | |
tree | fa897bbeaeac95953725392b220dd2ec8f6206fb | |
parent | c210af76ef32c6e521f2e3a0fc65781098d9cd83 (diff) |
add readInteger and readString to NinePConnection
-rw-r--r-- | src/main/kotlin/NinePConnection.kt | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/src/main/kotlin/NinePConnection.kt b/src/main/kotlin/NinePConnection.kt index 8514ffd..73ab5fa 100644 --- a/src/main/kotlin/NinePConnection.kt +++ b/src/main/kotlin/NinePConnection.kt @@ -1,4 +1,5 @@ import java.io.IOException +import java.math.BigInteger /** * This class represents a 9P connection. It provides a practical implementation with networking to the 9P methods @@ -28,5 +29,39 @@ class NinePConnection(netPackTrans: NetworkPacketTransporter) : NinePTranslator this.npt.close() } + /** + * Read an [nBytes]-long unsigned integer number from the connection. + * + * In 9P, binary numbers (non-textual) are specified in little-endian order (least significant byte first). + * + * @param nBytes The length of the integer number in bytes. It only works with unsigned numbers strictly greater + * than zero (zero is excluded). + * @return The number's value. + * @throws IllegalArgumentException if [nBytes] is zero or negative. + */ + private fun readInteger(nBytes: Int): BigInteger { + require(nBytes > 0) + val bytes = this.npt.receiveFixed(nBytes.toULong()) + var number: BigInteger = BigInteger.valueOf(0) + for (i in 0..<bytes.size) { + number += bytes[i].toInt().toBigInteger().shl(i) + } + + return number + } + + /** + * Read a string from 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. + * + * @return The string. + */ + private fun readString(): String { + val length = readInteger(2).toInt() + return String(this.npt.receiveFixed(length.toULong()).toByteArray()) + } + // TODO: implement methods from NinePTranslator }
\ No newline at end of file |