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(); } } }
/** * 保存 InputStream数据流 到 缓存中 * * @param key 保存的key * @param value 保存的数据 */ public int put(String key, InputStream value) { int count = -1; OutputStream out = null; DiskLruCache.Editor editor = null; try { editor = editor(key); if (editor == null) { return count; } out = editor.newOutputStream(0); count = copy(value, out); 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(); } } } return count; }
// ======================================= // ============== 序列化 数据 读写 ============= // ======================================= 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(); } } } }