Issue
I have a Kotlin object that I need converted into a byte array (byte[]). I understand how to convert a String and a series of other kinds of variables into byte[], but I can't find anything on doing this with an object. 
Here is what I've tried:
override fun activateQuestion(instructorUserName: String, host: String, port: Int, questionToActivate: MultipleChoiceQuestion) {
        val socket = DatagramSocket()
        //This is the problem -- `.toByteArray(...)` only works for Strings
        val questionToActivateAsByteArray = questionToActivate.toByteArray(Charsets.UTF_8)
        //send byte[] data 
        val packet = DatagramPacket(questionToActivateAsByteArray, questionToActivateAsByteArray.size, InetAddress.getByName(host), port)
        socket.send(packet)
    }
Solution
The following is an object serializable class which is helpful to convert the object to bytes array and vice versa in Kotlin.
public class ObjectSerializer {
    companion object {
        public fun serialize(obj: Any?) : String {
            if (obj == null) {
                return ""
            }
            var baos = ByteArrayOutputStream()
            var oos = ObjectOutputStream(baos)
            oos.writeObject(obj)
            oos.close()
            return encodeBytes(baos.toByteArray())
        }
        public fun deserialize(str: String?) : Any? {
            if (str == null || str.length() == 0) {
                return null
            }
            var bais = ByteArrayInputStream(decodeBytes(str))
            var ois = ObjectInputStream(bais)
            return ois.readObject()
        }
        private fun encodeBytes(bytes: ByteArray) : String {
            var buffer = StringBuffer()
            for (byte in bytes) {
                buffer.append(((byte.toInt() shr 4) and 0xF plus 'a').toChar())
                buffer.append(((byte.toInt()) and 0xF plus 'a').toChar())
            }
            return buffer.toString()
        }
        private fun decodeBytes(str: String) : ByteArray {
            var bytes = ByteArray(str.length() / 2)
            for (i in 0..(str.length() - 1)) {
                var c = str.charAt(i)
                bytes.set(i / 2, ((c minus 'a').toInt() shl 4).toByte())
                c = str.charAt(i + 1)
                bytes.set(i / 2, (bytes.get(i / 2) + (c minus 'a')).toByte())
            }
            return bytes
        }
    }
}
Answered By - Codemaker Answer Checked By - David Marino (PHPFixing Volunteer)
 
 Posts
Posts
 
 
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.