Skip to content

Instantly share code, notes, and snippets.

@elliottsj
Last active January 1, 2016 15:28
Show Gist options
  • Save elliottsj/8164036 to your computer and use it in GitHub Desktop.
Save elliottsj/8164036 to your computer and use it in GitHub Desktop.
import java.io.*;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
DiskLruCache diskLruCache = null;
try {
diskLruCache = DiskLruCache.open(new File("DiskLruCache"), 1, 2, 20 * 2^20);
} catch (IOException e) {
e.printStackTrace();
}
if (diskLruCache == null)
return;
DiskLruCache.Editor editor = null;
try {
editor = diskLruCache.edit("objects");
OutputStream timeOs = editor.newOutputStream(0);
OutputStream dataOs = editor.newOutputStream(1);
OutputStream timeBos = new BufferedOutputStream(timeOs);
OutputStream dataBos = new BufferedOutputStream(dataOs);
ObjectOutputStream timeOos = new ObjectOutputStream(timeBos);
ObjectOutputStream dataOos = new ObjectOutputStream(dataBos);
long createTime = System.currentTimeMillis();
String str = "testString";
ArrayList<String> list = new ArrayList<String>();
list.add("item1");
list.add("item2");
timeOos.writeLong(createTime);
// this works:
dataOos.writeObject(str);
// this does not work:
//dataOos.writeObject(list);
timeOos.close();
dataOos.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (editor != null)
try {
editor.commit();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public class MyCache {
/**
* Static strings for each command
*/
private static class Command {
public static final String MY_OBJECTS = "myobjects";
}
private enum EntryType {
CREATE_TIME,
DATA
}
DiskLruCache lruCache;
/**
* Constructs this cache in the given context.
*
* @param context the Android context where this cache will exist
* @throws IOException if reading or writing the cache directory fails
*/
public MyCache(Context context) throws IOException {
long maxSize = 20 * 2^20; // 20 MiB
/**
* Open or create the cache files on the cache directory of the context.
* Each entry has two items:
* - item 0 contains a Long of the creation time of the data (milliseconds since epoch)
* - item 1 contains the data
*/
this.lruCache = DiskLruCache.open(context.getCacheDir(), 1, EntryType.values().length, maxSize);
}
public boolean isMyObjectsCached() {
try {
return lruCache.get(Command.MY_OBJECTS) != null;
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
public long getMyObjectsAge() {
if (!isMyObjectsCached())
throw new RuntimeException("MyObjects are not cached");
DiskLruCache.Snapshot snapshot = null;
try {
snapshot = lruCache.get(Command.MY_OBJECTS);
ObjectInputStream ois = new ObjectInputStream(snapshot.getInputStream(EntryType.CREATE_TIME.ordinal()));
return ois.readLong();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (snapshot != null)
snapshot.close();
}
return 0;
}
public Map<String, MySerializableClass> getMyObjects() {
DiskLruCache.Snapshot snapshot = null;
Map<String, MySerializableClass> myObjectMap = null;
try {
snapshot = lruCache.get(Command.MY_OBJECTS);
InputStream dataIs = snapshot.getInputStream(EntryType.DATA.ordinal());
ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(dataIs));
//noinspection unchecked
myObjectMap = (Map<String, MySerializableClass>) ois.readObject();
} catch (IOException e) {
String error = e.toString();
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
if (snapshot != null)
snapshot.close();
}
return myObjectMap;
}
public void putMyObjects(List<MySerializableClass> myObjects) {
DiskLruCache.Editor editor = null;
try {
editor = lruCache.edit(Command.MY_OBJECTS);
OutputStream timeOs = editor.newOutputStream(EntryType.CREATE_TIME.ordinal());
OutputStream dataOs = editor.newOutputStream(EntryType.DATA.ordinal());
long createTime = System.currentTimeMillis();
Map<String, MySerializableClass> myObjectsMap = new HashMap<String, MySerializableClass>();
for (MySerializableClass myObject : myObjects)
myObjectsMap.put(myObject.getId(), myObject);
ObjectOutputStream timeOos = new ObjectOutputStream(new BufferedOutputStream(timeOs));
ObjectOutputStream dataOos = new ObjectOutputStream(new BufferedOutputStream(dataOs));
timeOos.writeLong(createTime);
dataOos.writeObject(myObjectsMap);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (editor != null)
try {
editor.commit();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@elliottsj
Copy link
Author

(From Stackoverflow, 2013-12-28):

I'm using putMyObjects() to write objects to the cache, then reading data using getMyObjects().

Unfortunately, a call to getMyObjects() results in a java.io.EOFException. I've checked the actual cache files that DiskLruCache creates and found that they are empty, so the issue must be that the data is not being written properly.

In putMyObjects(), I've tried putting calls like lruCache.flush(), timeOos.flush(), dataOos.flush(), timeOos.close(), dataOos.close() in the finally block, but the data is still not written.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment