bitmap.compress(compressFormat, compressQuality, os); os.flush(); editor.commit(); } catch (IOException e) { Log.e(Constants.LOG_TAG, "Error while writing to disk cache", e);
DiskLruCache.Editor editor = mDiskCache.edit(key); IoUtils.copy(tmpFile, editor.newOutputStream(0)); editor.commit(); } catch (IOException e) { Log.e(Constants.LOG_TAG, "Error writing to disk cache. URL: " + url, e);
public void put(String key, Object object) { DiskLruCache.Editor editor = null; try { editor = mDiskCache.edit(key); if (editor == null) { return; } ObjectOutputStream out = new ObjectOutputStream(editor.newOutputStream(0)); out.writeObject(object); out.close(); editor.commit() } catch()...etc
editor.set(0, diskSerializer.toString(object)); editor.commit(); } catch (IOException e) { logger.logError(e);
editor.set(0, diskSerializer.toString(object)); editor.commit(); } catch (IOException e) { logger.logError(e);
try { if (success) { cacheItem.commit(); } else { cacheItem.abort();
@Override public void put(String key, File value, CacheLoadFinishListener listener) { if (diskLruCache == null || value == null || TextUtils.isEmpty(key)) { return; } DiskLruCache.Editor editor = null; try { String cacheKey = cacheKeyProvider.buildCacheKey(key); editor = diskLruCache.edit(cacheKey); if (editor != null) { OutputStream os = editor.newOutputStream(0); if (writeCache(value, os) && listener != null) { listener.onFinished(); } editor.commit(); } diskLruCache.flush(); } catch (IOException e) { try { if (editor != null) { editor.abort(); } } catch (IOException e1) { e1.printStackTrace(); } } }
/** * 下载图片放入本地缓存 * @param urlString 下载图片的链接 * @throws IOException */ public void downloadImageToDiskCache(String urlString) throws IOException { if(mDiskLruCache == null){ return ; } String key =MD5Utils.hashKeyFromUrl(urlString); DiskLruCache.Editor editor =mDiskLruCache.edit(key); if(editor != null){ //打开本地缓存的输入流 OutputStream outputStream =editor.newOutputStream(DISK_CACHE_INDEX); //将从网络下载并写入输出流中 if(NetRequest.downloadUrlToStream(urlString,outputStream)){ //提交数据,并是释放连接 editor.commit(); }else{ //释放连接 editor.abort(); } mDiskLruCache.flush(); } }
public CacheableBitmapDrawable putInDiskCache(final String url, final CacheableBitmapDrawable drawable, Bitmap.CompressFormat compressFormat, int compressQuality) { if (null != mDiskCache) { checkNotOnMainThread(); final String key = transformUrlForDiskCacheKey(url); final ReentrantLock lock = getLockForDiskCacheEdit(key); lock.lock(); OutputStream os = null; try { DiskLruCache.Editor editor = mDiskCache.edit(key); os = editor.newOutputStream(0); drawable.getBitmap().compress(compressFormat, compressQuality, os); os.flush(); editor.commit(); } catch (IOException e) { Log.e(Constants.LOG_TAG, "Error while writing to disk cache", e); } finally { IoUtils.closeStream(os); lock.unlock(); scheduleDiskCacheFlush(); } } return drawable; }
@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; }
@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; }
private void putToDiskQuietly(String key, V newValue) { if (mDiskCache == null) { return; } Editor editor = null; OutputStream out = null; try { editor = mDiskCache.edit(key); if (editor != null) { out = editor.newOutputStream(INDEX_VALUE); mConverter.toStream(newValue, out); editor.commit(); } } catch (IOException e) { System.out .println("Unable to put entry to disk cache. key: " + key); } finally { IOUtils.closeQuietly(out); quietlyAbortUnlessCommitted(editor); } }
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(); } }
@Override public void onBitmapLoaded(Bitmap b, Metadata metadata) { try { Editor editor = getEditor(request); try { Bitmap.CompressFormat format = BitmapUtils.getCompressFormat(metadata.getContentType()); writeBitmap(editor, b, format); writeMetadata(editor, metadata); editor.commit(); } finally { editor.abortUnlessCommitted(); } } catch (IOException e) { Log.e(TAG, "Failed saving bitmap to cache", e); } // We can always pass on the bitmap we got, even if // we didn't manage to write it to cache manager.deliverBitmap(b, metadata); }
@Override public void onNotModified(Metadata metadata) { try { Editor editor = getEditor(request); try { writeMetadata(editor, metadata); editor.commit(); } finally { editor.abortUnlessCommitted(); } } catch (IOException e) { Log.e(TAG, "Failed to update metadata", e); } manager.deliverNotMotified(metadata); }
@Override public <T> void addInCache(Response<T> response, byte[] rawResponse) { String cacheKey = urlToKey(response.raw().request().url().url()); memoryCache.put(cacheKey, rawResponse); try { DiskLruCache.Editor editor = diskCache.edit(urlToKey(response.raw().request().url().url())); editor.set(0, new String(rawResponse, Charset.defaultCharset())); editor.commit(); }catch(IOException exc){ Log.e("SmartCall", "", exc); } }
@Override public <T> void addInCache(Response<T> response, byte[] rawResponse) { String cacheKey = urlToKey(response.raw().request().url().url()); memoryCache.put(cacheKey, rawResponse); try { DiskLruCache.Editor editor = diskCache.edit(urlToKey(response.raw().request().url().url())); editor.set(0, new String(rawResponse, Charset.defaultCharset())); editor.commit(); }catch(IOException exc){ Log.e("SmartCall", "", exc); } }
@Override public <T> void addInCache(Response<T> response, byte[] rawResponse) { String cacheKey = urlToKey(response.raw().request().url().url()); memoryCache.put(cacheKey, rawResponse); try { DiskLruCache.Editor editor = diskCache.edit(urlToKey(response.raw().request().url().url())); editor.set(0, new String(rawResponse, Charset.defaultCharset())); editor.commit(); Log.e("SmartCall", "c缓存成功"); }catch(IOException exc){ Log.e("SmartCall", "", exc); } }
DiskLruCache.Editor editor = mHttpDiskCache.edit(key); if (editor != null) { if (downloadUrlToStream(data,editor.newOutputStream(DISK_CACHE_INDEX))) { editor.commit(); } else { editor.abort(); } }
@Override public Bitmap put(String key, Bitmap data) { if (contains(key)) { return data; } DiskLruCache.Editor editor = null; try { editor = mDiskCache.edit(key); if (editor != null) { if (writeBitmapToCache(data, editor)) { mDiskCache.flush(); editor.commit(); } else { editor.abort(); } } } catch (IOException e) { try { if (editor != null) { editor.abort(); } } catch (IOException ignored) { // } } return data; }