Example usage for java.lang.ref SoftReference get

List of usage examples for java.lang.ref SoftReference get

Introduction

In this page you can find the example usage for java.lang.ref SoftReference get.

Prototype

public T get() 

Source Link

Document

Returns this reference object's referent.

Usage

From source file:com.example.hifadhi.utils.CacheImageDownloader.java

/**
 * @param url/*from www  .  j  ava 2  s .  c om*/
 *            The URL of the image that will be retrieved from the cache.
 * @return The cached bitmap or null if it was not found.
 */
private Bitmap getBitmapFromCache(final String url) {
    // First try the hard reference cache
    synchronized (sHardBitmapCache) {
        final Bitmap bitmap = sHardBitmapCache.get(url);
        if (bitmap != null) {
            // Bitmap found in hard cache
            // Move element to first position, so that it is removed last
            sHardBitmapCache.remove(url);
            sHardBitmapCache.put(url, bitmap);
            return bitmap;
        }
    }

    // Then try the soft reference cache
    final SoftReference<Bitmap> bitmapReference = CacheImageDownloader.sSoftBitmapCache.get(url);
    if (bitmapReference != null) {
        final Bitmap bitmap = bitmapReference.get();
        if (bitmap != null) {
            // Bitmap found in soft cache
            return bitmap;
        } else {
            // Soft reference has been Garbage Collected
            CacheImageDownloader.sSoftBitmapCache.remove(url);
        }
    }

    return null;
}

From source file:com.ndn.menurandom.ImageDownloader.java

/**
 * @param url The URL of the image that will be retrieved from the cache.
 * @return The cached bitmap or null if it was not found.
 *///from   ww  w  .j av a 2 s.  c  om
private Bitmap getBitmapFromCache(String url) {

    Bitmap returnVal = null;
    if (mMemoryCache.containsKey(url)) {
        SoftReference<Bitmap> ref = mMemoryCache.get(url);
        returnVal = ref.get();
    }

    if (returnVal == null) {
        File file = getFile(url);
        returnVal = decodeFile(file);
    }
    return returnVal;
}

From source file:com.scoreloop.client.android.ui.util.ImageDownloader.java

/**
 * @param url The URL of the image that will be retrieved from the cache.
 * @return The cached bitmap or null if it was not found.
 *///from   ww  w  .jav a 2  s .c  om
private Bitmap getBitmapFromCache(String url) {

    // First try the hard reference cache
    synchronized (sHardBitmapCache) {
        final Bitmap bitmap = sHardBitmapCache.get(url);
        if (bitmap != null) {
            // Bitmap found in hard cache
            // Move element to first position, so that it is removed last
            sHardBitmapCache.remove(url);
            sHardBitmapCache.put(url, bitmap);
            return bitmap;
        }
    }

    // Then try the soft reference cache
    SoftReference<Bitmap> bitmapReference = sSoftBitmapCache.get(url);
    if (bitmapReference != null) {
        final Bitmap bitmap = bitmapReference.get();
        if (bitmap != null) {
            // Bitmap found in soft cache
            return bitmap;
        } else {
            // Soft reference has been Garbage Collected
            sSoftBitmapCache.remove(url);
        }
    }

    return null;
}

From source file:com.waveface.installer.util.ImageDownloader.java

/**
 * @param url The URL of the image that will be retrieved from the cache.
 * @return The cached bitmap or null if it was not found.
 *///from   w ww .  j a  va  2 s.c o  m
private Bitmap getBitmapFromCache(String url) {
    // First try the hard reference cache
    synchronized (sHardBitmapCache) {
        final Bitmap bitmap = sHardBitmapCache.get(url);
        if (bitmap != null) {
            // Bitmap found in hard cache
            // Move element to first position, so that it is removed last
            sHardBitmapCache.remove(url);
            sHardBitmapCache.put(url, bitmap);
            return bitmap;
        }
    }

    File cacheDir = mContext.getCacheDir();
    File file = new File(cacheDir, Base64.encodeToString(url.getBytes(), Base64.DEFAULT));
    if (file.exists()) {
        Bitmap bm = BitmapFactory.decodeFile(file.getAbsolutePath());
        if (bm != null) {
            return bm;
        }
    }

    // Then try the soft reference cache
    SoftReference<Bitmap> bitmapReference = sSoftBitmapCache.get(url);
    if (bitmapReference != null) {
        final Bitmap bitmap = bitmapReference.get();
        if (bitmap != null) {
            // Bitmap found in soft cache
            return bitmap;
        } else {
            // Soft reference has been Garbage Collected
            sSoftBitmapCache.remove(url);
        }
    }

    return null;
}

From source file:com.app.hutbay.utility.ImageDownloader.java

/**
 * @param url The URL of the image that will be retrieved from the cache.
 * @return The cached bitmap or null if it was not found.
 *///w w w. j  ava2  s  . co m
