Migrated to Kotlin
This commit is contained in:
57
src/main/kotlin/ch/dissem/msgpack/Reader.kt
Normal file
57
src/main/kotlin/ch/dissem/msgpack/Reader.kt
Normal file
@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright 2017 Christian Basler
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package ch.dissem.msgpack
|
||||
|
||||
import ch.dissem.msgpack.types.*
|
||||
import java.io.IOException
|
||||
import java.io.InputStream
|
||||
|
||||
/**
|
||||
* Reads MPType object from an [InputStream].
|
||||
*/
|
||||
object Reader {
|
||||
private val unpackers = mutableListOf<MPType.Unpacker<*>>()
|
||||
|
||||
init {
|
||||
unpackers.add(MPNil.Unpacker())
|
||||
unpackers.add(MPBoolean.Unpacker())
|
||||
unpackers.add(MPInteger.Unpacker())
|
||||
unpackers.add(MPFloat.Unpacker())
|
||||
unpackers.add(MPString.Unpacker())
|
||||
unpackers.add(MPBinary.Unpacker())
|
||||
unpackers.add(MPMap.Unpacker(this))
|
||||
unpackers.add(MPArray.Unpacker(this))
|
||||
}
|
||||
|
||||
/**
|
||||
* Register your own extensions. The last registered unpacker always takes precedence.
|
||||
*/
|
||||
fun register(unpacker: MPType.Unpacker<*>) {
|
||||
unpackers.add(0, unpacker)
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun read(input: InputStream): MPType<*> {
|
||||
val firstByte = input.read()
|
||||
unpackers
|
||||
.firstOrNull { it.doesUnpack(firstByte) }
|
||||
?.let {
|
||||
return it.unpack(firstByte, input)
|
||||
}
|
||||
throw IOException(String.format("Unsupported input, no reader for 0x%02x", firstByte))
|
||||
}
|
||||
}
|
145
src/main/kotlin/ch/dissem/msgpack/types/MPArray.kt
Normal file
145
src/main/kotlin/ch/dissem/msgpack/types/MPArray.kt
Normal file
@ -0,0 +1,145 @@
|
||||
/*
|
||||
* Copyright 2017 Christian Basler
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package ch.dissem.msgpack.types
|
||||
|
||||
import ch.dissem.msgpack.Reader
|
||||
import java.io.IOException
|
||||
import java.io.InputStream
|
||||
import java.io.OutputStream
|
||||
import java.nio.ByteBuffer
|
||||
|
||||
/**
|
||||
* Representation of a msgpack encoded array. Uses a list to represent data internally, and implements the [List]
|
||||
* interface for your convenience.
|
||||
|
||||
* @param <E> content type
|
||||
*/
|
||||
data class MPArray<E : MPType<*>>(override val value: MutableList<E> = mutableListOf()) : MPType<MutableList<E>>, MutableList<E> {
|
||||
|
||||
@SafeVarargs
|
||||
constructor(vararg objects: E) : this(mutableListOf(*objects))
|
||||
|
||||
|
||||
@Throws(IOException::class)
|
||||
override fun pack(out: OutputStream) {
|
||||
val size = value.size
|
||||
when {
|
||||
size < 16 -> out.write(144 + size)
|
||||
size < 65536 -> {
|
||||
out.write(0xDC)
|
||||
out.write(ByteBuffer.allocate(2).putShort(size.toShort()).array())
|
||||
}
|
||||
else -> {
|
||||
out.write(0xDD)
|
||||
out.write(ByteBuffer.allocate(4).putInt(size).array())
|
||||
}
|
||||
}
|
||||
for (o in value) {
|
||||
o.pack(out)
|
||||
}
|
||||
}
|
||||
|
||||
override val size: Int
|
||||
get() = value.size
|
||||
|
||||
override fun isEmpty(): Boolean {
|
||||
return value.isEmpty()
|
||||
}
|
||||
|
||||
override fun contains(element: E) = value.contains(element)
|
||||
|
||||
override fun iterator() = value.iterator()
|
||||
|
||||
override fun add(element: E) = value.add(element)
|
||||
|
||||
override fun remove(element: E) = value.remove(element)
|
||||
|
||||
override fun containsAll(elements: Collection<E>) = value.containsAll(elements)
|
||||
|
||||
override fun addAll(elements: Collection<E>) = value.addAll(elements)
|
||||
|
||||
override fun addAll(index: Int, elements: Collection<E>) = value.addAll(index, elements)
|
||||
|
||||
override fun removeAll(elements: Collection<E>) = value.removeAll(elements)
|
||||
|
||||
override fun retainAll(elements: Collection<E>) = value.retainAll(elements)
|
||||
|
||||
override fun clear() = value.clear()
|
||||
|
||||
override fun get(index: Int) = value[index]
|
||||
|
||||
override operator fun set(index: Int, element: E) = value.set(index, element)
|
||||
|
||||
override fun add(index: Int, element: E) = value.add(index, element)
|
||||
|
||||
override fun removeAt(index: Int) = value.removeAt(index)
|
||||
|
||||
override fun indexOf(element: E) = value.indexOf(element)
|
||||
|
||||
override fun lastIndexOf(element: E) = value.lastIndexOf(element)
|
||||
|
||||
override fun listIterator() = value.listIterator()
|
||||
|
||||
override fun listIterator(index: Int) = value.listIterator(index)
|
||||
|
||||
override fun subList(fromIndex: Int, toIndex: Int) = value.subList(fromIndex, toIndex)
|
||||
|
||||
override fun toString() = toJson()
|
||||
|
||||
override fun toJson() = toJson("")
|
||||
|
||||
internal fun toJson(indent: String): String {
|
||||
val result = StringBuilder()
|
||||
result.append("[\n")
|
||||
val iterator = value.iterator()
|
||||
val indent2 = indent + " "
|
||||
while (iterator.hasNext()) {
|
||||
val item = iterator.next()
|
||||
result.append(indent2)
|
||||
result.append(Utils.toJson(item, indent2))
|
||||
if (iterator.hasNext()) {
|
||||
result.append(',')
|
||||
}
|
||||
result.append('\n')
|
||||
}
|
||||
result.append("]")
|
||||
return result.toString()
|
||||
}
|
||||
|
||||
class Unpacker(private val reader: Reader) : MPType.Unpacker<MPArray<*>> {
|
||||
|
||||
override fun doesUnpack(firstByte: Int): Boolean {
|
||||
return firstByte == 0xDC || firstByte == 0xDD || firstByte and 240 == 144
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
override fun unpack(firstByte: Int, input: InputStream): MPArray<MPType<*>> {
|
||||
val size: Int = when {
|
||||
firstByte and 240 == 144 -> firstByte and 15
|
||||
firstByte == 0xDC -> input.read() shl 8 or input.read()
|
||||
firstByte == 0xDD -> 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 list = mutableListOf<MPType<*>>()
|
||||
for (i in 0 until size) {
|
||||
val value = reader.read(input)
|
||||
list.add(value)
|
||||
}
|
||||
return MPArray(list)
|
||||
}
|
||||
}
|
||||
}
|
85
src/main/kotlin/ch/dissem/msgpack/types/MPBinary.kt
Normal file
85
src/main/kotlin/ch/dissem/msgpack/types/MPBinary.kt
Normal file
@ -0,0 +1,85 @@
|
||||
/*
|
||||
* Copyright 2017 Christian Basler
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package ch.dissem.msgpack.types
|
||||
|
||||
import ch.dissem.msgpack.types.Utils.bytes
|
||||
import java.io.IOException
|
||||
import java.io.InputStream
|
||||
import java.io.OutputStream
|
||||
import java.nio.ByteBuffer
|
||||
import java.util.*
|
||||
|
||||
/**
|
||||
* Representation of msgpack encoded binary data a.k.a. byte array.
|
||||
*/
|
||||
data class MPBinary(override val value: ByteArray) : MPType<ByteArray> {
|
||||
|
||||
@Throws(IOException::class)
|
||||
override fun pack(out: OutputStream) {
|
||||
val size = value.size
|
||||
when {
|
||||
size < 256 -> {
|
||||
out.write(0xC4)
|
||||
out.write(size.toByte().toInt())
|
||||
}
|
||||
size < 65536 -> {
|
||||
out.write(0xC5)
|
||||
out.write(ByteBuffer.allocate(2).putShort(size.toShort()).array())
|
||||
}
|
||||
else -> {
|
||||
out.write(0xC6)
|
||||
out.write(ByteBuffer.allocate(4).putInt(size).array())
|
||||
}
|
||||
}
|
||||
out.write(value)
|
||||
}
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (this === other) return true
|
||||
if (other !is MPBinary) return false
|
||||
return Arrays.equals(value, other.value)
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
return Arrays.hashCode(value)
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
return toJson()
|
||||
}
|
||||
|
||||
override fun toJson(): String {
|
||||
return Utils.base64(value)
|
||||
}
|
||||
|
||||
class Unpacker : MPType.Unpacker<MPBinary> {
|
||||
override fun doesUnpack(firstByte: Int): Boolean {
|
||||
return firstByte == 0xC4 || firstByte == 0xC5 || firstByte == 0xC6
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
override fun unpack(firstByte: Int, input: InputStream): MPBinary {
|
||||
val size: Int = when (firstByte) {
|
||||
0xC4 -> input.read()
|
||||
0xC5 -> input.read() shl 8 or input.read()
|
||||
0xC6 -> 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))
|
||||
}
|
||||
return MPBinary(bytes(input, size).array())
|
||||
}
|
||||
}
|
||||
}
|
62
src/main/kotlin/ch/dissem/msgpack/types/MPBoolean.kt
Normal file
62
src/main/kotlin/ch/dissem/msgpack/types/MPBoolean.kt
Normal file
@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright 2017 Christian Basler
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package ch.dissem.msgpack.types
|
||||
|
||||
import java.io.IOException
|
||||
import java.io.InputStream
|
||||
import java.io.OutputStream
|
||||
|
||||
/**
|
||||
* Representation of a msgpack encoded boolean.
|
||||
*/
|
||||
data class MPBoolean(override val value: Boolean) : MPType<Boolean> {
|
||||
|
||||
@Throws(IOException::class)
|
||||
override fun pack(out: OutputStream) {
|
||||
out.write(if (value) {
|
||||
TRUE
|
||||
} else {
|
||||
FALSE
|
||||
})
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
return value.toString()
|
||||
}
|
||||
|
||||
override fun toJson(): String {
|
||||
return value.toString()
|
||||
}
|
||||
|
||||
class Unpacker : MPType.Unpacker<MPBoolean> {
|
||||
|
||||
override fun doesUnpack(firstByte: Int): Boolean {
|
||||
return firstByte == TRUE || firstByte == FALSE
|
||||
}
|
||||
|
||||
override fun unpack(firstByte: Int, input: InputStream) = when (firstByte) {
|
||||
TRUE -> MPBoolean(true)
|
||||
FALSE -> MPBoolean(false)
|
||||
else -> throw IllegalArgumentException(String.format("0xC2 or 0xC3 expected but was 0x%02x", firstByte))
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val FALSE = 0xC2
|
||||
private const val TRUE = 0xC3
|
||||
}
|
||||
}
|
71
src/main/kotlin/ch/dissem/msgpack/types/MPFloat.kt
Normal file
71
src/main/kotlin/ch/dissem/msgpack/types/MPFloat.kt
Normal file
@ -0,0 +1,71 @@
|
||||
/*
|
||||
* Copyright 2017 Christian Basler
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package ch.dissem.msgpack.types
|
||||
|
||||
import ch.dissem.msgpack.types.Utils.bytes
|
||||
import java.io.IOException
|
||||
import java.io.InputStream
|
||||
import java.io.OutputStream
|
||||
import java.nio.ByteBuffer
|
||||
|
||||
/**
|
||||
* Representation of a msgpack encoded float32 or float64 number.
|
||||
*/
|
||||
data class MPFloat(override val value: Double, val precision: Precision) : MPType<Double> {
|
||||
|
||||
enum class Precision {
|
||||
FLOAT32, FLOAT64
|
||||
}
|
||||
|
||||
constructor(value: Float) : this(value.toDouble(), Precision.FLOAT32)
|
||||
constructor(value: Double) : this(value, Precision.FLOAT64)
|
||||
|
||||
@Throws(IOException::class)
|
||||
override fun pack(out: OutputStream) {
|
||||
when (precision) {
|
||||
MPFloat.Precision.FLOAT32 -> {
|
||||
out.write(0xCA)
|
||||
out.write(ByteBuffer.allocate(4).putFloat(value.toFloat()).array())
|
||||
}
|
||||
MPFloat.Precision.FLOAT64 -> {
|
||||
out.write(0xCB)
|
||||
out.write(ByteBuffer.allocate(8).putDouble(value).array())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
return value.toString()
|
||||
}
|
||||
|
||||
override fun toJson(): String {
|
||||
return value.toString()
|
||||
}
|
||||
|
||||
class Unpacker : MPType.Unpacker<MPFloat> {
|
||||
override fun doesUnpack(firstByte: Int): Boolean {
|
||||
return firstByte == 0xCA || firstByte == 0xCB
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
override fun unpack(firstByte: Int, input: InputStream) = when (firstByte) {
|
||||
0xCA -> MPFloat(bytes(input, 4).float)
|
||||
0xCB -> MPFloat(bytes(input, 8).double)
|
||||
else -> throw IllegalArgumentException(String.format("Unexpected first byte 0x%02x", firstByte))
|
||||
}
|
||||
}
|
||||
}
|
116
src/main/kotlin/ch/dissem/msgpack/types/MPInteger.kt
Normal file
116
src/main/kotlin/ch/dissem/msgpack/types/MPInteger.kt
Normal file
@ -0,0 +1,116 @@
|
||||
/*
|
||||
* Copyright 2017 Christian Basler
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package ch.dissem.msgpack.types
|
||||
|
||||
import ch.dissem.msgpack.types.Utils.bytes
|
||||
import java.io.IOException
|
||||
import java.io.InputStream
|
||||
import java.io.OutputStream
|
||||
import java.nio.ByteBuffer
|
||||
|
||||
/**
|
||||
* Representation of a msgpack encoded integer. The encoding is automatically selected according to the value's size.
|
||||
* Uses long due to the fact that the msgpack integer implementation may contain up to 64 bit numbers, corresponding
|
||||
* to Java long values. Also note that uint64 values may be too large for signed long (thanks Java for not supporting
|
||||
* unsigned values) and end in a negative value.
|
||||
*/
|
||||
data class MPInteger(override val value: Long) : MPType<Long> {
|
||||
|
||||
constructor(value: Byte) : this(value.toLong())
|
||||
constructor(value: Short) : this(value.toLong())
|
||||
constructor(value: Int) : this(value.toLong())
|
||||
|
||||
@Throws(IOException::class)
|
||||
override fun pack(out: OutputStream) {
|
||||
when (value) {
|
||||
in -32..127 -> out.write(value.toInt())
|
||||
in 0..0xFF -> {
|
||||
out.write(0xCC)
|
||||
out.write(value.toInt())
|
||||
}
|
||||
in 0..0xFFFF -> {
|
||||
out.write(0xCD)
|
||||
out.write(ByteBuffer.allocate(2).putShort(value.toShort()).array())
|
||||
}
|
||||
in 0..0xFFFFFFFFL -> {
|
||||
out.write(0xCE)
|
||||
out.write(ByteBuffer.allocate(4).putInt(value.toInt()).array())
|
||||
}
|
||||
in 0..Long.MAX_VALUE -> {
|
||||
out.write(0xCF)
|
||||
out.write(ByteBuffer.allocate(8).putLong(value).array())
|
||||
}
|
||||
in Byte.MIN_VALUE..0 -> {
|
||||
out.write(0xD0)
|
||||
out.write(ByteBuffer.allocate(1).put(value.toByte()).array())
|
||||
}
|
||||
in Short.MIN_VALUE..0 -> {
|
||||
out.write(0xD1)
|
||||
out.write(ByteBuffer.allocate(2).putShort(value.toShort()).array())
|
||||
}
|
||||
in Int.MIN_VALUE..0 -> {
|
||||
out.write(0xD2)
|
||||
out.write(ByteBuffer.allocate(4).putInt(value.toInt()).array())
|
||||
}
|
||||
in Long.MIN_VALUE..0 -> {
|
||||
out.write(0xD3)
|
||||
out.write(ByteBuffer.allocate(8).putLong(value).array())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun toString() = value.toString()
|
||||
|
||||
override fun toJson() = value.toString()
|
||||
|
||||
class Unpacker : MPType.Unpacker<MPInteger> {
|
||||
override fun doesUnpack(firstByte: Int): Boolean {
|
||||
return when (firstByte) {
|
||||
0xCC, 0xCD, 0xCE, 0xCF, 0xD0, 0xD1, 0xD2, 0xD3 -> true
|
||||
else -> firstByte and 128 == 0 || firstByte and 224 == 224
|
||||
}
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
override fun unpack(firstByte: Int, input: InputStream): MPInteger {
|
||||
if (firstByte and 128 == 0 || firstByte and 224 == 224) {
|
||||
// The cast needs to happen for the MPInteger to have the correct sign
|
||||
return MPInteger(firstByte.toByte())
|
||||
} else {
|
||||
return when (firstByte) {
|
||||
0xCC -> MPInteger(readLong(input, 1))
|
||||
0xCD -> MPInteger(readLong(input, 2))
|
||||
0xCE -> MPInteger(readLong(input, 4))
|
||||
0xCF -> MPInteger(readLong(input, 8))
|
||||
0xD0 -> MPInteger(bytes(input, 1).get())
|
||||
0xD1 -> MPInteger(bytes(input, 2).short)
|
||||
0xD2 -> MPInteger(bytes(input, 4).int)
|
||||
0xD3 -> MPInteger(bytes(input, 8).long)
|
||||
else -> throw IllegalArgumentException(String.format("Unexpected first byte 0x%02x", firstByte))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun readLong(input: InputStream, length: Int): Long {
|
||||
var value: Long = 0
|
||||
for (i in 0 until length) {
|
||||
value = value shl 8 or input.read().toLong()
|
||||
}
|
||||
return value
|
||||
}
|
||||
}
|
||||
}
|
119
src/main/kotlin/ch/dissem/msgpack/types/MPMap.kt
Normal file
119
src/main/kotlin/ch/dissem/msgpack/types/MPMap.kt
Normal file
@ -0,0 +1,119 @@
|
||||
package ch.dissem.msgpack.types
|
||||
|
||||
import ch.dissem.msgpack.Reader
|
||||
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
|
||||
* of entries. For convenience, it also implements the [Map] interface.
|
||||
*
|
||||
* @param <K> key type
|
||||
* @param <V> value type
|
||||
*/
|
||||
data class MPMap<K : MPType<*>, V : MPType<*>>(override val value: MutableMap<K, V> = LinkedHashMap<K, V>()) : MPType<Map<K, V>>, MutableMap<K, V> {
|
||||
|
||||
|
||||
@Throws(IOException::class)
|
||||
override fun pack(out: OutputStream) {
|
||||
val size = value.size
|
||||
if (size < 16) {
|
||||
out.write(0x80 + size)
|
||||
} else if (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)
|
||||
value1.pack(out)
|
||||
}
|
||||
}
|
||||
|
||||
override val size: Int
|
||||
get() = value.size
|
||||
|
||||
override fun isEmpty() = value.isEmpty()
|
||||
|
||||
override fun containsKey(key: K) = value.containsKey(key)
|
||||
|
||||
override fun containsValue(value: V) = this.value.containsValue(value)
|
||||
|
||||
override fun get(key: K) = value[key]
|
||||
|
||||
override fun putIfAbsent(key: K, value: V) = this.value.putIfAbsent(key, value)
|
||||
|
||||
override fun put(key: K, value: V): V? = this.value.put(key, value)
|
||||
|
||||
override fun remove(key: K): V? = value.remove(key)
|
||||
|
||||
override fun putAll(from: Map<out K, V>) = value.putAll(from)
|
||||
|
||||
override fun clear() = value.clear()
|
||||
|
||||
override val keys: MutableSet<K>
|
||||
get() = value.keys
|
||||
|
||||
override val values: MutableCollection<V>
|
||||
get() = value.values
|
||||
|
||||
override val entries: MutableSet<MutableMap.MutableEntry<K, V>>
|
||||
get() = value.entries
|
||||
|
||||
override fun toString() = toJson()
|
||||
|
||||
internal fun toJson(indent: String): String {
|
||||
val result = StringBuilder()
|
||||
result.append("{\n")
|
||||
val iterator = value.entries.iterator()
|
||||
val indent2 = indent + " "
|
||||
while (iterator.hasNext()) {
|
||||
val item = iterator.next()
|
||||
result.append(indent2)
|
||||
result.append(Utils.toJson(item.key, indent2))
|
||||
result.append(": ")
|
||||
result.append(Utils.toJson(item.value, indent2))
|
||||
if (iterator.hasNext()) {
|
||||
result.append(',')
|
||||
}
|
||||
result.append('\n')
|
||||
}
|
||||
result.append(indent).append("}")
|
||||
return result.toString()
|
||||
}
|
||||
|
||||
override fun toJson(): String {
|
||||
return toJson("")
|
||||
}
|
||||
|
||||
class Unpacker(private val reader: Reader) : MPType.Unpacker<MPMap<*, *>> {
|
||||
|
||||
override fun doesUnpack(firstByte: Int): Boolean {
|
||||
return firstByte == 0xDE || firstByte == 0xDF || firstByte and 0xF0 == 0x80
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
override fun unpack(firstByte: Int, input: InputStream): MPMap<MPType<*>, 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()
|
||||
else -> throw IllegalArgumentException(String.format("Unexpected first byte 0x%02x", firstByte))
|
||||
}
|
||||
val map = LinkedHashMap<MPType<*>, MPType<*>>()
|
||||
for (i in 0..size - 1) {
|
||||
val key = reader.read(input)
|
||||
val value = reader.read(input)
|
||||
map.put(key, value)
|
||||
}
|
||||
return MPMap(map)
|
||||
}
|
||||
}
|
||||
}
|
50
src/main/kotlin/ch/dissem/msgpack/types/MPNil.kt
Normal file
50
src/main/kotlin/ch/dissem/msgpack/types/MPNil.kt
Normal file
@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright 2017 Christian Basler
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package ch.dissem.msgpack.types
|
||||
|
||||
import java.io.IOException
|
||||
import java.io.InputStream
|
||||
import java.io.OutputStream
|
||||
|
||||
/**
|
||||
* Representation of msgpack encoded nil / null.
|
||||
*/
|
||||
object MPNil : MPType<Void?> {
|
||||
|
||||
override val value: Void? = null
|
||||
|
||||
@Throws(IOException::class)
|
||||
override fun pack(out: OutputStream) = out.write(NIL)
|
||||
|
||||
override fun toString() = "null"
|
||||
|
||||
override fun toJson() = "null"
|
||||
|
||||
class Unpacker : MPType.Unpacker<MPNil> {
|
||||
|
||||
override fun doesUnpack(firstByte: Int) = firstByte == NIL
|
||||
|
||||
override fun unpack(firstByte: Int, input: InputStream): MPNil {
|
||||
return when (firstByte) {
|
||||
NIL -> MPNil
|
||||
else -> throw IllegalArgumentException(String.format("0xC0 expected but was 0x%02x", firstByte))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private val NIL = 0xC0
|
||||
}
|
135
src/main/kotlin/ch/dissem/msgpack/types/MPString.kt
Normal file
135
src/main/kotlin/ch/dissem/msgpack/types/MPString.kt
Normal file
@ -0,0 +1,135 @@
|
||||
/*
|
||||
* Copyright 2017 Christian Basler
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package ch.dissem.msgpack.types
|
||||
|
||||
import ch.dissem.msgpack.types.Utils.bytes
|
||||
import java.io.IOException
|
||||
import java.io.InputStream
|
||||
import java.io.OutputStream
|
||||
import java.nio.ByteBuffer
|
||||
import java.nio.charset.Charset
|
||||
|
||||
/**
|
||||
* Representation of a msgpack encoded string. The encoding is automatically selected according to the string's length.
|
||||
*
|
||||
* The default encoding is UTF-8.
|
||||
*/
|
||||
data class MPString(override val value: String) : MPType<String>, CharSequence {
|
||||
|
||||
@Throws(IOException::class)
|
||||
override fun pack(out: OutputStream) {
|
||||
val bytes = value.toByteArray(encoding)
|
||||
val size = bytes.size
|
||||
when {
|
||||
size < 32 -> out.write(FIXSTR_PREFIX + size)
|
||||
size < STR8_LIMIT -> {
|
||||
out.write(STR8_PREFIX)
|
||||
out.write(size)
|
||||
}
|
||||
size < STR16_LIMIT -> {
|
||||
out.write(STR16_PREFIX)
|
||||
out.write(ByteBuffer.allocate(2).putShort(size.toShort()).array())
|
||||
}
|
||||
else -> {
|
||||
out.write(STR32_PREFIX)
|
||||
out.write(ByteBuffer.allocate(4).putInt(size).array())
|
||||
}
|
||||
}
|
||||
out.write(bytes)
|
||||
}
|
||||
|
||||
override val length: Int = value.length
|
||||
|
||||
override fun get(index: Int): Char {
|
||||
return value[index]
|
||||
}
|
||||
|
||||
override fun subSequence(startIndex: Int, endIndex: Int): CharSequence {
|
||||
return value.subSequence(startIndex, endIndex)
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
return value
|
||||
}
|
||||
|
||||
override fun toJson(): String {
|
||||
val result = StringBuilder(value.length + 4)
|
||||
result.append('"')
|
||||
value.forEach {
|
||||
when (it) {
|
||||
'\\', '"', '/' -> result.append('\\').append(it)
|
||||
'\b' -> result.append("\\b")
|
||||
'\t' -> result.append("\\t")
|
||||
'\n' -> result.append("\\n")
|
||||
'\r' -> result.append("\\r")
|
||||
else -> if (it < ' ') {
|
||||
result.append("\\u")
|
||||
val hex = Integer.toHexString(it.toInt())
|
||||
var j = 0
|
||||
while (j + hex.length < 4) {
|
||||
result.append('0')
|
||||
j++
|
||||
}
|
||||
result.append(hex)
|
||||
} else {
|
||||
result.append(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
result.append('"')
|
||||
return result.toString()
|
||||
}
|
||||
|
||||
class Unpacker : MPType.Unpacker<MPString> {
|
||||
override fun doesUnpack(firstByte: Int): Boolean {
|
||||
return firstByte == STR8_PREFIX || firstByte == STR16_PREFIX || firstByte == STR32_PREFIX
|
||||
|| firstByte and FIXSTR_PREFIX_FILTER == FIXSTR_PREFIX
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
override fun unpack(firstByte: Int, input: InputStream): MPString {
|
||||
val size: Int = when {
|
||||
firstByte and FIXSTR_PREFIX_FILTER == FIXSTR_PREFIX -> firstByte and FIXSTR_FILTER
|
||||
firstByte == STR8_PREFIX -> input.read()
|
||||
firstByte == STR16_PREFIX -> input.read() shl 8 or input.read()
|
||||
firstByte == STR32_PREFIX -> 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))
|
||||
}
|
||||
return MPString(String(bytes(input, size).array(), encoding))
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val FIXSTR_PREFIX = 160
|
||||
private val FIXSTR_PREFIX_FILTER = 224
|
||||
private val STR8_PREFIX = 0xD9
|
||||
private val STR8_LIMIT = 256
|
||||
private val STR16_PREFIX = 0xDA
|
||||
private val STR16_LIMIT = 65536
|
||||
private val STR32_PREFIX = 0xDB
|
||||
private val FIXSTR_FILTER = 31
|
||||
|
||||
/**
|
||||
* Use this if for some messed up reason you really need to use something else than UTF-8.
|
||||
* Ask yourself: why should I? Is this really necessary?
|
||||
*
|
||||
* It will set the encoding for all [MPString]s, but if you have inconsistent encoding in your
|
||||
* format you're lost anyway.
|
||||
*/
|
||||
var encoding = Charset.forName("UTF-8")
|
||||
}
|
||||
}
|
40
src/main/kotlin/ch/dissem/msgpack/types/MPType.kt
Normal file
40
src/main/kotlin/ch/dissem/msgpack/types/MPType.kt
Normal file
@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright 2017 Christian Basler
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package ch.dissem.msgpack.types
|
||||
|
||||
import java.io.IOException
|
||||
import java.io.InputStream
|
||||
import java.io.OutputStream
|
||||
|
||||
/**
|
||||
* Representation of some msgpack encoded data.
|
||||
*/
|
||||
interface MPType<out T> {
|
||||
interface Unpacker<out M : MPType<*>> {
|
||||
fun doesUnpack(firstByte: Int): Boolean
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun unpack(firstByte: Int, input: InputStream): M
|
||||
}
|
||||
|
||||
val value: T
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun pack(out: OutputStream)
|
||||
|
||||
fun toJson(): String
|
||||
}
|
133
src/main/kotlin/ch/dissem/msgpack/types/Utils.kt
Normal file
133
src/main/kotlin/ch/dissem/msgpack/types/Utils.kt
Normal file
@ -0,0 +1,133 @@
|
||||
/*
|
||||
* Copyright 2017 Christian Basler
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package ch.dissem.msgpack.types
|
||||
|
||||
import java.io.IOException
|
||||
import java.io.InputStream
|
||||
import java.nio.ByteBuffer
|
||||
|
||||
object Utils {
|
||||
private val BASE64_CODES = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".toCharArray()
|
||||
|
||||
/**
|
||||
* Returns a [ByteBuffer] containing the next `count` bytes from the [InputStream].
|
||||
*/
|
||||
@Throws(IOException::class)
|
||||
internal fun bytes(`in`: InputStream, count: Int): ByteBuffer {
|
||||
val result = ByteArray(count)
|
||||
var off = 0
|
||||
while (off < count) {
|
||||
val read = `in`.read(result, off, count - off)
|
||||
if (read < 0) {
|
||||
throw IOException("Unexpected end of stream, wanted to read $count bytes but only got $off")
|
||||
}
|
||||
off += read
|
||||
}
|
||||
return ByteBuffer.wrap(result)
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to decide which types support extra indention (for pretty printing JSON)
|
||||
*/
|
||||
internal fun toJson(type: MPType<*>, indent: String): String {
|
||||
if (type is MPMap<*, *>) {
|
||||
return type.toJson(indent)
|
||||
}
|
||||
if (type is MPArray<*>) {
|
||||
return type.toJson(indent)
|
||||
}
|
||||
return type.toJson()
|
||||
}
|
||||
|
||||
/**
|
||||
* Slightly improved code from https://en.wikipedia.org/wiki/Base64
|
||||
*/
|
||||
internal fun base64(data: ByteArray): String {
|
||||
val result = StringBuilder(data.size * 4 / 3 + 3)
|
||||
var b: Int
|
||||
var i = 0
|
||||
while (i < data.size) {
|
||||
b = data[i].toInt() and 0xFC shr 2
|
||||
result.append(BASE64_CODES[b])
|
||||
b = data[i].toInt() and 0x03 shl 4
|
||||
if (i + 1 < data.size) {
|
||||
b = b or (data[i + 1].toInt() and 0xF0 shr 4)
|
||||
result.append(BASE64_CODES[b])
|
||||
b = data[i + 1].toInt() and 0x0F shl 2
|
||||
if (i + 2 < data.size) {
|
||||
b = b or (data[i + 2].toInt() and 0xC0 shr 6)
|
||||
result.append(BASE64_CODES[b])
|
||||
b = data[i + 2].toInt() and 0x3F
|
||||
result.append(BASE64_CODES[b])
|
||||
} else {
|
||||
result.append(BASE64_CODES[b])
|
||||
result.append('=')
|
||||
}
|
||||
} else {
|
||||
result.append(BASE64_CODES[b])
|
||||
result.append("==")
|
||||
}
|
||||
i += 3
|
||||
}
|
||||
|
||||
return result.toString()
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
val String.mp
|
||||
@JvmName("mp")
|
||||
get() = MPString(this)
|
||||
|
||||
@JvmStatic
|
||||
val Boolean.mp
|
||||
@JvmName("mp")
|
||||
get() = MPBoolean(this)
|
||||
|
||||
@JvmStatic
|
||||
val Float.mp
|
||||
@JvmName("mp")
|
||||
get() = MPFloat(this)
|
||||
|
||||
@JvmStatic
|
||||
val Double.mp
|
||||
@JvmName("mp")
|
||||
get() = MPFloat(this)
|
||||
|
||||
@JvmStatic
|
||||
val Int.mp
|
||||
@JvmName("mp")
|
||||
get() = MPInteger(this.toLong())
|
||||
|
||||
@JvmStatic
|
||||
val Long.mp
|
||||
@JvmName("mp")
|
||||
get() = MPInteger(this)
|
||||
|
||||
@JvmStatic
|
||||
val ByteArray.mp
|
||||
get() = MPBinary(this)
|
||||
|
||||
@JvmStatic
|
||||
fun mp(vararg data: Byte): MPBinary {
|
||||
return MPBinary(data)
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun nil(): MPNil {
|
||||
return MPNil
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user