11

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();
3
  • 3
    Do you want one Object per byte, as the boxed Byte? It's not clear. Commented Jun 26, 2012 at 19:17
  • i am having an ArrayList< Object> for storing purpose coverted to byte[] and i want to retrive ArrayList< Object> back from byte[] Commented Jun 26, 2012 at 19:28
  • Well how have you converted it to a byte array? We can't tell you how to reverse the process without that information. Commented Jun 26, 2012 at 19:29

2 Answers 2

19

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();
}
Sign up to request clarification or add additional context in comments.

Comments

-2

I'm going to go with the obvious answer here...

for(byte b : bytearray) {
  arraylist.add(new Byte(b));
}

1 Comment

I think he doesn't want to create a List of Byte

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.