use of com.squareup.okhttp.internal.DiskLruCache in project remusic by aa112901.
the class HttpUtil method getFromCache.
public static FilterInputStream getFromCache(Context context, String url) throws Exception {
// File cacheDirectory = new File("/storage/emulated/0/Android/data/com.name.demo .dev/cache/HttpCache");
File cacheDirectory = context.getExternalCacheDir();
DiskLruCache cache = DiskLruCache.create(FileSystem.SYSTEM, cacheDirectory, 201105, 2, 1024 * 1024 * 30);
cache.flush();
String key = Util.md5Hex(url);
final DiskLruCache.Snapshot snapshot;
try {
snapshot = cache.get(key);
if (snapshot == null) {
return null;
}
} catch (IOException e) {
return null;
}
okio.Source source = snapshot.getSource(1);
BufferedSource metadata = Okio.buffer(source);
FilterInputStream bodyIn = new FilterInputStream(metadata.inputStream()) {
@Override
public void close() throws IOException {
snapshot.close();
super.close();
}
};
return bodyIn;
}
Aggregations