Java: Object to Byte[] and Byte[] to Object Converter (For Tokyo Cabinet)

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();
}

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

Error in casting Object[] to byte[]

B cannot be cast to [Ljava.lang.Object;

Means you are indeed getting bytearray and you are trying to convert it into Object array.

Android real time game byte message transfer

Make your custom Object serializable if it's not.
Implement Serializable Interface.
Then convert between object and byte array to transfer or receive the data.

Object to byte[]:

ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream(bos);
out.writeObject(yourObject);
byte[] yourBytes = bos.toByteArray();

byte[] to Object:

ByteArrayIntputSream bis = new ByteArrayInputStream(yourBytes);
ObjectInput in = new ObjectInputStream(bis);
Object o = in.readObject();

How can an ArrayList of objects(ArrayLists) be converted to an array of bytes?

I have found that this solution works for Java UDP using the serialization method,

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();
}

sending the ArrayList,

ArrayList<Object> array = new ArrayList<Object>();
array.add(arrayList1);
array.add(arrayList2);
array.add(arrayList3);
array.add(arrayList4);

sendData = serialize(array);
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, address, port);
datagramSocket.send(sendPacket);

and then receiving it.

DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
clientSocket.receive(receivePacket);
ArrayList<Object> object = (ArrayList)deserialize(receivePacket.getData());
ArrayList<Object> arrayList = (ArrayList<Object>)object.get(0);

This seems to work for me, so I think that this method should work for you.



Related Topics



Leave a reply



Submit