public void put(String key, String value) { DiskLruCache.Editor edit = null; BufferedWriter bw = null; try { edit = editor(key); if (edit == null) return; OutputStream os = edit.newOutputStream(0); bw = new BufferedWriter(new OutputStreamWriter(os)); bw.write(value); edit.commit(); // write CLEAN } catch (IOException e) { e.printStackTrace(); try { // s edit.abort(); // write REMOVE } catch (IOException e1) { e1.printStackTrace(); } } finally { try { if (bw != null) bw.close(); } catch (IOException e) { e.printStackTrace(); } } }
// ======================================= // ============== 序列化 数据 读写 ============= // ======================================= public void put(String key, Serializable value) { DiskLruCache.Editor editor = editor(key); ObjectOutputStream oos = null; if (editor == null) return; try { OutputStream os = editor.newOutputStream(0); oos = new ObjectOutputStream(os); oos.writeObject(value); oos.flush(); editor.commit(); } catch (IOException e) { e.printStackTrace(); try { editor.abort(); } catch (IOException e1) { e1.printStackTrace(); } } finally { try { if (oos != null) oos.close(); } catch (IOException e) { e.printStackTrace(); } } }
/** * 保存 byte数据 到 缓存中 * * @param key 保存的key * @param value 保存的数据 */ public void put(String key, byte[] value) { OutputStream out = null; DiskLruCache.Editor editor = null; try { editor = editor(key); if (editor == null) { return; } out = editor.newOutputStream(0); out.write(value); out.flush(); editor.commit(); // write CLEAN } catch (Exception e) { e.printStackTrace(); try { editor.abort(); // write REMOVE } catch (IOException e1) { e1.printStackTrace(); } } finally { if (out != null) { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } }
@Override public boolean save(String imageUri, Bitmap bitmap) throws IOException { DiskLruCache.Editor editor = cache.edit(getKey(imageUri)); if (editor == null) { return false; } OutputStream os = new BufferedOutputStream(editor.newOutputStream(0), bufferSize); boolean savedSuccessfully = false; try { savedSuccessfully = bitmap.compress(compressFormat, compressQuality, os); } finally { IoUtils.closeSilently(os); } if (savedSuccessfully) { editor.commit(); } else { editor.abort(); } return savedSuccessfully; }
@Override public boolean save(String imageUri, InputStream imageStream, IoUtils.CopyListener listener) throws IOException { DiskLruCache.Editor editor = cache.edit(getKey(imageUri)); if (editor == null) { return false; } OutputStream os = new BufferedOutputStream(editor.newOutputStream(0), bufferSize); boolean copied = false; try { copied = IoUtils.copyStream(imageStream, os, listener, bufferSize); } finally { IoUtils.closeSilently(os); if (copied) { editor.commit(); } else { editor.abort(); } } return copied; }
/** * The main process method, which will be called by the ImageWorker in the AsyncTask background * thread. * * @param data The data to load the bitmap, in this case, a regular http URL * @return The downloaded and resized bitmap */ private Bitmap processBitmap(String data) { if (CacheConfig.DEBUG) { Log.d(TAG, "processBitmap - " + data); } final String key = ImageCache.hashKeyForDisk(data); FileDescriptor fileDescriptor = null; FileInputStream fileInputStream = null; DiskLruCache.Snapshot snapshot; synchronized (mHttpDiskCacheLock) { // Wait for disk cache to initialize while (mHttpDiskCacheStarting) { try { mHttpDiskCacheLock.wait(); } catch (InterruptedException e) { } } if (mHttpDiskCache != null) { try { snapshot = mHttpDiskCache.get(key); if (snapshot == null) { if (CacheConfig.DEBUG) { Log.d(TAG, "processBitmap, not found in http cache, downloading..."); } DiskLruCache.Editor editor = mHttpDiskCache.edit(key); if (editor != null) { if (downloadUrlToStream(data, editor.newOutputStream(DISK_CACHE_INDEX))) { editor.commit(); } else { editor.abort(); } } snapshot = mHttpDiskCache.get(key); } if (snapshot != null) { fileInputStream = (FileInputStream) snapshot.getInputStream(DISK_CACHE_INDEX); fileDescriptor = fileInputStream.getFD(); } } catch (IOException e) { Log.e(TAG, "processBitmap - " + e); } catch (IllegalStateException e) { Log.e(TAG, "processBitmap - " + e); } finally { if (fileDescriptor == null && fileInputStream != null) { try { fileInputStream.close(); } catch (IOException e) { } } } } } Bitmap bitmap = null; if (fileDescriptor != null) { bitmap = decodeSampledBitmapFromDescriptor( fileDescriptor, mImageWidth, mImageHeight, getImageCache()); } if (fileInputStream != null) { try { fileInputStream.close(); } catch (IOException e) { } } return bitmap; }