Home | History | Annotate | Download | only in data
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.gallery3d.data;
     18 
     19 import android.content.Context;
     20 
     21 import com.android.gallery3d.common.BlobCache;
     22 import com.android.gallery3d.common.BlobCache.LookupRequest;
     23 import com.android.gallery3d.common.Utils;
     24 import com.android.gallery3d.data.BytesBufferPool.BytesBuffer;
     25 import com.android.gallery3d.util.CacheManager;
     26 import com.android.gallery3d.util.GalleryUtils;
     27 
     28 import java.io.IOException;
     29 import java.nio.ByteBuffer;
     30 
     31 public class ImageCacheService {
     32     @SuppressWarnings("unused")
     33     private static final String TAG = "ImageCacheService";
     34 
     35     private static final String IMAGE_CACHE_FILE = "imgcache";
     36     private static final int IMAGE_CACHE_MAX_ENTRIES = 5000;
     37     private static final int IMAGE_CACHE_MAX_BYTES = 200 * 1024 * 1024;
     38     private static final int IMAGE_CACHE_VERSION = 7;
     39 
     40     private BlobCache mCache;
     41 
     42     public ImageCacheService(Context context) {
     43         mCache = CacheManager.getCache(context, IMAGE_CACHE_FILE,
     44                 IMAGE_CACHE_MAX_ENTRIES, IMAGE_CACHE_MAX_BYTES,
     45                 IMAGE_CACHE_VERSION);
     46     }
     47 
     48     /**
     49      * Gets the cached image data for the given <code>path</code> and <code>type</code>.
     50      *
     51      * The image data will be stored in <code>buffer.data</code>, started from
     52      * <code>buffer.offset</code> for <code>buffer.length</code> bytes. If the
     53      * buffer.data is not big enough, a new byte array will be allocated and returned.
     54      *
     55      * @return true if the image data is found; false if not found.
     56      */
     57     public boolean getImageData(Path path, int type, BytesBuffer buffer) {
     58         byte[] key = makeKey(path, type);
     59         long cacheKey = Utils.crc64Long(key);
     60         try {
     61             LookupRequest request = new LookupRequest();
     62             request.key = cacheKey;
     63             request.buffer = buffer.data;
     64             synchronized (mCache) {
     65                 if (!mCache.lookup(request)) return false;
     66             }
     67             if (isSameKey(key, request.buffer)) {
     68                 buffer.data = request.buffer;
     69                 buffer.offset = key.length;
     70                 buffer.length = request.length - buffer.offset;
     71                 return true;
     72             }
     73         } catch (IOException ex) {
     74             // ignore.
     75         }
     76         return false;
     77     }
     78 
     79     public void putImageData(Path path, int type, byte[] value) {
     80         byte[] key = makeKey(path, type);
     81         long cacheKey = Utils.crc64Long(key);
     82         ByteBuffer buffer = ByteBuffer.allocate(key.length + value.length);
     83         buffer.put(key);
     84         buffer.put(value);
     85         synchronized (mCache) {
     86             try {
     87                 mCache.insert(cacheKey, buffer.array());
     88             } catch (IOException ex) {
     89                 // ignore.
     90             }
     91         }
     92     }
     93 
     94     public void clearImageData(Path path, int type) {
     95         byte[] key = makeKey(path, type);
     96         long cacheKey = Utils.crc64Long(key);
     97         synchronized (mCache) {
     98             try {
     99                 mCache.clearEntry(cacheKey);
    100             } catch (IOException ex) {
    101                 // ignore.
    102             }
    103         }
    104     }
    105 
    106     private static byte[] makeKey(Path path, int type) {
    107         return GalleryUtils.getBytes(path.toString() + "+" + type);
    108     }
    109 
    110     private static boolean isSameKey(byte[] key, byte[] buffer) {
    111         int n = key.length;
    112         if (buffer.length < n) {
    113             return false;
    114         }
    115         for (int i = 0; i < n; ++i) {
    116             if (key[i] != buffer[i]) {
    117                 return false;
    118             }
    119         }
    120         return true;
    121     }
    122 }
    123