public Bitmap getBitmapFromCache(String url) {
    // First try the hard reference cache
    synchronized (sHardBitmapCache) {
        final Bitmap bitmap = sHardBitmapCache.get(url);
        if (bitmap != null) {
            // Bitmap found in hard cache
            // Move element to first position, so that it is removed last
            sHardBitmapCache.remove(url);
            sHardBitmapCache.put(url, bitmap);
            return bitmap;
        }
    }

    // Then try the soft reference cache
    SoftReference<Bitmap> bitmapReference = sSoftBitmapCache.get(url);
    if (bitmapReference != null) {
        final Bitmap bitmap = bitmapReference.get();
        if (bitmap != null) {
            // Bitmap found in soft cache
            return bitmap;
        } else {
            // Soft reference has been Garbage Collected
            sSoftBitmapCache.remove(url);
        }
    }

    return null;
}

From source file:org.apache.ctakes.ytex.kernel.IntrinsicInfoContentEvaluatorImpl.java

/**
 * recursively compute the number of leaves. fill in the icInfoMap as we go
 * along/*from   ww w . j  av  a 2  s  .c  o m*/
 * 
 * @param concept
 *            concept for which we should get the leaves
 * @param leafCache
 *            cache of concept's leaves
 * @param icInfoMap
 *            to be updated with leaf counts
 * @param cg
 * @param w
 * @param visitedNodes
 *            list of nodes that have already been visited - we don't need
 *            to revisit them when getting the leaves
 * @return
 * @throws IOException
 */
private HashSet<Integer> getLeaves(ConcRel concept, SoftReference<HashSet<Integer>>[] leafCache,
        Map<String, IntrinsicICInfo> icInfoMap, ConceptGraph cg, BufferedWriter w,
        HashSet<Integer> visitedNodes) throws IOException {
    // look in cache
    SoftReference<HashSet<Integer>> refLeaves = leafCache[concept.getNodeIndex()];
    if (refLeaves != null && refLeaves.get() != null) {
        return refLeaves.get();
    }
    // not in cache - compute recursively
    HashSet<Integer> leaves = new HashSet<Integer>();
    leafCache[concept.getNodeIndex()] = new SoftReference<HashSet<Integer>>(leaves);
    if (concept.isLeaf()) {
        // for leaves, just add the concept id
        leaves.add(concept.getNodeIndex());
    } else {
        IntrinsicICInfo icInfo = icInfoMap.get(concept.getConceptID());
        // have we already computed the leaf count for this node?
        // if yes, then we can ignore previously visited nodes
        // if no, then compute it now and revisit previously visited nodes
        // if we have to
        boolean needLeaves = (icInfo != null && icInfo.getLeafCount() == 0);
        HashSet<Integer> visitedNodesLocal = visitedNodes;
        if (needLeaves || visitedNodesLocal == null) {
            // allocate a set to keep track of nodes we've already visited
            // so that we don't revisit them. if we have already computed
            // this node's leaf count then we reuse whatever the caller gave
            // us if non null, else allocate a new one.
            // if we haven't already computed this node's leaf count,
            // allocate a new set to avoid duplications in the traversal for
            // this node
            visitedNodesLocal = new HashSet<Integer>();
        }
        // for inner nodes, recurse
        for (ConcRel child : concept.getChildren()) {
            // if we've already visited a node, then don't bother adding
            // that node's leaves - we already have them
            if (!visitedNodesLocal.contains(child.getNodeIndex())) {
                leaves.addAll(getLeaves(child, leafCache, icInfoMap, cg, w, visitedNodesLocal));
            }
        }
        // add this node to the set of visited nodes so we know not to
        // revisit. This is only of importance if the caller gave us
        // a non-empty set.
        if (visitedNodes != null && visitedNodes != visitedNodesLocal) {
            visitedNodes.add(concept.getNodeIndex());
            visitedNodes.addAll(visitedNodesLocal);
        }
        // update the leaf count if we haven't done so already
        if (needLeaves) {
            icInfo.setLeafCount(leaves.size());
            // output leaves if desired
            if (w != null) {
                w.write(concept.getConceptID());
                w.write("\t");
                w.write(Integer.toString(leaves.size()));
                w.write("\t");
                Iterator<Integer> iter = leaves.iterator();
                while (iter.hasNext()) {
                    w.write(cg.getConceptList().get(iter.next()).getConceptID());
                    w.write(" ");
                }
                w.newLine();
            }
        }
    }
    return leaves;
}

From source file:net.peterkuterna.android.apps.devoxxfrsched.util.ImageDownloader.java

/**
 * @param url/*  ww  w .j a  v  a  2  s. c om*/
 *            The URL of the image that will be retrieved from the cache.
 * @return The cached bitmap or null if it was not found.
 */
