File file = editor.getFile(0); if (writer.write(file)) { editor.commit();
/** Sets the value at {@code index} to {@code value}. */ public void set(int index, String value) throws IOException { Writer writer = null; try { OutputStream os = new FileOutputStream(getFile(index)); writer = new OutputStreamWriter(os, Util.UTF_8); writer.write(value); } finally { Util.closeQuietly(writer); } }
@Override public void put(Key key, Writer writer) { String safeKey = safeKeyGenerator.getSafeKey(key); writeLocker.acquire(key); try { DiskLruCache.Editor editor = getDiskCache().edit(safeKey); // Editor will be null if there are two concurrent puts. In the worst case we will just silently fail. if (editor != null) { try { File file = editor.getFile(0); if (writer.write(file)) { editor.commit(); } } finally { editor.abortUnlessCommitted(); } } } catch (IOException e) { if (Log.isLoggable(TAG, Log.WARN)) { Log.w(TAG, "Unable to put to disk cache", e); } } finally { writeLocker.release(key); } }
File file = editor.getFile(0); if (writer.write(file)) { editor.commit();
private static void assertInoperable(DiskLruCache.Editor editor) throws Exception { try { editor.getString(0); Assert.fail(); } catch (IllegalStateException expected) { } try { editor.set(0, "A"); Assert.fail(); } catch (IllegalStateException expected) { } try { editor.getFile(0); Assert.fail(); } catch (IllegalStateException expected) { } try { editor.commit(); Assert.fail(); } catch (IllegalStateException expected) { } try { editor.abort(); Assert.fail(); } catch (IllegalStateException expected) { } }
/** Sets the value at {@code index} to {@code value}. */ public void set(int index, String value) throws IOException { Writer writer = null; try { OutputStream os = new FileOutputStream(getFile(index)); writer = new OutputStreamWriter(os, Util.UTF_8); writer.write(value); } finally { Util.closeQuietly(writer); } }
/** Sets the value at {@code index} to {@code value}. */ public void set(int index, String value) throws IOException { Writer writer = null; try { OutputStream os = new FileOutputStream(getFile(index)); writer = new OutputStreamWriter(os, Util.UTF_8); writer.write(value); } finally { Util.closeQuietly(writer); } }
private boolean writeBitmapToFile(Bitmap bitmap, DiskLruCache.Editor editor) throws IOException { OutputStream out = null; try { out = new FileOutputStream(editor.getFile(BITMAP_INDEX)); return bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); } finally { if (out != null) { out.close(); } } }