Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a class that receives an Object object as a parameter. Then, I want to write two functions to serialize, and deserialize the given object respectively.

public class ObjectWrapper implements Serializable {

    private static final long serialVersionUID = 1L;
    private byte[] bytes;
    private Object object;

    public ObjectWrapper(Object object) {
        this.object = object;
    }

    public ObjectWrapper setObject() throws NotSerializableException {
        try {
            return serialize();
        } catch (NotSerializableException e) {
            throw new NotSerializableException("Error setting object");
        }
    }

    public Object getObject()
            throws NotSerializableException, ClassNotFoundException {
        try {
           return deserialize();
        } catch (NotSerializableException e) {
            throw new NotSerializableException("Error getting object");
        } catch (ClassNotFoundException e) {
            throw new ClassNotFoundException();
        }
    }

    private ObjectWrapper serialize()
            throws NotSerializableException {

        try {

            ObjectOutput out = null;

            // Serialize data object to a byte array
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            out = new ObjectOutputStream(bos);
            out.writeObject(object);

            // Get the bytes of the serialized object
            bytes = bos.toByteArray();

            return new ObjectWrapper(new ObjectBytes(bytes));

        } catch (IOException e) {
            throw new NotSerializableException("Error serializing the object!");  
        }

    }

    private Object deserialize()
            throws NotSerializableException, ClassNotFoundException {

         try {

            ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
            ObjectInput in = null;
            in = new ObjectInputStream(bis);
            Object obj = in.readObject();
            return obj;

        } catch (IOException e) {
            throw new NotSerializableException(
                "Error deserializing the object!");
        }

    }

}

I also needed to write this class, in order to return an object containing only an array of bytes:

public class ObjectBytes implements Serializable{


    private static final long serialVersionUID = 1L;
    private byte[] object_bytes;

    public ObjectBytes(byte[] object_bytes) {
        this.object_bytes = object_bytes;

    }

    public byte[] getObjectBytes() {
        return object_bytes;
    }

}

What I'm struggling with is the whole process of simplifying this code, because for something this simple, I find it a bit odd to have so much code. How can this be shortened?

share|improve this question
2  
What is the use-case for this serialization? Why do you need it? Is it for a client and a server to talk to each other, or for storing to file? Please provide some more context about the usage of this. Could another serialization approach be appropriate as well? – Simon Forsberg May 6 '15 at 11:32
    
@SimonAndréForsberg sorry, you're right! The purpose of this serialisation and deserialization is for persisting the object into a database. That's why i wrote a wrapper class for it. This object could have the type String, Integer or List<String>, so i really have to serialize it by hand. – laker001 May 6 '15 at 11:38
    
What is the meaning of the objects? How much data are we dealing with? As you're dealing with a database, you can store the data in a more database-normalized way. – Simon Forsberg May 6 '15 at 11:48
    
The object could be a string, an integer or a list<String>, representing the properties of a file i want to add to a database. I have a table named properties where i store the properties for each file (i use a Map<String, Object> for that). – laker001 May 6 '15 at 11:50
    
@SimonAndréForsberg any help ? – laker001 May 6 '15 at 21:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.