zwwcn

Just another WordPress.com site

find object can’t be serialized

I encountered a problem today :one of the object can't be serialized, then I found
this useful code on from http://www.theserverside.com/discussions/thread.tss?thread_id=17336
       public static final Object clone(Serializable in) {
            try {
                ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream();
                ObjectOutputStream outStream = new ObjectOutputStream(byteOutStream);
                outStream.writeObject(in);
                ByteArrayInputStream byteInStream =
                    new ByteArrayInputStream(byteOutStream.toByteArray());
                ObjectInputStream inStream = new ObjectInputStream(byteInStream);
                return inStream.readObject();
            } catch (OptionalDataException e) {
             throw new RuntimeException("Optional data found. " + e.getMessage()); 
            } catch (StreamCorruptedException e) {
             throw new RuntimeException("Serialized object got corrupted. " + e.getMessage());
            } catch (ClassNotFoundException e) {
             throw new RuntimeException("A class could not be found during deserialization. " + e.getMessage()); 
            } catch (NotSerializableException ex) {
                ex.printStackTrace();
             throw new IllegalArgumentException("Object is not serializable: " + ex.getMessage());
            } catch (IOException e) {
             throw new RuntimeException("IO operation failed during serialization. " + e.getMessage()); 
            }
        }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: