當前位置: 首頁>>代碼示例>>Java>>正文


Java DiskLruCache.Editor方法代碼示例

本文整理匯總了Java中com.jakewharton.disklrucache.DiskLruCache.Editor方法的典型用法代碼示例。如果您正苦於以下問題:Java DiskLruCache.Editor方法的具體用法?Java DiskLruCache.Editor怎麽用?Java DiskLruCache.Editor使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在com.jakewharton.disklrucache.DiskLruCache的用法示例。


在下文中一共展示了DiskLruCache.Editor方法的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: 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

示例3: 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

示例4: 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

示例5: 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

示例6: 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

示例7: 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

示例8: 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

示例9: put

import com.jakewharton.disklrucache.DiskLruCache; //導入方法依賴的package包/類
public synchronized void put(@NonNull BaseCollectionSubscription subscription, String jsonValue) throws IOException, JSONException, 	NoSuchAlgorithmException {
	if(!mEnabled)
		return;

	JSONArray documentArray = new JSONArray(jsonValue);
	JSONArray documentIdArray = new JSONArray();
	for(int i = 0; i < documentArray.length(); i++) {
		JSONObject document = documentArray.getJSONObject(i);
		String documentId = Sha1Utility.sha1(document.optString(RapidDocument.KEY_ID));
		documentIdArray.put(documentId);
		putDocument(subscription, documentId, document.toString(), subscription.getAuthToken());
	}

	String documentIdArrayJson = documentIdArray.toString();
	String fingerprint = subscription.getFingerprint();
	DiskLruCache.Editor editor = mCache.edit(fingerprint);
	OutputStream out = editor.newOutputStream(DEFAULT_INDEX);
	out.write(XorUtility.xor(documentIdArrayJson, subscription.getAuthToken()));
	out.flush();
	out.close();
	editor.commit();
	Logcat.d("Saving to disk subscription cache. key=%s; value=%s", fingerprint, documentIdArrayJson);
}
 
開發者ID:rapid-io,項目名稱:rapid-io-android,代碼行數:24,代碼來源:SubscriptionDiskCache.java

示例10: performTask

import com.jakewharton.disklrucache.DiskLruCache; //導入方法依賴的package包/類
@Override
public Void performTask() throws Throwable {
    synchronized (mDiskCacheLock) {
        if (mDiskLruCache == null) {
            return null;
        }
        DiskLruCache.Editor editor = null;
        try {
            Log.d(TAG, "Writing to cache: " + title.getDisplayText());
            String key = title.getIdentifier();
            editor = mDiskLruCache.edit(key);
            if (editor == null) {
                return null;
            }
            OutputStream outputStream = new BufferedOutputStream(editor.newOutputStream(0));
            writeToStream(outputStream, page.toJSON().toString());
            mDiskLruCache.flush();
            editor.commit();
        } catch (IOException e) {
            if (editor != null) {
                editor.abort();
            }
        }
    }
    return null;
}
 
開發者ID:gnosygnu,項目名稱:xowa_android,代碼行數:27,代碼來源:PageCache.java

示例11: put

import com.jakewharton.disklrucache.DiskLruCache; //導入方法依賴的package包/類
public static void put(String key, Comment comment){
    try {

        if(diskLrucache==null){
            throw new IllegalStateException("»º´æ²»´æÔÚ");
        }
        DiskLruCache.Editor editor=diskLrucache.edit(hashKeyForDisk(key));
        OutputStream out=editor.newOutputStream(0);
        out.write(comment.toString().getBytes());
        //boolean success=values.compress(Bitmap.CompressFormat.PNG, 100, out);
        //if (success) {
        editor.commit();
        diskLrucache.flush();
        //}
        //else{
        //    editor.abort();
       // }

    } catch (Exception e) {
        // TODO: handle exception
    }
}
 
開發者ID:liudabao,項目名稱:Evisa,代碼行數:23,代碼來源:DiskLrucacheHelper.java

示例12: put

import com.jakewharton.disklrucache.DiskLruCache; //導入方法依賴的package包/類
public synchronized void put(double lat, double lon, WeatherData weatherData) throws IOException {
    OutputStream bos = null;
    DiskLruCache.Editor editor;
    try {
        editor = mDiskLruCache.edit(getKey(lat, lon));
        if (editor != null) {
            bos = editor.newOutputStream(DISK_CACHE_INDEX);
            String json = mGson.toJson(weatherData);
            bos.write(json.getBytes());
            editor.commit();
        }
    } catch (IOException e) {
        throw new IOException(e);
    } finally {
        if (bos != null) {
            try {
                bos.close();
            } catch (IOException ignored) {}
        }
    }
}
 
開發者ID:Clans,項目名稱:RxWeather,代碼行數:22,代碼來源:DiskCacheManager.java

示例13: addInCache

import com.jakewharton.disklrucache.DiskLruCache; //導入方法依賴的package包/類
/**
 * 添加到 緩存
 */
@Override
public void addInCache(Request request, Buffer buffer) {
    byte[] rawResponse = buffer.readByteArray();
    String cacheKey = urlToKey(request.url());// 用網址作為 key

    // 添加到內存
    memoryCache.put(cacheKey, rawResponse);

    // 添加到磁盤
    try {
        DiskLruCache.Editor editor = diskCache.edit(urlToKey(request.url()));
        editor.set(0, new String(rawResponse, Charset.defaultCharset()));
        editor.commit();
    } catch (IOException exc) {
        Log.e("BasicCache", "", exc);
    }
}
 
開發者ID:xqgdmg,項目名稱:Retrofit-RxJava,代碼行數:21,代碼來源:BasicCache.java

示例14: writeBitmapToDisk

import com.jakewharton.disklrucache.DiskLruCache; //導入方法依賴的package包/類
private boolean writeBitmapToDisk(InputStream is, String key) {
	OutputStream os = null;
	try {
		DiskLruCache.Editor editor = getDiskEditor(key);
		
		if(editor != null) {
			os = editor.newOutputStream(0);
			
			if(IoUtils.copy(is, os)) {
				editor.commit();
				return true;
			}
		}
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		IoUtils.closeStream(os);
	}
	return false;
}
 
開發者ID:dolpphins,項目名稱:KImageLoader,代碼行數:21,代碼來源:BitmapDiskLruCache.java

示例15: editor

import com.jakewharton.disklrucache.DiskLruCache; //導入方法依賴的package包/類
public DiskLruCache.Editor editor(String key) {
    try {
        key = Utils.hashKeyForDisk(key);
        //wirte DIRTY
        DiskLruCache.Editor edit = mDiskLruCache.edit(key);
        //edit maybe null :the entry is editing
        if (edit == null) {
            Log.w(TAG, "the entry spcified key:" + key + " is editing by other . ");
        }
        return edit;
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}
 
開發者ID:lujianzhao,項目名稱:AndroidBase,代碼行數:17,代碼來源:DiskLruCacheHelper.java


注:本文中的com.jakewharton.disklrucache.DiskLruCache.Editor方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。