public static RequestQueue newRequestQueueForTest(final Context context, final OkHttpClient okHttpClient) { final File cacheDir = new File(context.getCacheDir(), "volley"); final Network network = new BasicNetwork(new OkHttpStack(okHttpClient)); final ResponseDelivery responseDelivery = new ExecutorDelivery(Executors.newSingleThreadExecutor()); final RequestQueue queue = new RequestQueue( new DiskBasedCache(cacheDir), network, 4, responseDelivery); queue.start(); return queue; }
/** * Reads the header off of an InputStream and returns a CacheHeader object. * @param is The InputStream to read from. * @throws java.io.IOException */ public static CacheHeader readHeader(InputStream is) throws IOException { CacheHeader entry = new CacheHeader(); int magic = readInt(is); if (magic != CACHE_MAGIC) { // don't bother deleting, it'll get pruned eventually throw new IOException(); } entry.key = readString(is); entry.etag = readString(is); if (entry.etag.equals("")) { entry.etag = null; } entry.serverDate = readLong(is); entry.lastModified = readLong(is); entry.ttl = readLong(is); entry.softTtl = readLong(is); entry.responseHeaders = readStringStringMap(is); return entry; }
/** * Writes the contents of this CacheHeader to the specified OutputStream. */ public boolean writeHeader(OutputStream os) { try { writeInt(os, CACHE_MAGIC); writeString(os, key); writeString(os, etag == null ? "" : etag); writeLong(os, serverDate); writeLong(os, lastModified); writeLong(os, ttl); writeLong(os, softTtl); writeStringStringMap(responseHeaders, os); os.flush(); return true; } catch (IOException e) { VolleyLog.d("%s", e.toString()); return false; } }
/** * Invalidates an entry in the cache. * @param key Cache key * @param fullExpire True to fully expire the entry, false to soft expire */ @Override public synchronized void invalidate(String key, boolean fullExpire) { Entry entry = get(key); if (entry != null) { entry.softTtl = 0; if (fullExpire) { entry.ttl = 0; } put(key, entry); } }
static String readString(InputStream is) throws IOException { int n = (int) readLong(is); byte[] b = streamToBytes(is, n); return new String(b, "UTF-8"); }
static void writeStringStringMap(Map<String, String> map, OutputStream os) throws IOException { if (map != null) { writeInt(os, map.size()); for (Map.Entry<String, String> entry : map.entrySet()) { writeString(os, entry.getKey()); writeString(os, entry.getValue()); } } else { writeInt(os, 0); } }
static Map<String, String> readStringStringMap(InputStream is) throws IOException { int size = readInt(is); Map<String, String> result = (size == 0) ? Collections.<String, String>emptyMap() : new HashMap<String, String>(size); for (int i = 0; i < size; i++) { String key = readString(is).intern(); String value = readString(is).intern(); result.put(key, value); } return result; }
@Test public void serializeMap() throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); Map<String, String> empty = new HashMap<String, String>(); DiskBasedCache.writeStringStringMap(empty, baos); DiskBasedCache.writeStringStringMap(null, baos); Map<String, String> twoThings = new HashMap<String, String>(); twoThings.put("first", "thing"); twoThings.put("second", "item"); DiskBasedCache.writeStringStringMap(twoThings, baos); Map<String, String> emptyKey = new HashMap<String, String>(); emptyKey.put("", "value"); DiskBasedCache.writeStringStringMap(emptyKey, baos); Map<String, String> emptyValue = new HashMap<String, String>(); emptyValue.put("key", ""); DiskBasedCache.writeStringStringMap(emptyValue, baos); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); assertEquals(DiskBasedCache.readStringStringMap(bais), empty); assertEquals(DiskBasedCache.readStringStringMap(bais), empty); // null reads back empty assertEquals(DiskBasedCache.readStringStringMap(bais), twoThings); assertEquals(DiskBasedCache.readStringStringMap(bais), emptyKey); assertEquals(DiskBasedCache.readStringStringMap(bais), emptyValue); }
@Test public void serializeString() throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); DiskBasedCache.writeString(baos, ""); DiskBasedCache.writeString(baos, "This is a string."); DiskBasedCache.writeString(baos, "ファイカス"); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); assertEquals(DiskBasedCache.readString(bais), ""); assertEquals(DiskBasedCache.readString(bais), "This is a string."); assertEquals(DiskBasedCache.readString(bais), "ファイカス"); }
static void writeString(OutputStream os, String s) throws IOException { byte[] b = s.getBytes("UTF-8"); writeLong(os, b.length); os.write(b, 0, b.length); }
@Test public void serializeInt() throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); DiskBasedCache.writeInt(baos, 0); DiskBasedCache.writeInt(baos, 19791214); DiskBasedCache.writeInt(baos, -20050711); DiskBasedCache.writeInt(baos, Integer.MIN_VALUE); DiskBasedCache.writeInt(baos, Integer.MAX_VALUE); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); assertEquals(DiskBasedCache.readInt(bais), 0); assertEquals(DiskBasedCache.readInt(bais), 19791214); assertEquals(DiskBasedCache.readInt(bais), -20050711); assertEquals(DiskBasedCache.readInt(bais), Integer.MIN_VALUE); assertEquals(DiskBasedCache.readInt(bais), Integer.MAX_VALUE); }
@Test public void serializeLong() throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); DiskBasedCache.writeLong(baos, 0); DiskBasedCache.writeLong(baos, 31337); DiskBasedCache.writeLong(baos, -4160); DiskBasedCache.writeLong(baos, 4295032832L); DiskBasedCache.writeLong(baos, -4314824046L); DiskBasedCache.writeLong(baos, Long.MIN_VALUE); DiskBasedCache.writeLong(baos, Long.MAX_VALUE); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); assertEquals(DiskBasedCache.readLong(bais), 0); assertEquals(DiskBasedCache.readLong(bais), 31337); assertEquals(DiskBasedCache.readLong(bais), -4160); assertEquals(DiskBasedCache.readLong(bais), 4295032832L); assertEquals(DiskBasedCache.readLong(bais), -4314824046L); assertEquals(DiskBasedCache.readLong(bais), Long.MIN_VALUE); assertEquals(DiskBasedCache.readLong(bais), Long.MAX_VALUE); }
static Map<String, String> readStringStringMap(InputStream is) throws IOException { int size = readInt(is); Map<String, String> result = (size == 0) ? Collections.<String, String>emptyMap() : new HashMap<String, String>(size); for (int i = 0; i < size; i++) { String key = readString(is).intern(); String value = readString(is).intern(); result.put(key, value); } return result; }
static void writeStringStringMap(Map<String, String> map, OutputStream os) throws IOException { if (map != null) { writeInt(os, map.size()); for (Map.Entry<String, String> entry : map.entrySet()) { writeString(os, entry.getKey()); writeString(os, entry.getValue()); } } else { writeInt(os, 0); } }
/** * Invalidates an entry in the cache. * @param key Cache key * @param fullExpire True to fully expire the entry, false to soft expire */ @Override public synchronized void invalidate(String key, boolean fullExpire) { Entry entry = get(key); if (entry != null) { entry.softTtl = 0; if (fullExpire) { entry.ttl = 0; } put(key, entry); } }
static String readString(InputStream is) throws IOException { int n = (int) readLong(is); byte[] b = streamToBytes(is, n); return new String(b, "UTF-8"); }
@Test public void serializeMap() throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); Map<String, String> empty = new HashMap<String, String>(); DiskBasedCache.writeStringStringMap(empty, baos); DiskBasedCache.writeStringStringMap(null, baos); Map<String, String> twoThings = new HashMap<String, String>(); twoThings.put("first", "thing"); twoThings.put("second", "item"); DiskBasedCache.writeStringStringMap(twoThings, baos); Map<String, String> emptyKey = new HashMap<String, String>(); emptyKey.put("", "value"); DiskBasedCache.writeStringStringMap(emptyKey, baos); Map<String, String> emptyValue = new HashMap<String, String>(); emptyValue.put("key", ""); DiskBasedCache.writeStringStringMap(emptyValue, baos); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); assertEquals(DiskBasedCache.readStringStringMap(bais), empty); assertEquals(DiskBasedCache.readStringStringMap(bais), empty); // null reads back empty assertEquals(DiskBasedCache.readStringStringMap(bais), twoThings); assertEquals(DiskBasedCache.readStringStringMap(bais), emptyKey); assertEquals(DiskBasedCache.readStringStringMap(bais), emptyValue); }
@Test public void serializeString() throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); DiskBasedCache.writeString(baos, ""); DiskBasedCache.writeString(baos, "This is a string."); DiskBasedCache.writeString(baos, "ファイカス"); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); assertEquals(DiskBasedCache.readString(bais), ""); assertEquals(DiskBasedCache.readString(bais), "This is a string."); assertEquals(DiskBasedCache.readString(bais), "ファイカス"); }
static void writeString(OutputStream os, String s) throws IOException { byte[] b = s.getBytes("UTF-8"); writeLong(os, b.length); os.write(b, 0, b.length); }
@Test public void serializeInt() throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); DiskBasedCache.writeInt(baos, 0); DiskBasedCache.writeInt(baos, 19791214); DiskBasedCache.writeInt(baos, -20050711); DiskBasedCache.writeInt(baos, Integer.MIN_VALUE); DiskBasedCache.writeInt(baos, Integer.MAX_VALUE); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); assertEquals(DiskBasedCache.readInt(bais), 0); assertEquals(DiskBasedCache.readInt(bais), 19791214); assertEquals(DiskBasedCache.readInt(bais), -20050711); assertEquals(DiskBasedCache.readInt(bais), Integer.MIN_VALUE); assertEquals(DiskBasedCache.readInt(bais), Integer.MAX_VALUE); }