PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, October 22, 2022

[FIXED] How to convert an Object into a byte array in Kotlin

 October 22, 2022     arrays, kotlin, sockets     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing