当前位置: 首页>>代码示例>>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;未经允许,请勿转载。