本文整理汇总了Java中com.jakewharton.disklrucache.DiskLruCache.Snapshot方法的典型用法代码示例。如果您正苦于以下问题:Java DiskLruCache.Snapshot方法的具体用法?Java DiskLruCache.Snapshot怎么用?Java DiskLruCache.Snapshot使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.jakewharton.disklrucache.DiskLruCache
的用法示例。
在下文中一共展示了DiskLruCache.Snapshot方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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;
}
示例2: 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();
}
}
}
示例3: 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 "";
}
示例4: 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;
}
示例5: 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;
}
示例6: 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;
}
}
示例7: get
import com.jakewharton.disklrucache.DiskLruCache; //导入方法依赖的package包/类
@Nullable
public synchronized String get(@NonNull BaseCollectionSubscription subscription) throws IOException, JSONException, NoSuchAlgorithmException {
if(!mEnabled)
return null;
String fingerprint = subscription.getFingerprint();
DiskLruCache.Snapshot record = mCache.get(fingerprint);
if(record != null) {
InputStream inputstream = record.getInputStream(DEFAULT_INDEX);
byte[] rec = StreamUtility.toByteArray(inputstream);
String jsonValue = XorUtility.xor(rec, subscription.getAuthToken());
Logcat.d("Reading from subscription cache. key=%s; value=%s", fingerprint, jsonValue);
JSONArray documentIdArray = new JSONArray(jsonValue);
JSONArray documentArray = new JSONArray();
for(int i = 0; i < documentIdArray.length(); i++) {
String document = getDocument(subscription, documentIdArray.optString(i));
if(document == null) {
return null;
}
documentArray.put(new JSONObject(document));
}
return documentArray.toString();
}
Logcat.d("Reading from disk subscription cache. key=%s; value=null", fingerprint);
return null;
}
示例8: performTask
import com.jakewharton.disklrucache.DiskLruCache; //导入方法依赖的package包/类
@Override
public Page performTask() throws Throwable {
synchronized (mDiskCacheLock) {
if (mDiskLruCache == null) {
return null;
}
String key = title.getIdentifier();
DiskLruCache.Snapshot snapshot = mDiskLruCache.get(key);
if (snapshot == null) {
return null;
}
try {
Log.d(TAG, "Reading from cache: " + title.getDisplayText());
InputStream inputStream = new BufferedInputStream(snapshot.getInputStream(0));
String jsonStr = readFile(inputStream);
return new Page(new JSONObject(jsonStr));
} finally {
snapshot.close();
}
}
}
示例9: isCached
import com.jakewharton.disklrucache.DiskLruCache; //导入方法依赖的package包/类
@Override
public boolean isCached(String cacheKey) {
String key = generateKey(cacheKey);
boolean isInMem = mMemCache.get(key) != null;
boolean isInDisk = false;
if (getDiskCache() != null) {
try {
DiskLruCache.Snapshot snapshot = getDiskCache().get(key);
if (snapshot != null) {
isInDisk = snapshot.getInputStream(0) != null;
}
} catch (Exception e) {
e.printStackTrace();
}
}
return isInMem && isInDisk;
}
示例10: get
import com.jakewharton.disklrucache.DiskLruCache; //导入方法依赖的package包/类
public static Comment get(String key){
try {
if(diskLrucache==null){
throw new IllegalStateException("»º´æ²»´æÔÚ");
}
DiskLruCache.Snapshot snapshot=diskLrucache.get(hashKeyForDisk(key));
if(snapshot!=null){
InputStream in=snapshot.getInputStream(0);
// Bitmap bitmap=BitmapFactory.decodeStream(in);
//return bitmap;
}
} catch (Exception e) {
// TODO: handle exception
}
return null;
}
示例11: getFromCache
import com.jakewharton.disklrucache.DiskLruCache; //导入方法依赖的package包/类
/**
* @param request
* @return
* 从两种缓存中读取数据
*/
@Override
public ResponseBody getFromCache(Request request) {
String cacheKey = urlToKey(request.url());
byte[] memoryResponse = (byte[]) memoryCache.get(cacheKey);
if (memoryResponse != null) {
Log.d("BasicCache", "从内存读取数据!");
return ResponseBody.create(null, memoryResponse);
}
try {
DiskLruCache.Snapshot cacheSnapshot = diskCache.get(cacheKey);
if (cacheSnapshot != null) {
Log.d("BasicCache", "从磁盘读取数据!");
return ResponseBody.create(null, cacheSnapshot.getString(0).getBytes());
} else {
return null;
}
} catch (IOException exc) {
return null;
}
}
示例12: readMetadata
import com.jakewharton.disklrucache.DiskLruCache; //导入方法依赖的package包/类
private Map<String, Serializable> readMetadata(DiskLruCache.Snapshot snapshot)
throws IOException {
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(new BufferedInputStream(
snapshot.getInputStream(METADATA_IDX)));
@SuppressWarnings("unchecked")
Map<String, Serializable> annotations = (Map<String, Serializable>) ois.readObject();
return annotations;
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} finally {
if(ois != null)
ois.close();
}
}
示例13: getImage
import com.jakewharton.disklrucache.DiskLruCache; //导入方法依赖的package包/类
@Override
public InputStream getImage(String url) {
synchronized (CACHE_LOCK) {
if (diskLruCache == null) {
return null;
}
}
String key = getKey(url);
try {
DiskLruCache.Snapshot snapshot = diskLruCache.get(key);
return snapshot != null ? snapshot.getInputStream(0) : null;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
示例14: containsKey
import com.jakewharton.disklrucache.DiskLruCache; //导入方法依赖的package包/类
public boolean containsKey( String key ) {
boolean contained = false;
DiskLruCache.Snapshot snapshot = null;
try {
snapshot = mDiskCache.get( key );
contained = snapshot != null;
} catch (IOException e) {
e.printStackTrace();
} finally {
if ( snapshot != null ) {
snapshot.close();
}
}
return contained;
}
示例15: get
import com.jakewharton.disklrucache.DiskLruCache; //导入方法依赖的package包/类
public static Bitmap get(String key){
try {
if(diskLrucache==null){
throw new IllegalStateException("���治����");
}
DiskLruCache.Snapshot snapshot=diskLrucache.get(hashKeyForDisk(key));
if(snapshot!=null){
InputStream in=snapshot.getInputStream(0);
Bitmap bitmap=BitmapFactory.decodeStream(in);
return bitmap;
}
} catch (Exception e) {
// TODO: handle exception
}
return null;
}