Jan 7, 2004 #1 Stevoie Programmer Jun 7, 2001 68 IE anyone know how to convert any java object to a byte array then convert it back again? thanx StevoIE
Jan 7, 2004 #2 globos Programmer Nov 8, 2000 260 FR See java API documentation : - Write to an OutputStream an object whose class implements Serializable : http://java.sun.com/j2se/1.4.2/docs/api/java/io/ObjectOutputStream.html - Read from an InputStream an object whose class implements Serializable : http://java.sun.com/j2se/1.4.2/docs/api/java/io/ObjectInputStream.html I've never used these classes, I think they are the right ones to use, anyway you must deal with the Serializable interface. -- Globos Upvote 0 Downvote
See java API documentation : - Write to an OutputStream an object whose class implements Serializable : http://java.sun.com/j2se/1.4.2/docs/api/java/io/ObjectOutputStream.html - Read from an InputStream an object whose class implements Serializable : http://java.sun.com/j2se/1.4.2/docs/api/java/io/ObjectInputStream.html I've never used these classes, I think they are the right ones to use, anyway you must deal with the Serializable interface. -- Globos
Jan 7, 2004 Thread starter #3 Stevoie Programmer Jun 7, 2001 68 IE /** * * @param obj * @return * @throws java.io.IOException */ public byte[] convertObjectToBytes(Object obj) throws java.io.IOException{ ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeObject(obj); oos.flush(); oos.close(); bos.close(); byte [] data = bos.toByteArray(); return data; } /** * * @param obj * @return * @throws java.io.IOException */ public Object convertBytesToObject(byte[] bytes) throws java.io.IOException{ Object convertedObj = null; ByteArrayInputStream bos = new ByteArrayInputStream(bytes); ObjectInputStream oos = new ObjectInputStream(bos); try{ convertedObj = (Object) oos.readObject(); }catch(Exception ex){ ; } oos.close(); bos.close(); return convertedObj; } StevoIE Upvote 0 Downvote
/** * * @param obj * @return * @throws java.io.IOException */ public byte[] convertObjectToBytes(Object obj) throws java.io.IOException{ ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeObject(obj); oos.flush(); oos.close(); bos.close(); byte [] data = bos.toByteArray(); return data; } /** * * @param obj * @return * @throws java.io.IOException */ public Object convertBytesToObject(byte[] bytes) throws java.io.IOException{ Object convertedObj = null; ByteArrayInputStream bos = new ByteArrayInputStream(bytes); ObjectInputStream oos = new ObjectInputStream(bos); try{ convertedObj = (Object) oos.readObject(); }catch(Exception ex){ ; } oos.close(); bos.close(); return convertedObj; } StevoIE