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 com.android.gallery3d.common.BlobCache; 20 import com.android.gallery3d.common.Utils; 21 import com.android.gallery3d.util.CacheManager; 22 import com.android.gallery3d.util.GalleryUtils; 23 24 import android.content.Context; 25 26 import java.io.IOException; 27 import java.nio.ByteBuffer; 28 29 public class ImageCacheService { 30 @SuppressWarnings("unused") 31 private static final String TAG = "ImageCacheService"; 32 33 private static final String IMAGE_CACHE_FILE = "imgcache"; 34 private static final int IMAGE_CACHE_MAX_ENTRIES = 5000; 35 private static final int IMAGE_CACHE_MAX_BYTES = 200 * 1024 * 1024; 36 private static final int IMAGE_CACHE_VERSION = 3; 37 38 private BlobCache mCache; 39 40 public ImageCacheService(Context context) { 41 mCache = CacheManager.getCache(context, IMAGE_CACHE_FILE, 42 IMAGE_CACHE_MAX_ENTRIES, IMAGE_CACHE_MAX_BYTES, 43 IMAGE_CACHE_VERSION); 44 } 45 46 public static class ImageData { 47 public ImageData(byte[] data, int offset) { 48 mData = data; 49 mOffset = offset; 50 } 51 public byte[] mData; 52 public int mOffset; 53 } 54 55 public ImageData getImageData(Path path, int type) { 56 byte[] key = makeKey(path, type); 57 long cacheKey = Utils.crc64Long(key); 58 try { 59 byte[] value = null; 60 synchronized (mCache) { 61 value = mCache.lookup(cacheKey); 62 } 63 if (value == null) return null; 64 if (isSameKey(key, value)) { 65 int offset = key.length; 66 return new ImageData(value, offset); 67 } 68 } catch (IOException ex) { 69 // ignore. 70 } 71 return null; 72 } 73 74 public void putImageData(Path path, int type, byte[] value) { 75 byte[] key = makeKey(path, type); 76 long cacheKey = Utils.crc64Long(key); 77 ByteBuffer buffer = ByteBuffer.allocate(key.length + value.length); 78 buffer.put(key); 79 buffer.put(value); 80 synchronized (mCache) { 81 try { 82 mCache.insert(cacheKey, buffer.array()); 83 } catch (IOException ex) { 84 // ignore. 85 } 86 } 87 } 88 89 private static byte[] makeKey(Path path, int type) { 90 return GalleryUtils.getBytes(path.toString() + "+" + type); 91 } 92 93 private static boolean isSameKey(byte[] key, byte[] buffer) { 94 int n = key.length; 95 if (buffer.length < n) { 96 return false; 97 } 98 for (int i = 0; i < n; ++i) { 99 if (key[i] != buffer[i]) { 100 return false; 101 } 102 } 103 return true; 104 } 105 } 106