private boolean writeBitmapToCache(Bitmap bitmap, DiskLruCache.Editor editor) { OutputStream outputStream = null; try { outputStream = editor.newOutputStream(0); outputStream = new BufferedOutputStream(outputStream, IO_BUFFER_SIZE); return bitmap.compress(CompressFormat.PNG, 0, outputStream); } catch (Exception e) { // no op } finally { try { if (outputStream != null) { outputStream.close(); } } catch (IOException e) { // no op } } return false; }
final DiskLruCache.Editor editor = mDiskLruCache.edit(key); if (editor != null) { out = editor.newOutputStream(DISK_CACHE_INDEX); bitmap.compress( mCacheParams.compressFormat, mCacheParams.compressQuality, out); editor.commit(); out.close();
out = editor.newOutputStream(DISK_CACHE_INDEX); editor.commit(); out.close();
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) {
DiskLruCache diskCache = getDiskCache(); edit = diskCache == null ? null : diskCache.edit(key); outputStream = edit == null ? null : new BufferedOutputStream(edit.newOutputStream(0)); outputStream.flush(); edit.set(1, ifModifiedSince == null ? "" : ifModifiedSince); edit.commit(); edit.abortUnlessCommitted();
DiskLruCache.Editor editor = cache.edit(key); if (editor != null) { if (downloadUrlToStream(data, editor.newOutputStream(DISK_CACHE_INDEX))) editor.commit(); else editor.abort();
IoUtils.copy(tmpFile, editor.newOutputStream(0)); editor.commit(); } catch (IOException e) { Log.e(Constants.LOG_TAG, "Error writing to disk cache. URL: " + url, e);
return false; editor.set(0, String.valueOf(bytes.length)); outputStream = editor.newOutputStream(1); outputStream.write(bytes); outputStream.flush(); editor.commit(); editor = null; return true; if (null != editor) { try { editor.commit(); } catch (IOException e) { e.printStackTrace();
return; out = editor.newOutputStream(0); out.write(bytes); out.flush(); editor.commit(); } catch (IOException e) { e.printStackTrace(); try { if (editor != null) { editor.abort();
return; oos = new ObjectOutputStream(editor.newOutputStream(0)); oos.writeObject(object); oos.flush(); editor.commit(); } catch (IOException e) { e.printStackTrace(); try { if (editor != null) editor.abort(); } catch (IOException e1) { e1.printStackTrace();
Editor editor = getEditor(request); OutputStream os = new BufferedOutputStream(editor.newOutputStream(INPUT_IMAGE), BUFFER_SIZE); try { try { editor.commit(); editor.abort(); manager.deliverError(e);
return; OutputStream os = editor.newOutputStream(0); writer = new BufferedWriter(new OutputStreamWriter(os)); writer.write(value); editor.commit(); } catch (IOException e) { e.printStackTrace(); try { if (editor != null) editor.abort(); } catch (IOException e1) { e1.printStackTrace();
@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; }