Java Code Examples for com.android.volley.VolleyLog#v()

The following examples show how to use com.android.volley.VolleyLog#v() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpHeaderParser.java    From volley with Apache License 2.0 6 votes vote down vote up
/** Parse date in RFC1123 format, and return its value as epoch */
public static long parseDateAsEpoch(String dateStr) {
    try {
        // Parse date in RFC1123 format if this header contains one
        return newUsGmtFormatter(RFC1123_PARSE_FORMAT).parse(dateStr).getTime();
    } catch (ParseException e) {
        // Date in invalid format, fallback to 0
        // If the value is either "0" or "-1" we only log to verbose,
        // these values are pretty common and cause log spam.
        String message = "Unable to parse dateStr: %s, falling back to 0";
        if ("0".equals(dateStr) || "-1".equals(dateStr)) {
            VolleyLog.v(message, dateStr);
        } else {
            VolleyLog.e(e, message, dateStr);
        }

        return 0;
    }
}
 
Example 2
Source File: DiskBasedCache.java    From android-common-utils with Apache License 2.0 5 votes vote down vote up
/**
 * Prunes the cache to fit the amount of bytes specified.
 * @param neededSpace The amount of bytes we are trying to fit into the cache.
 */
private void pruneIfNeeded(int neededSpace) {
    if ((mTotalSize + neededSpace) < mMaxCacheSizeInBytes) {
        return;
    }
    if (VolleyLog.DEBUG) {
        VolleyLog.v("Pruning old cache entries.");
    }

    long before = mTotalSize;
    int prunedFiles = 0;
    long startTime = SystemClock.elapsedRealtime();

    Iterator<Map.Entry<String, CacheHeader>> iterator = mEntries.entrySet().iterator();
    while (iterator.hasNext()) {
        Map.Entry<String, CacheHeader> entry = iterator.next();
        CacheHeader e = entry.getValue();
        boolean deleted = getFileForKey(e.key).delete();
        if (deleted) {
            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 ((mTotalSize + neededSpace) < mMaxCacheSizeInBytes * HYSTERESIS_FACTOR) {
            break;
        }
    }

    if (VolleyLog.DEBUG) {
        VolleyLog.v("pruned %d files, %d bytes, %d ms",
                prunedFiles, (mTotalSize - before), SystemClock.elapsedRealtime() - startTime);
    }
}
 
Example 3
Source File: BasicNetwork.java    From TitanjumNote with Apache License 2.0 5 votes vote down vote up
/** Reads the contents of HttpEntity into a byte[]. */
private byte[] entityToBytes(HttpEntity entity) throws IOException,
                                                       ServerError {
    PoolingByteArrayOutputStream bytes =
            new PoolingByteArrayOutputStream(mPool, (int) entity.getContentLength());
    byte[] buffer = null;
    try {
        InputStream in = entity.getContent();
        if (in == null) {
            throw new ServerError();
        }
        buffer = mPool.getBuf(1024);
        int count;
        while ((count = in.read(buffer)) != -1) {
            bytes.write(buffer, 0, count);
        }
        return bytes.toByteArray();
    } finally {
        try {
            // Close the InputStream and release the resources by "consuming the content".
            entity.consumeContent();
        } catch (IOException e) {
            // This can happen if there was an exception above that left the entity in
            // an invalid state.
            VolleyLog.v("Error occured when calling consumingContent");
        }
        mPool.returnBuf(buffer);
        bytes.close();
    }
}
 
Example 4
Source File: BasicNetwork.java    From volley with Apache License 2.0 5 votes vote down vote up
/** Reads the contents of an InputStream into a byte[]. */
private byte[] inputStreamToBytes(InputStream in, int contentLength)
        throws IOException, ServerError {
    PoolingByteArrayOutputStream bytes = new PoolingByteArrayOutputStream(mPool, contentLength);
    byte[] buffer = null;
    try {
        if (in == null) {
            throw new ServerError();
        }
        buffer = mPool.getBuf(1024);
        int count;
        while ((count = in.read(buffer)) != -1) {
            bytes.write(buffer, 0, count);
        }
        return bytes.toByteArray();
    } finally {
        try {
            // Close the InputStream and release the resources by "consuming the content".
            if (in != null) {
                in.close();
            }
        } catch (IOException e) {
            // This can happen if there was an exception above that left the stream in
            // an invalid state.
            VolleyLog.v("Error occurred when closing InputStream");
        }
        mPool.returnBuf(buffer);
        bytes.close();
    }
}
 
Example 5
Source File: DiskBasedCache.java    From android-discourse with Apache License 2.0 5 votes vote down vote up
/**
 * Prunes the cache to fit the amount of bytes specified.
 *
 * @param neededSpace The amount of bytes we are trying to fit into the cache.
 */
private void pruneIfNeeded(int neededSpace) {
    if ((mTotalSize + neededSpace) < mMaxCacheSizeInBytes) {
        return;
    }
    if (VolleyLog.DEBUG) {
        VolleyLog.v("Pruning old cache entries.");
    }

    long before = mTotalSize;
    int prunedFiles = 0;
    long startTime = SystemClock.elapsedRealtime();

    Iterator<Map.Entry<String, CacheHeader>> iterator = mEntries.entrySet().iterator();
    while (iterator.hasNext()) {
        Map.Entry<String, CacheHeader> entry = iterator.next();
        CacheHeader e = entry.getValue();
        boolean deleted = getFileForKey(e.key).delete();
        if (deleted) {
            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 ((mTotalSize + neededSpace) < mMaxCacheSizeInBytes * HYSTERESIS_FACTOR) {
            break;
        }
    }

    if (VolleyLog.DEBUG) {
        VolleyLog.v("pruned %d files, %d bytes, %d ms", prunedFiles, (mTotalSize - before), SystemClock.elapsedRealtime() - startTime);
    }
}
 
Example 6
Source File: DiskBasedCache.java    From volley with Apache License 2.0 5 votes vote down vote up
/**
 * Prunes the cache to fit the amount of bytes specified.
 * @param neededSpace The amount of bytes we are trying to fit into the cache.
 */
private void pruneIfNeeded(int neededSpace) {
    if ((mTotalSize + neededSpace) < mMaxCacheSizeInBytes) {
        return;
    }
    if (VolleyLog.DEBUG) {
        VolleyLog.v("Pruning old cache entries.");
    }

    long before = mTotalSize;
    int prunedFiles = 0;
    long startTime = SystemClock.elapsedRealtime();

    Iterator<Map.Entry<String, CacheHeader>> iterator = mEntries.entrySet().iterator();
    while (iterator.hasNext()) {
        Map.Entry<String, CacheHeader> entry = iterator.next();
        CacheHeader e = entry.getValue();
        boolean deleted = getFileForKey(e.key).delete();
        if (deleted) {
            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 ((mTotalSize + neededSpace) < mMaxCacheSizeInBytes * HYSTERESIS_FACTOR) {
            break;
        }
    }

    if (VolleyLog.DEBUG) {
        VolleyLog.v("pruned %d files, %d bytes, %d ms",
                prunedFiles, (mTotalSize - before), SystemClock.elapsedRealtime() - startTime);
    }
}
 
Example 7
Source File: DiskBasedCache.java    From device-database with Apache License 2.0 5 votes vote down vote up
/**
 * Prunes the cache to fit the amount of bytes specified.
 * @param neededSpace The amount of bytes we are trying to fit into the cache.
 */
private void pruneIfNeeded(int neededSpace) {
    if ((mTotalSize + neededSpace) < mMaxCacheSizeInBytes) {
        return;
    }
    if (VolleyLog.DEBUG) {
        VolleyLog.v("Pruning old cache entries.");
    }

    long before = mTotalSize;
    int prunedFiles = 0;
    long startTime = SystemClock.elapsedRealtime();

    Iterator<Map.Entry<String, CacheHeader>> iterator = mEntries.entrySet().iterator();
    while (iterator.hasNext()) {
        Map.Entry<String, CacheHeader> entry = iterator.next();
        CacheHeader e = entry.getValue();
        boolean deleted = getFileForKey(e.key).delete();
        if (deleted) {
            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 ((mTotalSize + neededSpace) < mMaxCacheSizeInBytes * HYSTERESIS_FACTOR) {
            break;
        }
    }

    if (VolleyLog.DEBUG) {
        VolleyLog.v("pruned %d files, %d bytes, %d ms",
                prunedFiles, (mTotalSize - before), SystemClock.elapsedRealtime() - startTime);
    }
}
 
Example 8
Source File: DiskBasedCache.java    From CrossBow with Apache License 2.0 5 votes vote down vote up
/**
 * Prunes the cache to fit the amount of bytes specified.
 * @param neededSpace The amount of bytes we are trying to fit into the cache.
 */
private void pruneIfNeeded(int neededSpace) {
    if ((mTotalSize + neededSpace) < mMaxCacheSizeInBytes) {
        return;
    }
    if (VolleyLog.DEBUG) {
        VolleyLog.v("Pruning old cache entries.");
    }

    long before = mTotalSize;
    int prunedFiles = 0;
    long startTime = SystemClock.elapsedRealtime();

    Iterator<Map.Entry<String, CacheHeader>> iterator = mEntries.entrySet().iterator();
    while (iterator.hasNext()) {
        Map.Entry<String, CacheHeader> entry = iterator.next();
        CacheHeader e = entry.getValue();
        boolean deleted = getFileForKey(e.key).delete();
        if (deleted) {
            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 ((mTotalSize + neededSpace) < mMaxCacheSizeInBytes * HYSTERESIS_FACTOR) {
            break;
        }
    }

    if (VolleyLog.DEBUG) {
        VolleyLog.v("pruned %d files, %d bytes, %d ms",
                prunedFiles, (mTotalSize - before), SystemClock.elapsedRealtime() - startTime);
    }
}
 
Example 9
Source File: DiskBasedCache.java    From android_tv_metro with Apache License 2.0 5 votes vote down vote up
/**
 * Prunes the cache to fit the amount of bytes specified.
 * @param neededSpace The amount of bytes we are trying to fit into the cache.
 */
private void pruneIfNeeded(int neededSpace) {
    if ((mTotalSize + neededSpace) < mMaxCacheSizeInBytes) {
        return;
    }
    if (VolleyLog.DEBUG) {
        VolleyLog.v("Pruning old cache entries.");
    }

    long before = mTotalSize;
    int prunedFiles = 0;
    long startTime = SystemClock.elapsedRealtime();

    Iterator<Map.Entry<String, CacheHeader>> iterator = mEntries.entrySet().iterator();
    while (iterator.hasNext()) {
        Map.Entry<String, CacheHeader> entry = iterator.next();
        CacheHeader e = entry.getValue();
        boolean deleted = getFileForKey(e.key).delete();
        if (deleted) {
            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 ((mTotalSize + neededSpace) < mMaxCacheSizeInBytes * HYSTERESIS_FACTOR) {
            break;
        }
    }

    if (VolleyLog.DEBUG) {
        VolleyLog.v("pruned %d files, %d bytes, %d ms",
                prunedFiles, (mTotalSize - before), SystemClock.elapsedRealtime() - startTime);
    }
}
 
Example 10
Source File: BasicNetwork.java    From FeedListViewDemo with MIT License 5 votes vote down vote up
/** Reads the contents of HttpEntity into a byte[]. */
private byte[] entityToBytes(HttpEntity entity) throws IOException, ServerError {
    PoolingByteArrayOutputStream bytes =
            new PoolingByteArrayOutputStream(mPool, (int) entity.getContentLength());
    byte[] buffer = null;
    try {
        InputStream in = entity.getContent();
        if (in == null) {
            throw new ServerError();
        }
        buffer = mPool.getBuf(1024);
        int count;
        while ((count = in.read(buffer)) != -1) {
            bytes.write(buffer, 0, count);
        }
        return bytes.toByteArray();
    } finally {
        try {
            // Close the InputStream and release the resources by "consuming the content".
            entity.consumeContent();
        } catch (IOException e) {
            // This can happen if there was an exception above that left the entity in
            // an invalid state.
            VolleyLog.v("Error occured when calling consumingContent");
        }
        mPool.returnBuf(buffer);
        bytes.close();
    }
}
 
Example 11
Source File: BasicNetwork.java    From volley_demo with Apache License 2.0 5 votes vote down vote up
/** Reads the contents of HttpEntity into a byte[]. */
private byte[] entityToBytes(HttpEntity entity) throws IOException, ServerError {
    PoolingByteArrayOutputStream bytes =
            new PoolingByteArrayOutputStream(mPool, (int) entity.getContentLength());
    byte[] buffer = null;
    try {
        InputStream in = entity.getContent();
        if (in == null) {
            throw new ServerError();
        }
        buffer = mPool.getBuf(1024);
        int count;
        while ((count = in.read(buffer)) != -1) {
            bytes.write(buffer, 0, count);
        }
        return bytes.toByteArray();
    } finally {
        try {
            // Close the InputStream and release the resources by "consuming the content".
            entity.consumeContent();
        } catch (IOException e) {
            // This can happen if there was an exception above that left the entity in
            // an invalid state.
            VolleyLog.v("Error occured when calling consumingContent");
        }
        mPool.returnBuf(buffer);
        bytes.close();
    }
}
 
Example 12
Source File: DiskBasedCache.java    From WayHoo with Apache License 2.0 5 votes vote down vote up
/**
 * Prunes the cache to fit the amount of bytes specified.
 * @param neededSpace The amount of bytes we are trying to fit into the cache.
 */
private void pruneIfNeeded(int neededSpace) {
    if ((mTotalSize + neededSpace) < mMaxCacheSizeInBytes) {
        return;
    }
    if (VolleyLog.DEBUG) {
        VolleyLog.v("Pruning old cache entries.");
    }

    long before = mTotalSize;
    int prunedFiles = 0;
    long startTime = SystemClock.elapsedRealtime();

    Iterator<Map.Entry<String, CacheHeader>> iterator = mEntries.entrySet().iterator();
    while (iterator.hasNext()) {
        Map.Entry<String, CacheHeader> entry = iterator.next();
        CacheHeader e = entry.getValue();
        boolean deleted = getFileForKey(e.key).delete();
        if (deleted) {
            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 ((mTotalSize + neededSpace) < mMaxCacheSizeInBytes * HYSTERESIS_FACTOR) {
            break;
        }
    }

    if (VolleyLog.DEBUG) {
        VolleyLog.v("pruned %d files, %d bytes, %d ms",
                prunedFiles, (mTotalSize - before), SystemClock.elapsedRealtime() - startTime);
    }
}
 
Example 13
Source File: DiskBasedCache.java    From volley_demo with Apache License 2.0 5 votes vote down vote up
/**
 * Prunes the cache to fit the amount of bytes specified.
 * @param neededSpace The amount of bytes we are trying to fit into the cache.
 */
private void pruneIfNeeded(int neededSpace) {
    if ((mTotalSize + neededSpace) < mMaxCacheSizeInBytes) {
        return;
    }
    if (VolleyLog.DEBUG) {
        VolleyLog.v("Pruning old cache entries.");
    }

    long before = mTotalSize;
    int prunedFiles = 0;
    long startTime = SystemClock.elapsedRealtime();

    Iterator<Map.Entry<String, CacheHeader>> iterator = mEntries.entrySet().iterator();
    while (iterator.hasNext()) {
        Map.Entry<String, CacheHeader> entry = iterator.next();
        CacheHeader e = entry.getValue();
        boolean deleted = getFileForKey(e.key).delete();
        if (deleted) {
            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 ((mTotalSize + neededSpace) < mMaxCacheSizeInBytes * HYSTERESIS_FACTOR) {
            break;
        }
    }

    if (VolleyLog.DEBUG) {
        VolleyLog.v("pruned %d files, %d bytes, %d ms",
                prunedFiles, (mTotalSize - before), SystemClock.elapsedRealtime() - startTime);
    }
}
 
Example 14
Source File: HttpHeaderParser.java    From barterli_android with Apache License 2.0 4 votes vote down vote up
/**
 * Extracts a {@link Cache.Entry} from a {@link NetworkResponse}.
 *
 * @param response The network response to parse headers from
 * @return a cache entry for the given response, or null if the response is not cacheable.
 */
public static Cache.Entry parseCacheHeaders(NetworkResponse response) {
    long now = System.currentTimeMillis();

    Map<String, String> headers = response.headers;
    
    VolleyLog.v("Response headers: %s", headers);

    long serverDate = 0;
    long serverExpires = 0;
    long softExpire = 0;
    long maxAge = 0;
    boolean hasCacheControl = false;

    String serverEtag = null;
    String headerValue;

    headerValue = headers.get("Date");
    if (headerValue != null) {
        serverDate = parseDateAsEpoch(headerValue);
    }

    headerValue = headers.get("Cache-Control");
    if (headerValue != null) {
        hasCacheControl = true;
        String[] tokens = headerValue.split(",");
        for (int i = 0; i < tokens.length; i++) {
            String token = tokens[i].trim();
            if (token.equals("no-cache") || token.equals("no-store")) {
                return null;
            } else if (token.startsWith("max-age=")) {
                try {
                    maxAge = Long.parseLong(token.substring(8));
                } catch (Exception e) {
                }
            } else if (token.equals("must-revalidate") || token.equals("proxy-revalidate")) {
                maxAge = 0;
            }
        }
    }

    headerValue = headers.get("Expires");
    if (headerValue != null) {
        serverExpires = parseDateAsEpoch(headerValue);
    }

    serverEtag = headers.get("ETag");

    // Cache-Control takes precedence over an Expires header, even if both exist and Expires
    // is more restrictive.
    if (hasCacheControl) {
        softExpire = now + maxAge * 1000;
    } else if (serverDate > 0 && serverExpires >= serverDate) {
        // Default semantic for Expire header in HTTP specification is softExpire.
        softExpire = now + (serverExpires - serverDate);
    }

    Cache.Entry entry = new Cache.Entry();
    entry.data = response.data;
    entry.etag = serverEtag;
    entry.softTtl = softExpire;
    entry.ttl = entry.softTtl;
    entry.serverDate = serverDate;
    entry.responseHeaders = headers;

    return entry;
}
 
Example 15
Source File: OkNetwork.java    From OkVolley with Apache License 2.0 4 votes vote down vote up
protected void logError(String what, String url, long start) {
    long now = SystemClock.elapsedRealtime();
    VolleyLog.v("HTTP ERROR(%s) %d ms to fetch %s", what, (now - start), url);
}
 
Example 16
Source File: BasicNetwork.java    From product-emm with Apache License 2.0 4 votes vote down vote up
protected void logError(String what, String url, long start) {
    long now = SystemClock.elapsedRealtime();
    VolleyLog.v("HTTP ERROR(%s) %d ms to fetch %s", what, (now - start), url);
}
 
Example 17
Source File: BasicNetwork.java    From WayHoo with Apache License 2.0 4 votes vote down vote up
protected void logError(String what, String url, long start) {
    long now = SystemClock.elapsedRealtime();
    VolleyLog.v("HTTP ERROR(%s) %d ms to fetch %s", what, (now - start), url);
}
 
Example 18
Source File: BallCacheDispatcher.java    From Volley-Ball with MIT License 4 votes vote down vote up
@Override
public void run() {
    if (DEBUG) VolleyLog.v("start new dispatcher");
    Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);

    // Make a blocking call to initialize the cache.
    mCache.initialize();

    while (true) {
        try {
            // Get a request from the cache triage queue, blocking until
            // at least one is available.
            final BallRequest request = mCacheQueue.take();
            request.addMarker("cache-queue-take");

            // If the request has been canceled, don't bother dispatching it.
            if (request.isCanceled()) {
                request.finish("cache-discard-canceled");
                continue;
            }

            // Attempt to retrieve this item from cache.
            Cache.Entry entry = mCache.get(request.getCacheKey());
            if (entry == null) {
                request.addMarker("cache-miss");
                // Cache miss; send off to the network dispatcher.
                //mDelivery.postEmptyIntermediateResponse(request, BallResponse.ResponseSource.CACHE);
                mNetworkQueue.put(request);
                continue;
            }

            // If it is completely expired, just send it to the network.
            if (entry.isExpired()) {
                request.addMarker("cache-hit-expired");
                request.setCacheEntry(entry);
                //mDelivery.postEmptyIntermediateResponse(request, BallResponse.ResponseSource.CACHE);
                mNetworkQueue.put(request);
                continue;
            }

            // We have a cache hit; parse its data for delivery back to the request.
            request.addMarker("cache-hit");
            BallResponse<?> response = request.getNetworkRequestProcessor().parseNetworkResponse(new NetworkResponse(entry.data, entry.responseHeaders));
            response.setResponseSource(BallResponse.ResponseSource.CACHE);
            request.addMarker("cache-hit-parsed");

            if (!entry.refreshNeeded()) {
                // Completely unexpired cache hit. Just deliver the response.
                mDelivery.postResponse(request, response);
            } else {
                // Soft-expired cache hit. We can deliver the cached response,
                // but we need to also send the request to the network for
                // refreshing.
                request.addMarker("cache-hit-refresh-needed");
                request.setCacheEntry(entry);

                // Mark the response as intermediate.
                response.setIntermediate(true);

                // Post the intermediate response back to the user and have
                // the delivery then forward the request along to the network.
                mDelivery.postResponseAndForwardToNetwork(request, response);
            }

        } catch (InterruptedException e) {
            // We may have been interrupted because it was time to quit.
            if (mQuit) {
                return;
            }
            continue;
        }
    }
}
 
Example 19
Source File: BasicNetwork.java    From FeedListViewDemo with MIT License 4 votes vote down vote up
protected void logError(String what, String url, long start) {
    long now = SystemClock.elapsedRealtime();
    VolleyLog.v("HTTP ERROR(%s) %d ms to fetch %s", what, (now - start), url);
}
 
Example 20
Source File: BasicNetwork.java    From product-emm with Apache License 2.0 4 votes vote down vote up
protected void logError(String what, String url, long start) {
    long now = SystemClock.elapsedRealtime();
    VolleyLog.v("HTTP ERROR(%s) %d ms to fetch %s", what, (now - start), url);
}