本文整理匯總了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;
}