简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM