DiskBasedCache implements Cache { @Override public synchronized void put(String key, Entry entry) { pruneIfNeeded(entry.data.length); File file = getFileForKey(key); try { FileOutputStream fos = new FileOutputStream(file); CacheHeader e = new CacheHeader(key, entry); boolean success = e.writeHeader(fos); if (!success) { fos.close(); VolleyLog.d("Failed to write header for %s", file.getAbsolutePath()); throw new IOException(); } fos.write(entry.data); fos.close(); putEntry(key, e); return; } catch (IOException e) { } boolean deleted = file.delete(); if (!deleted) { VolleyLog.d("Could not clean up file %s", file.getAbsolutePath()); } } DiskBasedCache(File rootDirectory, int maxCacheSizeInBytes);  DiskBasedCache(File rootDirectory); @Override synchronized void clear(); @Override synchronized Entry get(String key); @Override synchronized void initialize(); @Override synchronized void invalidate(String key, boolean fullExpire); @Override synchronized void put(String key, Entry entry); @Override synchronized void remove(String key); File getFileForKey(String key);  }
@Test public void cacheHeaderSerialization() throws Exception { Cache.Entry e = new Cache.Entry(); e.data = new byte[8]; e.serverDate = 1234567L; e.lastModified = 13572468L; e.ttl = 9876543L; e.softTtl = 8765432L; e.etag = "etag"; e.responseHeaders = new HashMap<String, String>(); e.responseHeaders.put("fruit", "banana"); CacheHeader first = new CacheHeader("my-magical-key", e); ByteArrayOutputStream baos = new ByteArrayOutputStream(); first.writeHeader(baos); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); CacheHeader second = CacheHeader.readHeader(bais); assertEquals(first.key, second.key); assertEquals(first.serverDate, second.serverDate); assertEquals(first.lastModified, second.lastModified); assertEquals(first.ttl, second.ttl); assertEquals(first.softTtl, second.softTtl); assertEquals(first.etag, second.etag); assertEquals(first.responseHeaders, second.responseHeaders); }