Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

byte array in java returning null after conversion from objects

I need to send my phone contacts in android as bytes. So i have crated a bean class implementing serializable , but after converting the arraylist of bean class to byte array, byte array is always showing null. Here is my sample code.

 ByteArrayOutputStream baos = new ByteArrayOutputStream();
 ObjectOutputStream oos = new ObjectOutputStream(baos);
 oos.writeObject(presentContacts);
 byte[] buf = baos.toByteArray();

Here presentContacts is the ArrayList of bean class. Byte array, buf is always returning null but presentContacts is not null.

like image 667
Naba Avatar asked Dec 01 '25 21:12

Naba


1 Answers

You should probably close or at least flush the ObjectOutputStream. Something like this

 ByteArrayOutputStream baos = new ByteArrayOutputStream();
 ObjectOutputStream oos = new ObjectOutputStream(baos);
 oos.writeObject(presentContacts);
 oos.flush(); 
 byte[] buf = baos.toByteArray();
like image 52
Sergey Gazaryan Avatar answered Dec 03 '25 10:12

Sergey Gazaryan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!