0

I have an object caching class that can save an object in memory, the problem arises when I try to retreive it later. How do I output a generic Object and then put it into a defined object on the other end. Here is the class

public class ObjectCache implements Serializable{

private static final long serialVersionUID = 1L;
private Context context;
private String objectName;
private Integer keepAlive = 86400;

public ObjectCache(Context context,String objectName) {
    this.context = context;
    this.objectName = objectName;
}

public void setKeepAlive(Integer time) {
    this.keepAlive = time;
}

public boolean saveObject(Object obj) {

    final File cacheDir = context.getCacheDir();
    FileOutputStream fos = null;
    ObjectOutputStream oos = null;

    if (!cacheDir.exists()) {
        cacheDir.mkdirs();
    }

    final File cacheFile = new File(cacheDir, objectName);

    try {

        fos = new FileOutputStream(cacheFile);
        oos = new ObjectOutputStream(fos);
        oos.writeObject(obj);
    } catch (Exception e) {
        e.printStackTrace();

    } finally {
        try {
            if (oos != null) {
                oos.close();
            }
            if (fos != null) {
                fos.close();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return true;
}

public boolean deleteObject() {

    final File cacheDir = context.getCacheDir();
    final File cacheFile = new File(cacheDir, objectName);
    return (cacheFile.delete());
}

public Object getObject() {

    final File cacheDir = context.getCacheDir();
    final File cacheFile = new File(cacheDir, objectName);

    Object simpleClass = null;
    FileInputStream fis = null;
    ObjectInputStream is = null;

    try {

        fis = new FileInputStream(cacheFile);
        is = new ObjectInputStream(fis);
        simpleClass = (Object) is.readObject();
    } catch (Exception e) {
        e.printStackTrace();

    } finally {
        try {
            if (fis != null) {
                fis.close();
            }
            if (is != null) {
                is.close();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    return simpleClass;

}
}

And from activity I start the object class, save it, and retreive it like so

objectCache = new ObjectCache(this,"sessionCache2");
//save object returns true
boolean result = objectCache.saveObject(s);

//get object back
Object o = objectCache.getObject();

instead of Object o i need it to be a Session object, but then that will mean the return type of the getObject method needs to return that. Can't I convert the object some

Brian
  • 4,328
  • 13
  • 58
  • 103
  • 1
    If you truly have flattened and reflated an object of class Session, then there should still be an object of class Session and now a reference, to the object, of Type object. The object of class Session still exists. You should be able to do an explicit cast to obtain a reference of type Session to the object of class Session. So objects have class and reference variables have type. http://www.geocities.com/jeff_louie/OOP/oop6.htm – JAL Jun 03 '11 at 02:24

1 Answers1

2

If you are sure that the object that the ObjectCache will return is a Session object all you have to do is cast it:

//get object back
Session session = (Session) objectCache.getObject();

This will throw a ClassCastException (unchecked) if the getObject() method does not return a Session object.

Gyan aka Gary Buyn
  • 12,242
  • 2
  • 23
  • 26