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
           objOStream.flush();
           objOStream.close();
           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;
    }

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);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.