当前位置: 首页>>代码示例>>Java>>正文


Java DiskLruCache类代码示例

本文整理汇总了Java中com.jakewharton.disklrucache.DiskLruCache的典型用法代码示例。如果您正苦于以下问题:Java DiskLruCache类的具体用法?Java DiskLruCache怎么用?Java DiskLruCache使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


DiskLruCache类属于com.jakewharton.disklrucache包,在下文中一共展示了DiskLruCache类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: putDataToDiskLruCache

import com.jakewharton.disklrucache.DiskLruCache; //导入依赖的package包/类
private void putDataToDiskLruCache(Image image) {

        try {
            /* 第一步:获取将要缓存的图片的对应唯一 key 值 */
            String key = DiskCacheUtil.getMD5String(image.getUrl());
        /* 第二步:获取 DiskLruCache 的 Editor */
            DiskLruCache.Editor editor = mDiskLruCache.edit(key);

            if (null != editor) {
                /* 第三步:从 Editor 中获取 OutputStream */
                OutputStream outputStream = editor.newOutputStream(0);
                /* 第四步:下载网络图片且保存至 DiskLruCache 图片中 */
                boolean isSuccessful = download(image.getUrl(), outputStream);
                if (isSuccessful) {
                    editor.commit();
                } else {
                    editor.abort();
                }
                mDiskLruCache.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
开发者ID:InnoFang,项目名称:Android-Code-Demos,代码行数:25,代码来源:DiskCacheObservable.java

示例2: exist

import com.jakewharton.disklrucache.DiskLruCache; //导入依赖的package包/类
static int exist(String name) {

        DiskLruCache diskLruCache = getDiskLruCache();
        if (diskLruCache == null) {
            return -1;
        }
        try {
            DiskLruCache.Snapshot snapshot = diskLruCache.get(name);
            if (snapshot == null) {
                return -1;
            }
            InputStream stream = snapshot.getInputStream(1);
            boolean hasBitmap = readBoolean(stream);
            return hasBitmap ? 1 : 0;
        } catch (IOException e) {
            e.printStackTrace();
        }

        return -1;
    }
 
开发者ID:nichbar,项目名称:Aequorea,代码行数:21,代码来源:BitmapWrapper.java

示例3: cacheInStorage

import com.jakewharton.disklrucache.DiskLruCache; //导入依赖的package包/类
private void cacheInStorage(long id, String articleData) {
    String key = Long.toString(id);
    if (mDiskCache != null) {
        try {
            DiskLruCache.Snapshot snapshot = mDiskCache.get(key);
            
            if (snapshot == null) {
                DiskLruCache.Editor editor = mDiskCache.edit(key);

                OutputStream out = editor.newOutputStream(0);
                out.write(articleData.getBytes());
                out.close();
                editor.commit();
            } else {
                snapshot.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
 
开发者ID:nichbar,项目名称:Aequorea,代码行数:22,代码来源:ArticleCache.java

示例4: loadCacheFromStorage

import com.jakewharton.disklrucache.DiskLruCache; //导入依赖的package包/类
public String loadCacheFromStorage(long id) {
    String key = Long.toString(id);
    
    if (mDiskCache != null) {
        try {
            DiskLruCache.Snapshot snapshot = mDiskCache.get(key);
            InputStream in = snapshot.getInputStream(0);
            if (in != null) {
                BufferedInputStream bis = new BufferedInputStream(in);
                ByteArrayOutputStream buf = new ByteArrayOutputStream();
                int result = bis.read();
                while (result != -1) {
                    buf.write((byte) result);
                    result = bis.read();
                }
                return buf.toString("UTF-8");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return "";
}
 
开发者ID:nichbar,项目名称:Aequorea,代码行数:24,代码来源:ArticleCache.java

示例5: doLoad

import com.jakewharton.disklrucache.DiskLruCache; //导入依赖的package包/类
@Override
protected <T> T doLoad(Type type, String key) {
    if (mDiskLruCache == null) {
        return null;
    }
    try {
        DiskLruCache.Editor edit = mDiskLruCache.edit(key);
        if (edit == null) {
            return null;
        }

        InputStream source = edit.newInputStream(0);
        T value;
        if (source != null) {
            value = mDiskConverter.load(source,type);
            Utils.close(source);
            edit.commit();
            return value;
        }
        edit.abort();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}
 
开发者ID:zhou-you,项目名称:RxEasyHttp,代码行数:26,代码来源:LruDiskCache.java

示例6: doSave

import com.jakewharton.disklrucache.DiskLruCache; //导入依赖的package包/类
@Override
protected <T> boolean doSave(String key, T value) {
    if (mDiskLruCache == null) {
        return false;
    }
    try {
        DiskLruCache.Editor edit = mDiskLruCache.edit(key);
        if (edit == null) {
            return false;
        }
        OutputStream sink = edit.newOutputStream(0);
        if (sink != null) {
            boolean result = mDiskConverter.writer(sink, value);
            Utils.close(sink);
            edit.commit();
            return result;
        }
        edit.abort();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return false;
}
 
开发者ID:zhou-you,项目名称:RxEasyHttp,代码行数:24,代码来源:LruDiskCache.java

示例7: fetch

import com.jakewharton.disklrucache.DiskLruCache; //导入依赖的package包/类
public <T> T fetch(String key, Class<T> classOfT) {
    key = sanitizeKey(key);
    try {
        DiskLruCache.Snapshot snapshot = cache.get(key);
        if (snapshot != null) {
            String data = snapshot.getString(0);
            JsonObject entryJson = gson.fromJson(data, JsonObject.class);
            Entry entry = gson.fromJson(entryJson, Entry.class);
            if (!entry.hasExpired()) {
                JsonElement element = entryJson.get(Entry.VALUE);
                if (element.isJsonPrimitive()) {
                    return gson.fromJson(element.getAsJsonPrimitive(), classOfT);
                }
                return gson.fromJson(element.getAsJsonObject(), classOfT);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
 
开发者ID:myntra,项目名称:ObjectCache,代码行数:22,代码来源:ObjectCache.java

示例8: getCachedTile

import com.jakewharton.disklrucache.DiskLruCache; //导入依赖的package包/类
/**
 * Load a tile from cache.
 * Returns null if there is no corresponding cache entry or it could not be loaded.
 */
private Tile getCachedTile(String key) {
    if (mCache.isClosed()) {
        return null;
    }
    try {
        DiskLruCache.Snapshot snapshot = mCache.get(key);
        if (snapshot == null) {
            // tile is not in cache
            return null;
        }

        final byte[] data = readStreamAsByteArray(snapshot.getInputStream(INDEX_DATA));
        final int height = readStreamAsInt(snapshot.getInputStream(INDEX_HEIGHT));
        final int width = readStreamAsInt(snapshot.getInputStream(INDEX_WIDTH));
        if (data != null) {
            LOGD(TAG, "Cache hit for tile " + key);
            return new Tile(width, height, data);
        }

    } catch (IOException e) {
        // ignore error
    }
    return null;
}
 
开发者ID:dreaminglion,项目名称:iosched-reader,代码行数:29,代码来源:CachedTileProvider.java

示例9: cacheTile

import com.jakewharton.disklrucache.DiskLruCache; //导入依赖的package包/类
private boolean cacheTile(String key, Tile tile) {
    if (mCache.isClosed()) {
        return false;
    }
    try {
        DiskLruCache.Editor editor = mCache.edit(key);
        if (editor == null) {
            // editor is not available
            return false;
        }
        writeByteArrayToStream(tile.data, editor.newOutputStream(INDEX_DATA));
        writeIntToStream(tile.height, editor.newOutputStream(INDEX_HEIGHT));
        writeIntToStream(tile.width, editor.newOutputStream(INDEX_WIDTH));
        editor.commit();
        return true;
    } catch (IOException e) {
        // Tile could not be cached
    }
    return false;
}
 
开发者ID:dreaminglion,项目名称:iosched-reader,代码行数:21,代码来源:CachedTileProvider.java

示例10: open

import com.jakewharton.disklrucache.DiskLruCache; //导入依赖的package包/类
/**
 * 打开DiskLruCache,然后才能进行Cache操作,该方法只需执行一次,多次执行无效。
 * @param context Context 上下文环境
 * @param cacheType CACHE_TYPE 缓存数据类型
 * @param maxCacheSize int 自定义最大缓存,0~MAX_CACHE_SIZE(10M)之间,<=0则使用默认的最大值
 * @return 返回单例实例,提供链式调用支持
 */
public DiskCacheHelper open(Context context, CACHE_TYPE cacheType, int maxCacheSize) {
    if (mInstance != null && mDiskLruCache != null) {
        return mInstance;
    }
    try {
        File cacheDir = getDiskCacheDir(context, cacheType);

        if (maxCacheSize > 0 && maxCacheSize <= MAX_CACHE_SIZE) {
            mMaxCacheSize = maxCacheSize;
        } else {
            mMaxCacheSize = MAX_CACHE_SIZE;
        }
        mDiskLruCache = DiskLruCache.open(cacheDir, getAppVersion(context), 1, mMaxCacheSize);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return mInstance;
}
 
开发者ID:ymqq,项目名称:CommonFramework,代码行数:26,代码来源:DiskCacheHelper.java

示例11: writeBitmap

import com.jakewharton.disklrucache.DiskLruCache; //导入依赖的package包/类
/**
 * 缓存Bitmap数据
 * @param key String 缓存数据对应的文件名称,唯一值
 * @param bitmap Bitmap 需要缓存的图片数据
 * @return 返回单例实例,提供链式调用支持
 */
public DiskCacheHelper writeBitmap(String key, Bitmap bitmap) {
    try {
        String cacheKey = hashKeyForDisk(key);
        DiskLruCache.Editor editor = mDiskLruCache.edit(cacheKey);
        if (editor != null) {
            String bitmapString = bitmapToString(bitmap);
            if (bitmapString != null) {
                editor.set(0, bitmapString);
                editor.commit();
            } else {
                editor.abort();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        return mInstance;
    }
}
 
开发者ID:ymqq,项目名称:CommonFramework,代码行数:26,代码来源:DiskCacheHelper.java

示例12: readBitmap

import com.jakewharton.disklrucache.DiskLruCache; //导入依赖的package包/类
/**
 * 读取缓存的图片资源
 * @param key String 缓存数据对应的文件名称,唯一值
 * @return 返回Bitmap
 */
public Bitmap readBitmap(String key) {
    Bitmap bitmap = null;
    try {
        String cacheKey = hashKeyForDisk(key);
        DiskLruCache.Snapshot snapshot = mDiskLruCache.get(cacheKey);
        if (snapshot != null) {
            InputStream is = snapshot.getInputStream(0);
            bitmap = BitmapFactory.decodeStream(is);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        return bitmap;
    }
}
 
开发者ID:ymqq,项目名称:CommonFramework,代码行数:21,代码来源:DiskCacheHelper.java

示例13: writeData

import com.jakewharton.disklrucache.DiskLruCache; //导入依赖的package包/类
/**
 * 缓存数据对象,以JSON字符串格式保存
 * @param key String 缓存数据对应的文件名称,唯一值
 * @param data 需要缓存的JSON字符串
 * @return 返回单例实例,提供链式调用支持
 */
public DiskCacheHelper writeData(String key, String data) {
    try {
        String cacheKey = hashKeyForDisk(key);
        DiskLruCache.Editor editor = mDiskLruCache.edit(cacheKey);
        if (editor != null) {
            if (data != null) {
                editor.set(0, data);
                editor.commit();
            } else {
                editor.abort();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        return mInstance;
    }
}
 
开发者ID:ymqq,项目名称:CommonFramework,代码行数:25,代码来源:DiskCacheHelper.java

示例14: ImageLoader

import com.jakewharton.disklrucache.DiskLruCache; //导入依赖的package包/类
private ImageLoader(Context context) {
    mContext = context.getApplicationContext();
    int maxMemory = (int) Runtime.getRuntime().maxMemory();
    int cacheSize = maxMemory / 8;
    mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
        @Override
        protected int sizeOf(String key, Bitmap value) {
            return value.getRowBytes() * value.getHeight() / 1024;
        }
    };
    File diskCacheDir = getDiskCacheDir(mContext, "bitmap");
    if (!diskCacheDir.exists()) {
        diskCacheDir.mkdirs();
    }

    if (getUsableSpace(diskCacheDir) > DISK_CACHE_SIZE) {
        try {
            mDiskLruCache = DiskLruCache.open(diskCacheDir, 1, 1, DISK_CACHE_SIZE);
            mIsDiskLruCacheCreated = true;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
 
开发者ID:wuhighway,项目名称:DailyStudy,代码行数:26,代码来源:ImageLoader.java

示例15: loadBitmapFromHttp

import com.jakewharton.disklrucache.DiskLruCache; //导入依赖的package包/类
/**
 * 加载网络图片缓存到磁盘中
 * @param uri
 * @param reqWidth
 * @param reqHeight
 * @return
 */
private Bitmap loadBitmapFromHttp(String uri, int reqWidth, int reqHeight) {
    if (Looper.myLooper() == Looper.getMainLooper()) {
        throw new RuntimeException("can not visit network from UI thread.");
    }
    if (mDiskLruCache == null) {
        return null;
    }
    String key = hashKeyFromUri(uri);
    try {
        DiskLruCache.Editor editor = mDiskLruCache.edit(key);
        if (editor != null) {
            OutputStream outputStream = editor.newOutputStream(DISK_CACHE_INDEX);
            if (downloadBitmapToStream(uri, outputStream)){
                editor.commit();
            } else {
                editor.abort();
            }
            mDiskLruCache.flush();
            return loadBitmapFromDisCache(uri, reqWidth, reqHeight);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}
 
开发者ID:wuhighway,项目名称:DailyStudy,代码行数:33,代码来源:ImageLoader.java


注:本文中的com.jakewharton.disklrucache.DiskLruCache类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。