/** Sets the value at {@code index} to {@code value}. */ public void set(int index, String value) throws IOException { Writer writer = null; try { writer = new OutputStreamWriter(newOutputStream(index), Util.UTF_8); writer.write(value); } finally { Util.closeQuietly(writer); } }
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; }
os = editor.newOutputStream(0); bitmap.compress(compressFormat, compressQuality, os); os.flush();
outputStream = editor.newOutputStream(1); outputStream.write(bytes); outputStream.flush();
return; out = editor.newOutputStream(0); out.write(bytes); out.flush();
return; oos = new ObjectOutputStream(editor.newOutputStream(0)); oos.writeObject(object); oos.flush();
Editor editor = getEditor(request); OutputStream os = new BufferedOutputStream(editor.newOutputStream(INPUT_IMAGE), BUFFER_SIZE); try { try {
return; OutputStream os = editor.newOutputStream(0); writer = new BufferedWriter(new OutputStreamWriter(os)); writer.write(value);
@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(); } }
@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; }
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; }
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(); } }
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
/** * Initialize an url fetch * * @param url The url being fetched * @param payload A NetworkPacket Payload (if from the connected device). null if fetched from http(s) * @param cacheItem The disk cache item to edit */ FetchURLTask(URL url, NetworkPacket.Payload payload, DiskLruCache.Editor cacheItem) throws IOException { this.url = url; this.payload = payload; this.cacheItem = cacheItem; output = cacheItem.newOutputStream(0); }
DiskLruCache.Editor editor = mHttpDiskCache.edit(key); if (editor != null) { if (downloadUrlToStream(data,editor.newOutputStream(DISK_CACHE_INDEX))) { editor.commit(); } else { editor.abort(); } }
private void writeBitmap(Editor editor, Bitmap b, Bitmap.CompressFormat format) throws IOException { OutputStream os = new BufferedOutputStream(editor.newOutputStream(INPUT_IMAGE), BUFFER_SIZE); try { b.compress(format, Constants.DEFAULT_COMPRESS_QUALITY, os); } finally { IOUtil.closeQuietly(os); } } }
private void writeMetadata(Editor editor, Metadata metadata) throws IOException { OutputStream os = new BufferedOutputStream(editor.newOutputStream(INPUT_METADATA), BUFFER_SIZE); try { metadata.writeTo(os); } finally { IOUtil.closeQuietly(os); } }
IoUtils.copy(tmpFile, editor.newOutputStream(0)); editor.commit(); } catch (IOException e) {