Here's some code I'm using in my apps:

        public static byte[] getSerializedObject(Serializable s) {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                ObjectOutputStream oos = null;
                try {
                        oos = new ObjectOutputStream(baos);
                        oos.writeObject(s);
                } catch (IOException e) {
                        Logger.e(loggerTag, e.getMessage(), e);
                        return null;
                } finally {
                        try {
                                oos.close();
                        } catch (IOException e) {}
                }
                byte[] result = baos.toByteArray();
                Logger.d(loggerTag, "Object " + s.getClass().getSimpleName() + "
written to byte[]: " + result.length);
                return result;
        }

        public static Object readSerializedObject(byte[] in) {
                Object result = null;
                ByteArrayInputStream bais = new ByteArrayInputStream(in);
                ObjectInputStream ois = null;
                try {
                        ois = new ObjectInputStream(bais);
                        result = ois.readObject();
                } catch (Exception e) {
                        result = null;
                } finally {
                        try {
                                ois.close();
                        } catch (Throwable e) {
                        }
                }
                return result;
        }

The code is basically doing the same as the one in your link except
that the error handling is a bit more elaborate.
To store the object into a db use something like that:

    public long insertRecord(SQLiteDatabase db, Serializable myObject)
{
                try {
                        db.beginTransaction();
                        ContentValues values = new ContentValues();
                        values.put("MyColumn", 
Helper.getSerializedObject(myObject));
                        long id = db.insert("MyTable", null, values);
                        if (id>=0) db.setTransactionSuccessful();
                        return id;
                }
                catch (Exception e) {
                        Logger.e(loggerTag, e.getMessage(), e);
                        // ignore this and roll back the transaction
                }
                finally {
                        try {
                                db.endTransaction();
                        } catch (Exception e) {
                                return -1;
                        }
                }
                return -1;
    }

You mention nested objects. This is a serious issue with Java
serialization especially on platforms with small stack sizes (like
Android). If your objects are deeply nested you might run into
StackOverflowExceptions.
To prevent this you might want to implement your own writeObject/
readObject methods and read/write Objects in an iterative way instead
of using recursion. If you do this there are two additional issues:
- don't expect onSaveInstanceState(Bundle outState),
outState.putSerializable to use your custom serialization code because
the objects are only serialized if they are sent across processes
(which happens almost never).
In any other case (like orientation change) Android uses its internal
Parcel mechanism which will run into the same stack issues if the
objects are deeply nested.
- if you use ProGuard don't forget to add something like:

-keepclassmembers class * implements java.io.Serializable {
    static final long serialVersionUID;
    private static final java.io.ObjectStreamField[]
serialPersistentFields;
    private void writeObject(java.io.ObjectOutputStream);
    private void readObject(java.io.ObjectInputStream);
    java.lang.Object writeReplace();
    java.lang.Object readResolve();
}

Cheers
Emanuel Moecklin
1gravity LLC

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to