public synchronized void put(String key, Entry entry) { pruneIfNeeded(entry.data.length); File file = getFileForKey(key); try { BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(file)); CacheHeader e = new CacheHeader(key, entry); if (e.writeHeader(fos)) { fos.write(entry.data); fos.close(); putEntry(key, e); } else { fos.close(); VolleyLog.d("Failed to write header for %s", file.getAbsolutePath()); throw new IOException(); } } catch (IOException e2) { if (!file.delete()) { VolleyLog.d("Could not clean up file %s", file.getAbsolutePath()); } } }
@Override public Response<T> parseNetworkResponse(NetworkResponse response) { String parsed; try { String charsetName = HttpHeaderParser.parseCharset(response.headers); parsed = new String(response.data, charsetName); } catch (UnsupportedEncodingException e) { parsed = new String(response.data); e.printStackTrace(); } QyerResponse<T> resp = onResponse(parsed); if (resp.isSuccess()) { Entry entry = HttpHeaderParser.parseCacheHeaders(response); mObjResp = Response.success(resp.getData(), entry); return mObjResp; } else { return Response.error(new VolleyError(resp.getMsg())); } }
private void addCacheHeaders(Map<String, String> headers, Cache.Entry entry) { // If there's no cache entry, we're done. if (entry == null) { return; } if (entry.etag != null) { // 设置 If-None-Match headers.put("If-None-Match", entry.etag); } if (entry.lastModified > 0) { Date refTime = new Date(entry.lastModified); // 设置 If-Modified-Since headers.put("If-Modified-Since", DateUtils.formatDate(refTime)); } }
public final void invalidate(String paramString, boolean paramBoolean) { try { Cache.Entry localEntry = get(paramString); if (localEntry != null) { localEntry.softTtl = 0L; if (paramBoolean) { localEntry.ttl = 0L; } put(paramString, localEntry); } return; } finally {} }
private void stripForCache(Response.ResponseWrapper paramResponseWrapper, Cache.Entry paramEntry) { if ((paramResponseWrapper.preFetch.length <= 0) && (paramResponseWrapper.commands == null) && (paramResponseWrapper.notification.length <= 0) && (paramResponseWrapper.serverCookies == null)) { return; } Cache localCache = this.mApiContext.mCache; long l = System.currentTimeMillis(); for (PreFetch localPreFetch : paramResponseWrapper.preFetch) { Cache.Entry localEntry = new Cache.Entry(); localEntry.data = localPreFetch.response; localEntry.etag = localPreFetch.etag; localEntry.serverDate = paramEntry.serverDate; localEntry.ttl = (l + localPreFetch.ttl); localEntry.softTtl = (l + localPreFetch.softTtl); localCache.put(makeCacheKey(Uri.withAppendedPath(DfeApi.BASE_URI, localPreFetch.url).toString()), localEntry); } paramResponseWrapper.preFetch = PreFetch.emptyArray(); paramResponseWrapper.commands = null; paramResponseWrapper.notification = Notification.emptyArray(); paramResponseWrapper.serverCookies = null; paramEntry.data = MessageNano.toByteArray(paramResponseWrapper); }
public final String get(String paramString) { Cache.Entry localEntry = this.mCache.get("AddressMetadataCacheManager-" + paramString); if ((localEntry == null) || (localEntry.isExpired())) { return ""; } try { String str = new String(localEntry.data, "UTF-8"); return str; } catch (UnsupportedEncodingException localUnsupportedEncodingException) { throw new RuntimeException("UTF-8 not supported."); } }
public final void put(String paramString1, String paramString2) { Cache.Entry localEntry = new Cache.Entry(); try { localEntry.data = paramString2.getBytes("UTF-8"); localEntry.serverDate = System.currentTimeMillis(); localEntry.ttl = (604800000L + localEntry.serverDate); this.mCache.put("AddressMetadataCacheManager-" + paramString1, localEntry); return; } catch (UnsupportedEncodingException localUnsupportedEncodingException) { throw new RuntimeException("UTF-8 not supported."); } }
public static Document getDocument(PurchaseParams paramPurchaseParams, Cache paramCache) { if (paramPurchaseParams.document != null) { return paramPurchaseParams.document; } Cache.Entry localEntry = paramCache.get("InstantPurchaseDocument-" + paramPurchaseParams.docidStr); if ((localEntry == null) || (localEntry.isExpired())) { return null; } try { byte[] arrayOfByte = localEntry.data; Document localDocument = new Document((DocV2)MessageNano.mergeFrom$1ec43da(new DocV2(), arrayOfByte, arrayOfByte.length)); return localDocument; } catch (InvalidProtocolBufferNanoException localInvalidProtocolBufferNanoException) { throw new RuntimeException(localInvalidProtocolBufferNanoException); } }
private void stripForCache(PlayResponse.PlayResponseWrapper paramPlayResponseWrapper, Cache.Entry paramEntry) { if ((paramPlayResponseWrapper.preFetch.length <= 0) && (paramPlayResponseWrapper.commands == null)) { return; } Cache localCache = this.mApiContext.mCache; long l = System.currentTimeMillis(); for (PreFetch localPreFetch : paramPlayResponseWrapper.preFetch) { Cache.Entry localEntry = new Cache.Entry(); localEntry.data = localPreFetch.response; localEntry.etag = localPreFetch.etag; localEntry.serverDate = paramEntry.serverDate; localEntry.ttl = (l + localPreFetch.ttl); localEntry.softTtl = (l + localPreFetch.softTtl); localCache.put(makeCacheKey(Uri.withAppendedPath(PlayDfeApi.BASE_URI, localPreFetch.url).toString()), localEntry); } paramPlayResponseWrapper.preFetch = PreFetch.emptyArray(); paramPlayResponseWrapper.commands = null; paramEntry.data = MessageNano.toByteArray(paramPlayResponseWrapper); }
protected final Response<Pair<RequestT, ResponseT>> parseNetworkResponse(NetworkResponse paramNetworkResponse) { try { Class localClass = this.mResponseClass; MessageNano localMessageNano1 = (MessageNano)this.mResponseClass.newInstance(); byte[] arrayOfByte = paramNetworkResponse.data; MessageNano localMessageNano2 = (MessageNano)localClass.cast(MessageNano.mergeFrom$1ec43da(localMessageNano1, arrayOfByte, arrayOfByte.length)); ProtoUtils.logResponse(localMessageNano2, getUrl()); Cache.Entry localEntry = HttpHeaderParser.parseCacheHeaders(paramNetworkResponse); return Response.success(Pair.create(this, localMessageNano2), localEntry); } catch (InstantiationException localInstantiationException) { throw new RuntimeException("Failed to instantiate proto object.", localInstantiationException); } catch (IllegalAccessException localIllegalAccessException) { throw new RuntimeException("Failed to access proto constructor.", localIllegalAccessException); } catch (IOException localIOException) { Log.e("SecureRequest", "Couldn't parse proto response for url=" + getUrl()); } return Response.error(new ParseError(paramNetworkResponse)); }
@Override public byte[] getBody() throws AuthFailureError { try { if (params != null) { List<NameValuePair> pairs = new ArrayList<NameValuePair>(); for (Map.Entry<String, String> e : params.entrySet()) { String key = e.getKey(); String value = e.getValue(); if (value != null) { pairs.add(new BasicNameValuePair(key, value)); } } ByteArrayOutputStream bos = new ByteArrayOutputStream(); HttpEntity entity = new UrlEncodedFormEntity(pairs, PROTOCOL_CHARSET); entity.writeTo(bos); return bos.toByteArray(); } } catch (Exception ex) { ex.printStackTrace(); } return null; }
private void addCacheHeaders(Map<String, String> headers, Cache.Entry entry) { // If there's no cache entry, we're done. if (entry == null) { return; } if (entry.etag != null) { headers.put("If-None-Match", entry.etag); } if (entry.lastModified > 0) { Date refTime = new Date(entry.lastModified); headers.put("If-Modified-Since", DateUtils.formatDate(refTime)); } }
public CacheHeader(String key, Entry entry) { this.key = key; this.size = (long) entry.data.length; this.etag = entry.etag; this.serverDate = entry.serverDate; this.lastModified = entry.lastModified; this.ttl = entry.ttl; this.softTtl = entry.softTtl; this.responseHeaders = entry.responseHeaders; }
public Entry toCacheEntry(byte[] data) { Entry e = new Entry(); e.data = data; e.etag = this.etag; e.serverDate = this.serverDate; e.lastModified = this.lastModified; e.ttl = this.ttl; e.softTtl = this.softTtl; e.responseHeaders = this.responseHeaders; return e; }
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); } }
private void pruneIfNeeded(int neededSpace) { if (this.mTotalSize + ((long) neededSpace) >= ((long) this.mMaxCacheSizeInBytes)) { if (VolleyLog.DEBUG) { VolleyLog.v("Pruning old cache entries.", new Object[0]); } long before = this.mTotalSize; int prunedFiles = 0; long startTime = SystemClock.elapsedRealtime(); Iterator<Map.Entry<String, CacheHeader>> iterator = this.mEntries.entrySet().iterator(); while (iterator.hasNext()) { CacheHeader e = (CacheHeader) ((Map.Entry) iterator.next()).getValue(); if (getFileForKey(e.key).delete()) { this.mTotalSize -= e.size; } else { VolleyLog.d("Could not delete cache entry for key=%s, filename=%s", e.key, getFilenameForKey(e.key)); } iterator.remove(); prunedFiles++; if (((float) (this.mTotalSize + ((long) neededSpace))) < ((float) this.mMaxCacheSizeInBytes) * HYSTERESIS_FACTOR) { break; } } if (VolleyLog.DEBUG) { VolleyLog.v("pruned %d files, %d bytes, %d ms", Integer.valueOf(prunedFiles), Long.valueOf(this.mTotalSize - before), Long.valueOf(SystemClock.elapsedRealtime() - startTime)); } } }
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, (String) entry.getKey()); writeString(os, (String) entry.getValue()); } return; } writeInt(os, 0); }
private void addCacheHeaders(Map<String, String> headers, Entry entry) { if (entry != null) { if (entry.etag != null) { headers.put("If-None-Match", entry.etag); } if (entry.lastModified > 0) { headers.put("If-Modified-Since", DateUtils.formatDate(new Date(entry.lastModified))); } } }
private byte[] encodeParameters(Map<String, String> params, String paramsEncoding) { StringBuilder encodedParams = new StringBuilder(); try { for (Map.Entry<String, String> entry : params.entrySet()) { encodedParams.append(URLEncoder.encode((String) entry.getKey(), paramsEncoding)); encodedParams.append('='); encodedParams.append(URLEncoder.encode((String) entry.getValue(), paramsEncoding)); encodedParams.append('&'); } return encodedParams.toString().getBytes(paramsEncoding); } catch (UnsupportedEncodingException uee) { throw new RuntimeException("Encoding not supported: " + paramsEncoding, uee); } }
private void addCacheHeaders(Map<String, String> headers, Cache.Entry entry) { // If there's no cache entry, we're done. if (entry == null) { return; } if (entry.etag != null) { headers.put("If-None-Match", entry.etag); } if (entry.lastModified > 0) { Date refTime = new Date(entry.lastModified); headers.put("If-Modified-Since", InternalUtils.formatDate(refTime)); } }
private void addCacheHeaders(Map<String, String> headers, Cache.Entry entry) { // If there's no cache entry, we're done. if (entry == null) { return; } if (entry.etag != null) { headers.put("If-None-Match", entry.etag); } if (entry.serverDate > 0) { Date refTime = new Date(entry.serverDate); headers.put("If-Modified-Since", DateUtils.formatDate(refTime)); } }
public CacheHeader(String paramString, Cache.Entry paramEntry) { this.key = paramString; this.size = paramEntry.data.length; this.etag = paramEntry.etag; this.serverDate = paramEntry.serverDate; this.lastModified = paramEntry.lastModified; this.ttl = paramEntry.ttl; this.softTtl = paramEntry.softTtl; this.responseHeaders = paramEntry.responseHeaders; }
private static Cache.Entry parseCacheHeaders(NetworkResponse paramNetworkResponse) { localEntry = HttpHeaderParser.parseCacheHeaders(paramNetworkResponse); if (localEntry == null) { return null; } long l = System.currentTimeMillis(); try { String str1 = (String)paramNetworkResponse.headers.get("X-DFE-Soft-TTL"); if (str1 != null) { localEntry.softTtl = (l + Long.parseLong(str1)); } String str2 = (String)paramNetworkResponse.headers.get("X-DFE-Hard-TTL"); if (str2 != null) { localEntry.ttl = (l + Long.parseLong(str2)); } } catch (NumberFormatException localNumberFormatException) { for (;;) { Object[] arrayOfObject = new Object[1]; arrayOfObject[0] = paramNetworkResponse.headers; FinskyLog.d("Invalid TTL: %s", arrayOfObject); localEntry.softTtl = 0L; localEntry.ttl = 0L; } } localEntry.ttl = Math.max(localEntry.ttl, localEntry.softTtl); return localEntry; }
private static Cache.Entry parseCacheHeaders(NetworkResponse paramNetworkResponse) { localEntry = HttpHeaderParser.parseCacheHeaders(paramNetworkResponse); if (localEntry == null) { return null; } long l = System.currentTimeMillis(); try { String str1 = (String)paramNetworkResponse.headers.get("X-DFE-Soft-TTL"); if (str1 != null) { localEntry.softTtl = (l + Long.parseLong(str1)); } String str2 = (String)paramNetworkResponse.headers.get("X-DFE-Hard-TTL"); if (str2 != null) { localEntry.ttl = (l + Long.parseLong(str2)); } } catch (NumberFormatException localNumberFormatException) { for (;;) { Object[] arrayOfObject = new Object[1]; arrayOfObject[0] = paramNetworkResponse.headers; PlayCommonLog.d("Invalid TTL: %s", arrayOfObject); localEntry.softTtl = 0L; localEntry.ttl = 0L; } } localEntry.ttl = Math.max(localEntry.ttl, localEntry.softTtl); return localEntry; }