private Bitmap getBitmapFromCache(String url) {
    if (url != null) {
        // First try the hard reference cache
        synchronized (sHardBitmapCache) {
            final Bitmap bitmap = sHardBitmapCache.get(url);
            if (bitmap != null) {
                // Bitmap found in hard cache
                // Move element to first position, so that it is removed
                // last
                sHardBitmapCache.remove(url);
                sHardBitmapCache.put(url, bitmap);
                return bitmap;
            }
        }

        // Then try the soft reference cache
        SoftReference<Bitmap> bitmapReference = sSoftBitmapCache.get(url);
        if (bitmapReference != null) {
            final Bitmap bitmap = bitmapReference.get();
            if (bitmap != null) {
                // Bitmap found in soft cache
                return bitmap;
            } else {
                // Soft reference has been Garbage Collected
                sSoftBitmapCache.remove(url);
            }
        }

        File cacheFile = null;
        try {
            MessageDigest mDigest = MessageDigest.getInstance("SHA-1");
            mDigest.update(url.getBytes());
            final String cacheKey = bytesToHexString(mDigest.digest());
            if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
                cacheFile = new File(Environment.getExternalStorageDirectory() + File.separator + "Android"
                        + File.separator + "data" + File.separator + mContext.getPackageName() + File.separator
                        + "cache" + File.separator + "bitmap_" + cacheKey + ".tmp");
            }
        } catch (NoSuchAlgorithmException e) {
            // Oh well, SHA-1 not available (weird), don't cache bitmaps.
        }

        if (cacheFile != null && cacheFile.exists()) {
            Bitmap cachedBitmap = BitmapFactory.decodeFile(cacheFile.toString());
            if (cachedBitmap != null) {
                addBitmapToCache(url, cachedBitmap);
                return cachedBitmap;
            }
        }
    }

    return null;
}

From source file:com.kiddobloom.bucketlist.ImageDownloader.java

/**
 * @param url/*from  w w w.ja  va2 s .  c  o  m*/
 *            The URL of the image that will be retrieved from the cache.
 * @return The cached bitmap or null if it was not found.
 */
private Bitmap getBitmapFromCache(String url) {
    // First try the hard reference cache
    synchronized (sHardBitmapCache) {
        final Bitmap bitmap = sHardBitmapCache.get(url);
        if (bitmap != null) {
            //Log.d("tag", "bitmap found in hard cache");
            // Bitmap found in hard cache
            // Move element to first position, so that it is removed last
            sHardBitmapCache.remove(url);
            sHardBitmapCache.put(url, bitmap);
            return bitmap;
        } else {
            //Log.d("tag", "bitmap not found in hard cache");
        }
    }

    // Then try the soft reference cache
    SoftReference<Bitmap> bitmapReference = sSoftBitmapCache.get(url);
    if (bitmapReference != null) {
        final Bitmap bitmap = bitmapReference.get();
        if (bitmap != null) {
            // Bitmap found in soft cache
            //Log.d("tag", "bitmap found in soft cache");

            return bitmap;
        } else {
            // Soft reference has been Garbage Collected
            sSoftBitmapCache.remove(url);
            //Log.d("tag", "bitmap not found in soft cache");
        }
    }

    return null;
}

From source file:by.lykashenko.interfaces.ImageDownloader.java

/**
 * @param url The URL of the image that will be retrieved from the cache.
 * @return The cached bitmap or null if it was not found.
 *///from  w  ww.  j av a  2  s  . com
private Bitmap getBitmapFromCache(String url) {

    if (state == 1) {
        // First try the hard reference cache
        synchronized (sHardBitmapCache) {
            final Bitmap bitmap = sHardBitmapCache.get(url);
            if (bitmap != null) {
                // Bitmap found in hard cache
                // Move element to first position, so that it is removed last
                sHardBitmapCache.remove(url);
                sHardBitmapCache.put(url, bitmap);
                return bitmap;
            }
        }

        // Then try the soft reference cache
        SoftReference<Bitmap> bitmapReference = sSoftBitmapCache.get(url);
        if (bitmapReference != null) {
            final Bitmap bitmap = bitmapReference.get();
            if (bitmap != null) {
                // Bitmap found in soft cache
                return bitmap;
            } else {
                // Soft reference has been Garbage Collected
                sSoftBitmapCache.remove(url);
            }
        }
    } else {
        String[] separated = url.split("/");

        String file = new StringBuilder(path).append("/").append(separated[7]).toString();

        Bitmap bitmap = BitmapFactory.decodeFile(file);
        return bitmap;
    }

    return null;
}

From source file:com.poloniumarts.utils.ImageDownloader.java

/**
 * @param url/*from w  ww  .jav a  2s. c o m*/
 *            The URL of the image that will be retrieved from the cache.
 * @return The cached bitmap or null if it was not found.
 */
private Bitmap getBitmapFromCache(String url) {
    // First try the hard reference cache
    synchronized (sHardBitmapCache) {
        Bitmap bitmap = sHardBitmapCache.get(url);
        if (bitmap != null) {
            // Bitmap found in hard cache
            // Move element to first position, so that it is removed last
            sHardBitmapCache.remove(url);
            sHardBitmapCache.put(url, bitmap);
            return bitmap;
        } else {
            sHardBitmapCache.remove(url);
        }
    }

    // Then try the soft reference cache
    SoftReference<Bitmap> bitmapReference = sSoftBitmapCache.get(url);
    if (bitmapReference != null) {
        final Bitmap bitmap = bitmapReference.get();
        if (bitmap != null) {
            // Bitmap found in soft cache
            return bitmap;
        } else {
            // Soft reference has been Garbage Collected
            sSoftBitmapCache.remove(url);
        }
    }

    return null;
}