diff --git a/src/main/kotlin/ch/dissem/msgpack/types/MPMap.kt b/src/main/kotlin/ch/dissem/msgpack/types/MPMap.kt index 87f855f..4c70bf0 100644 --- a/src/main/kotlin/ch/dissem/msgpack/types/MPMap.kt +++ b/src/main/kotlin/ch/dissem/msgpack/types/MPMap.kt @@ -5,8 +5,6 @@ import java.io.IOException import java.io.InputStream import java.io.OutputStream import java.nio.ByteBuffer -import java.util.* -import kotlin.collections.LinkedHashMap /** * Representation of a msgpack encoded map. It is recommended to use a [LinkedHashMap] to ensure the order @@ -21,14 +19,18 @@ data class MPMap, V : MPType<*>>(override val value: MutableMap { + out.write(0x80 + size) + } + size < 65536 -> { + out.write(0xDE) + out.write(ByteBuffer.allocate(2).putShort(size.toShort()).array()) + } + else -> { + out.write(0xDF) + out.write(ByteBuffer.allocate(4).putInt(size).array()) + } } for ((key, value1) in value) { key.pack(out) @@ -68,24 +70,15 @@ data class MPMap, V : MPType<*>>(override val value: MutableMap + "$indent ${Utils.toJson(i.key, "$indent ")}: ${Utils.toJson(i.value, "$indent ")}" } - result.append(indent).append("}") - return result.toString() } override fun toJson(): String { @@ -100,15 +93,14 @@ data class MPMap, V : MPType<*>>(override val value: MutableMap, MPType<*>> { - val size: Int - when { - firstByte and 0xF0 == 0x80 -> size = firstByte and 0x0F - firstByte == 0xDE -> size = input.read() shl 8 or input.read() - firstByte == 0xDF -> size = input.read() shl 24 or (input.read() shl 16) or (input.read() shl 8) or input.read() + val size: Int = when { + firstByte and 0xF0 == 0x80 -> firstByte and 0x0F + firstByte == 0xDE -> input.read() shl 8 or input.read() + firstByte == 0xDF -> input.read() shl 24 or (input.read() shl 16) or (input.read() shl 8) or input.read() else -> throw IllegalArgumentException(String.format("Unexpected first byte 0x%02x", firstByte)) } val map = LinkedHashMap, MPType<*>>() - for (i in 0..size - 1) { + for (i in 0 until size) { val key = reader.read(input) val value = reader.read(input) map.put(key, value) diff --git a/src/test/kotlin/ch/dissem/msgpack/ReaderTest.kt b/src/test/kotlin/ch/dissem/msgpack/ReaderTest.kt index 000e015..b482a17 100644 --- a/src/test/kotlin/ch/dissem/msgpack/ReaderTest.kt +++ b/src/test/kotlin/ch/dissem/msgpack/ReaderTest.kt @@ -32,7 +32,7 @@ class ReaderTest { fun `ensure demo json is parsed correctly`() { val read = Reader.read(stream("demo.mp")) assertThat(read, instanceOf(MPMap::class.java)) - assertThat(read.toString(), equalTo(string("demo.json"))) + assertThat(read.toJson(), equalTo(string("demo.json"))) } @Test @@ -63,7 +63,7 @@ class ReaderTest { assertThat(read, instanceOf(MPArray::class.java)) @Suppress("UNCHECKED_CAST") assertThat(read as MPArray>, equalTo(array)) - assertThat(read.toJson(), equalTo("[\n AQMDBw==,\n false,\n 3.141592653589793,\n 1.5,\n 42,\n {\n },\n null,\n \"yay! 🤓\"\n]")) + assertThat(read.toJson(), equalTo("[\n AQMDBw==,\n false,\n 3.141592653589793,\n 1.5,\n 42,\n {},\n null,\n \"yay! 🤓\"\n]")) } @Test @@ -222,7 +222,7 @@ class ReaderTest { private fun ensureMapIsEncodedAndDecodedCorrectly(size: Int) { val nil = MPNil val map = HashMap(size) - for (i in 0..size - 1) { + for (i in 0 until size) { map.put(i.mp, nil) } val value = MPMap(map) @@ -236,7 +236,7 @@ class ReaderTest { private fun stringWithLength(length: Int): String { val result = StringBuilder(length) - for (i in 0..length - 1) { + for (i in 0 until length) { result.append('a') } return result.toString()