Search in sources :

Example 1 with DiskLruCache

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;
}
Also used : FilterInputStream(java.io.FilterInputStream) DiskLruCache(com.squareup.okhttp.internal.DiskLruCache) IOException(java.io.IOException) File(java.io.File) BufferedSource(okio.BufferedSource)

Aggregations

DiskLruCache (com.squareup.okhttp.internal.DiskLruCache)1 File (java.io.File)1 FilterInputStream (java.io.FilterInputStream)1 IOException (java.io.IOException)1 BufferedSource (okio.BufferedSource)1