Converting Any Object to a Byte Array in Java

Java Serializable Object to Byte Array

Prepare the byte array to send:

ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream out = null;
try {
out = new ObjectOutputStream(bos);
out.writeObject(yourObject);
out.flush();
byte[] yourBytes = bos.toByteArray();
...
} finally {
try {
bos.close();
} catch (IOException ex) {
// ignore close exception
}
}

Create an object from a byte array:

ByteArrayInputStream bis = new ByteArrayInputStream(yourBytes);
ObjectInput in = null;
try {
in = new ObjectInputStream(bis);
Object o = in.readObject();
...
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException ex) {
// ignore close exception
}
}

Converting from object to byte array and back in java

This should work, just cast the object to your class after receiving it from the serialize/deserialize function.

public static byte[] objToByte(TcpPacket tcpPacket) throws IOException {
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
ObjectOutputStream objStream = new ObjectOutputStream(byteStream);
objStream.writeObject(tcpPacket);

return byteStream.toByteArray();
}

public static Object byteToObj(byte[] bytes) throws IOException, ClassNotFoundException {
ByteArrayInputStream byteStream = new ByteArrayInputStream(bytes);
ObjectInputStream objStream = new ObjectInputStream(byteStream);

return objStream.readObject();
}

Java: object to byte[] and byte[] to object converter (for Tokyo Cabinet)

public static byte[] serialize(Object obj) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream os = new ObjectOutputStream(out);
os.writeObject(obj);
return out.toByteArray();
}
public static Object deserialize(byte[] data) throws IOException, ClassNotFoundException {
ByteArrayInputStream in = new ByteArrayInputStream(data);
ObjectInputStream is = new ObjectInputStream(in);
return is.readObject();
}

In Java, how to convert a list of objects to byte array?

Author class should implement Serializable

Then you can use ObjectOutputStream to serialize the object and ByteArrayOutputStream to get it written as bytes.

Then deserialize it using ObjectInputStream and convert back.

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(list);
byte[] bytes = bos.toByteArray();

How to convert an Object into a byte array in Kotlin

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
}
}
}


Related Topics



Leave a reply



Submit