Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert byte [] to ArrayList<Object>

Tags:

java

I have a byte[] that i obtained using Object ArrayList<Obj>

Can anyone tell me how to convert my byte[] to Object ArrayList?

Coveting ArrayList like this:

ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = null;
oos = new ObjectOutputStream(bos);
 
oos.writeObject(mArrayList);//mArrayList is the array to convert
byte[] buff = bos.toByteArray();
like image 406
evan Avatar asked Jun 26 '12 19:06

evan


People also ask

How to convert byte array to list of object in Java?

ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = null; oos = new ObjectOutputStream(bos); oos. writeObject(mArrayList);//mArrayList is the array to convert byte[] buff = bos. toByteArray(); java.

What is byte [] in Java?

A byte in Java is 8 bits. It is a primitive data type, meaning it comes packaged with Java. Bytes can hold values from -128 to 127. No special tasks are needed to use it; simply declare a byte variable and you are off to the races.


1 Answers

Now you've given us the information about how you did the conversion one way... you need:

ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes));
try {
    @SuppressWarnings("unchecked")
    ArrayList<Object> list = (ArrayList<Object>) ois.readObject();
    ...
} finally {
    ois.close();
}
like image 150
Jon Skeet Avatar answered Oct 05 '22 18:10

Jon Skeet