I'm trying to create a class that provides object serialization and deserialization methods for use by another class. These two methods (I think) are in the class already (see code below). I'm not sure how I would go about making those 2 methods their own class and then returning the object back to the class I have created.

public static byte[] serializeObj(Object obj)
        throws IOException
           ByteArrayOutputStream baOStream = new ByteArrayOutputStream();
           ObjectOutputStream objOStream = new ObjectOutputStream(baOStream);

           objOStream.writeObject(obj); // object must be Serializable
           return baOStream.toByteArray(); // returns stream as byte array

        // Method to read bytes from result set into a byte array and then
        // create an input stream and read the data into an object
        public static Object deserializeObj(byte[] buf)
        throws IOException, ClassNotFoundException
           Object obj = null;

           if (buf != null)
           ObjectInputStream objIStream =
           new ObjectInputStream(new ByteArrayInputStream(buf));

           obj = objIStream.readObject(); // throws IOException, ClassNotFoundException
        return obj;
8 Years
Discussion Span
Last Post by JamesCherrill

Just put them in a new class :

public class Serialiser {
  public static byte[] serializeObj(Object obj)
  // etc

then call them from any other class like this:

byte[] bytes = Serialiser.serialiseObj(myObject);
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.