Showing posts with label Java IOStream. Show all posts
Showing posts with label Java IOStream. Show all posts

Wednesday, April 22, 2009

Convert Object To Byte Array Using JAVA

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

public class ObjectToByte {

public byte[] convert(Object obj) throws IOException {
ObjectOutputStream os = null;

ByteArrayOutputStream byteStream = new ByteArrayOutputStream(5000);
os = new ObjectOutputStream(new BufferedOutputStream(byteStream));
os.flush();
os.writeObject(obj);
os.flush();
byte[] sendBuf = byteStream.toByteArray();
os.close();
return sendBuf;

}
}

LinkWithin

Related Posts with Thumbnails