1package com.bumptech.glide.load;
2
3import android.util.Log;
4import com.bumptech.glide.load.engine.Resource;
5import com.bumptech.glide.load.engine.cache.DiskCache;
6
7import java.io.IOException;
8import java.io.InputStream;
9
10public class CacheLoader {
11    private static final String TAG = "CacheLoader";
12    private DiskCache diskCache;
13
14    public CacheLoader(DiskCache diskCache) {
15        this.diskCache = diskCache;
16    }
17
18    public <Z> Resource<Z> load(Key key, ResourceDecoder<InputStream, Z> decoder, int width, int height) {
19        Resource<Z> result = null;
20        InputStream fromCache = diskCache.get(key);
21        if (fromCache != null) {
22            try {
23                result = decoder.decode(fromCache, width, height);
24            } catch (IOException e) {
25                if (Log.isLoggable(TAG, Log.DEBUG)) {
26                    Log.d(TAG, "Exception decoding image from cache", e);
27                }
28            }
29            if (result == null) {
30                if (Log.isLoggable(TAG, Log.DEBUG)) {
31                    Log.d(TAG, "Failed to decode image from cache or not present in cache");
32                }
33                diskCache.delete(key);
34            }
35        }
36        return result;
37    }
38}
39