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:myfightinglayoutbugs.ScreenshotCache.java

@Nonnull
public Screenshot getScreenshot(Condition condition) {
    SoftReference<Screenshot> softReference = _memoryCache.get(condition);
    Screenshot screenshot;//  w  ww  . j a  v a  2  s.co m
    if (softReference == null) {
        // Cache miss, take screenshot ...
        screenshot = takeScreenshot(condition);
        // ... and cache it ...
        _memoryCache.put(condition, new SoftReference<Screenshot>(screenshot));
        _diskCache.put(condition, saveToTempFile(screenshot));
    } else {
        screenshot = softReference.get();
        if (screenshot == null) {
            // screenshot in _memoryCache was garbage collected, read it from _diskCache ...
            File file = _diskCache.get(condition);
            screenshot = readFromFile(file);
            // ... and put it into _memoryCache again ...
            _memoryCache.put(condition, new SoftReference<Screenshot>(screenshot));
        }
    }
    return screenshot;
}

From source file:com.developer4droid.contactslister.backend.image_load.EnhancedImageDownloader.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 a  v  a  2  s .c  om*/
private Bitmap getBitmapFromCache(String url, ProgressImageView pHolder) {
    // I identify images by hashcode. Not a perfect solution, good for the
    // demo.
    String filename = String.valueOf(url.hashCode());
    File f = new File(cacheDir, filename);

    // from SD cache
    // if file is stored so simply read it, do not resize
    //        if(!loadedBmpList.contains(url)){
    Bitmap bmp = readFile(f);
    if (bmp != null) {
        //                loadedBmpList.add(url);
        pHolder.bitmap = bmp;
        addBitmapToCache(url, pHolder);
    }
    //        }

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

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

    return null;
}

From source file:cat.joronya.utils.image.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 w  w  .j a v  a  2s  . 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;
        }
    }

    // Then try the soft reference cache
    SoftReference<Bitmap> bitmapReference = sSoftBitmapCache.get(url);
    if (bitmapReference != null) {
        final Bitmap bitmap = bitmapReference.get();
        if (bitmap != null) {
            // found, remove from notfounds
            notFounds.remove(url);

            // Bitmap found in soft cache
            return bitmap;
        } else {
            // Soft reference has been Garbage Collected
            sSoftBitmapCache.remove(url);
        }
    }

    // otherwise try disk cache, no sabem el filename i guardem un hash
    URL theURL = null;
    try {
        theURL = new URL(url);
    } catch (MalformedURLException e) {
        Log.d(LOG_TAG, "Image URL malformed");

        // registrem el 404
        notFounds.add(url);

        return null;
    }

    String[] filename = theURL.getFile().split("/");
    File f = new File(cacheDir, filename[filename.length - 1]);

    //from SD cache
    Bitmap b = decodeFile(f);
    if (b != null) {
        // guardem fitxer a cache
        Log.d(LOG_TAG, "Image found in SD cache and loaded: " + filename);

        // afegim a la cache per proxims usos
        addBitmapToCache(url, b);

        // if url found, remove url from notfounds
        notFounds.remove(url);

        // retornat
        return b;
    }

    // registrem el 404
    notFounds.add(url);

    return null;
}

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

/**
 * @param url//from  w  ww  .java2 s  .  co  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) {

    if (mExternalStorageAvailable && url != null) {
        File f = new File(mfolder, URLEncoder.encode(url));

        if (f.exists()) {
            try {
                return BitmapFactory.decodeFile(f.getPath());

            } catch (Exception e) {
                // Log.e(Constants.PROJECT_TAG, "Error in retrieving picture", e);
                e.printStackTrace();
            }
        }
    }

    // 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
    try {
        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);
            }
        }
    } catch (Exception e) {
        return null;
    }

    return null;
}

From source file:com.life.wuhan.util.ImageDownloader.java

/**
 * @param url The URL of the image that will be retrieved from the
 *        cache.// w ww .  jav  a 2  s . c o m
 * @return The cached bitmap or null if it was not found.
 */
private Bitmap getBitmapFromCache(String url) {
    if (TextUtils.isEmpty(url)) {
        return 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);
        }
    }

    // look from local file.
    Bitmap bitmap = getBitmapFromCacheFile(url);

    if (bitmap != null) {
        addBitmapToCache(url, bitmap);
    }
    return bitmap;
}

From source file:org.atricore.idbus.kernel.main.databinding.JAXBUtils.java

/**
 * Get a JAXBContext for the class//from   w  ww  .ja va 2  s. c o  m
 *
 * Note: The contextPackage object is used by multiple threads.  It should be considered immutable
 * and not altered by this method.
 *
 * @param contextPackage  Set<Package>
 * @param contructionType (output value that indicates how the context was constructed)
 * @param forceArrays (forces the returned JAXBContext to include the array types)
 * @param cacheKey ClassLoader
 * @return JAXBContext
 * @throws javax.xml.bind.JAXBException
 */
public static JAXBContext getJAXBContext(TreeSet<String> contextPackages,
        Holder<CONSTRUCTION_TYPE> constructionType, boolean forceArrays, String key, ClassLoader cacheKey,
        Map<String, ?> properties) throws JAXBException {
    // JAXBContexts for the same class can be reused and are supposed to be thread-safe
    if (log.isDebugEnabled()) {
        log.debug("Following packages are in this batch of getJAXBContext() :");
        for (String pkg : contextPackages) {
            log.debug(pkg);
        }
    }
    if (JAXBUtilsMonitor.isMonitoring()) {
        JAXBUtilsMonitor.addPackageKey(contextPackages.toString());
    }

    // Get or Create The InnerMap using the package key
    ConcurrentHashMap<ClassLoader, JAXBContextValue> innerMap = null;
    SoftReference<ConcurrentHashMap<ClassLoader, JAXBContextValue>> softRef = jaxbMap.get(key);

    if (softRef != null) {
        innerMap = softRef.get();
    }

    if (innerMap == null) {
        synchronized (jaxbMap) {
            softRef = jaxbMap.get(key);
            if (softRef != null) {
                innerMap = softRef.get();
            }
            if (innerMap == null) {
                innerMap = new ConcurrentHashMap<ClassLoader, JAXBContextValue>();
                softRef = new SoftReference<ConcurrentHashMap<ClassLoader, JAXBContextValue>>(innerMap);
                jaxbMap.put(key, softRef);
            }
        }
    }

    // Now get the contextValue using either the classloader key or
    // the current Classloader
    ClassLoader cl = getContextClassLoader();
    JAXBContextValue contextValue = null;
    if (cacheKey != null) {
        if (log.isDebugEnabled()) {
            log.debug("Using supplied classloader to retrieve JAXBContext: " + cacheKey);
        }
        contextValue = innerMap.get(cacheKey);
    } else {
        if (log.isDebugEnabled()) {
            log.debug("Using classloader from Thread to retrieve JAXBContext: " + cl);
        }
        contextValue = innerMap.get(cl);
    }

    // If the context value is found, but the caller requested that the JAXBContext
    // contain arrays, then rebuild the JAXBContext value
    if (forceArrays && contextValue != null
            && contextValue.constructionType != JAXBUtils.CONSTRUCTION_TYPE.BY_CLASS_ARRAY_PLUS_ARRAYS) {
        if (log.isDebugEnabled()) {
            log.debug("Found a JAXBContextValue with constructionType=" + contextValue.constructionType
                    + "  but the caller requested a JAXBContext "
                    + " that includes arrays.  A new JAXBContext will be built");
        }
        contextValue = null;
    }

    if (contextPackages == null) {
        contextPackages = new TreeSet<String>();
    }
    if (contextValue == null) {
        synchronized (innerMap) {
            // Try to get the contextValue once more since sync was temporarily exited.
            ClassLoader clKey = (cacheKey != null) ? cacheKey : cl;
            contextValue = innerMap.get(clKey);
            adjustPoolSize(innerMap);
            if (forceArrays && contextValue != null
                    && contextValue.constructionType != JAXBUtils.CONSTRUCTION_TYPE.BY_CLASS_ARRAY_PLUS_ARRAYS) {
                contextValue = null;
            }
            if (contextValue == null) {
                // Create a copy of the contextPackages.  This new TreeSet will
                // contain only the valid contextPackages.
                // Note: The original contextPackage set is accessed by multiple
                // threads and should not be altered.

                TreeSet<String> validContextPackages = new TreeSet<String>(contextPackages);

                List<String> classRefs = pruneDirectives(validContextPackages);

                int numPackages = validContextPackages.size();

                contextValue = createJAXBContextValue(validContextPackages, clKey, forceArrays, properties,
                        classRefs);

                synchronized (jaxbMap) {
                    // Add the context value with the original package set
                    ConcurrentHashMap<ClassLoader, JAXBContextValue> map1 = null;
                    SoftReference<ConcurrentHashMap<ClassLoader, JAXBContextValue>> softRef1 = jaxbMap.get(key);
                    if (softRef1 != null) {
                        map1 = softRef1.get();
                    }
                    if (map1 == null) {
                        map1 = new ConcurrentHashMap<ClassLoader, JAXBContextValue>();
                        softRef1 = new SoftReference<ConcurrentHashMap<ClassLoader, JAXBContextValue>>(map1);
                        jaxbMap.put(key, softRef1);
                    }
                    map1.put(clKey, contextValue);

                    String validPackagesKey = validContextPackages.toString();

                    // Add the context value with the new package set
                    ConcurrentHashMap<ClassLoader, JAXBContextValue> map2 = null;
                    SoftReference<ConcurrentHashMap<ClassLoader, JAXBContextValue>> softRef2 = jaxbMap
                            .get(validPackagesKey);
                    if (softRef2 != null) {
                        map2 = softRef2.get();
                    }
                    if (map2 == null) {
                        map2 = new ConcurrentHashMap<ClassLoader, JAXBContextValue>();
                        softRef2 = new SoftReference<ConcurrentHashMap<ClassLoader, JAXBContextValue>>(map2);
                        jaxbMap.put(validPackagesKey, softRef2);
                    }
                    map2.put(clKey, contextValue);

                    if (log.isDebugEnabled()) {
                        log.debug("JAXBContext [created] for " + key);
                        log.debug("JAXBContext also stored by the list of valid packages:" + validPackagesKey);
                    }
                }
            }
        }
    } else {
        if (log.isDebugEnabled()) {
            log.debug("JAXBContext [from pool] for " + key);
        }
    }
    if (log.isDebugEnabled()) {
        log.debug("JAXBContext constructionType= " + contextValue.constructionType);
        log.debug("JAXBContextValue = " + JavaUtils.getObjectIdentity(contextValue));
        log.debug("JAXBContext = " + JavaUtils.getObjectIdentity(contextValue.jaxbContext));
    }
    constructionType.value = contextValue.constructionType;
    return contextValue.jaxbContext;
}

From source file:com.gfan.sbbs.utils.images.ImageManager.java

/**
 * /*from  w  w  w .jav a  2s .  co m*/
 * 
 * @param file
 *            file URL/file PATH
 * @param bitmap
 * @param quality
 */
@Override
public Bitmap get(String file) {
    SoftReference<Bitmap> ref;
    Bitmap bitmap;

    // Look in memory first.
    synchronized (this) {
        ref = mCache.get(file);
    }

    if (ref != null) {
        bitmap = ref.get();

        if (bitmap != null) {
            return bitmap;
        }
    }

    // Now try file.
    bitmap = lookupFile(file);

    if (bitmap != null) {
        synchronized (this) {
            mCache.put(file, new SoftReference<Bitmap>(bitmap));
        }

        return bitmap;
    }

    // TODO: why?
    // upload: see profileImageCacheManager line 96
    Log.w(TAG, "Image is missing: " + file);
    // return the default photo
    return mDefaultBitmap;
}

From source file:com.android.aft.AFCoreTools.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  w w .  j a v a 2s .co m
private Bitmap getBitmapFromCache(String url, int reqWidth, int reqHeight) {
    // 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);
        }
    }

    // Then try the persistent cache
    if (mContext != null) {
        try {
            final String filePath = filePathForUrl(url);

            final Bitmap bitmap = decodeSampledBitmapFromFile(filePath, reqWidth, reqHeight);
            if (bitmap != null) {
                return bitmap;
            }
        } catch (Exception ex) {
            return null;
        }
    }

    return null;
}

From source file:org.apache.axis2.jaxws.message.databinding.JAXBUtils.java

/**
 * Get a JAXBContext for the class/*from  w w  w . jav  a  2s. c  o  m*/
 *
 * Note: The contextPackage object is used by multiple threads.  It should be considered immutable
 * and not altered by this method.
 * 
 * @param contextPackage  Set<Package> 
 * @param contructionType (output value that indicates how the context was constructed)
 * @param forceArrays (forces the returned JAXBContext to include the array types)
 * @param cacheKey ClassLoader
 * @return JAXBContext
 * @throws JAXBException
 */
public static JAXBContext getJAXBContext(TreeSet<String> contextPackages,
        Holder<CONSTRUCTION_TYPE> constructionType, boolean forceArrays, String key, ClassLoader cacheKey,
        Map<String, ?> properties) throws JAXBException {
    // JAXBContexts for the same class can be reused and are supposed to be thread-safe
    if (log.isDebugEnabled()) {
        log.debug("Following packages are in this batch of getJAXBContext() :");
        for (String pkg : contextPackages) {
            log.debug(pkg);
        }
    }
    if (JAXBUtilsMonitor.isMonitoring()) {
        JAXBUtilsMonitor.addPackageKey(contextPackages.toString());
    }

    // Get or Create The InnerMap using the package key
    ConcurrentHashMap<ClassLoader, JAXBContextValue> innerMap = null;
    SoftReference<ConcurrentHashMap<ClassLoader, JAXBContextValue>> softRef = jaxbMap.get(key);

    if (softRef != null) {
        innerMap = softRef.get();
    }

    if (innerMap == null) {
        synchronized (jaxbMap) {
            softRef = jaxbMap.get(key);
            if (softRef != null) {
                innerMap = softRef.get();
            }
            if (innerMap == null) {
                innerMap = new ConcurrentHashMap<ClassLoader, JAXBContextValue>();
                softRef = new SoftReference<ConcurrentHashMap<ClassLoader, JAXBContextValue>>(innerMap);
                jaxbMap.put(key, softRef);
            }
        }
    }

    // Now get the contextValue using either the classloader key or 
    // the current Classloader
    ClassLoader cl = getContextClassLoader();
    JAXBContextValue contextValue = null;
    if (cacheKey != null) {
        if (log.isDebugEnabled()) {
            log.debug("Using supplied classloader to retrieve JAXBContext: " + cacheKey);
        }
        contextValue = innerMap.get(cacheKey);
    } else {
        if (log.isDebugEnabled()) {
            log.debug("Using classloader from Thread to retrieve JAXBContext: " + cl);
        }
        contextValue = innerMap.get(cl);
    }

    // If the context value is found, but the caller requested that the JAXBContext
    // contain arrays, then rebuild the JAXBContext value
    if (forceArrays && contextValue != null
            && contextValue.constructionType != JAXBUtils.CONSTRUCTION_TYPE.BY_CLASS_ARRAY_PLUS_ARRAYS) {
        if (log.isDebugEnabled()) {
            log.debug("Found a JAXBContextValue with constructionType=" + contextValue.constructionType
                    + "  but the caller requested a JAXBContext "
                    + " that includes arrays.  A new JAXBContext will be built");
        }
        contextValue = null;
    }

    if (contextPackages == null) {
        contextPackages = new TreeSet<String>();
    }
    if (contextValue == null) {
        synchronized (innerMap) {
            // Try to get the contextValue once more since sync was temporarily exited.
            ClassLoader clKey = (cacheKey != null) ? cacheKey : cl;
            contextValue = innerMap.get(clKey);
            adjustPoolSize(innerMap);
            if (forceArrays && contextValue != null
                    && contextValue.constructionType != JAXBUtils.CONSTRUCTION_TYPE.BY_CLASS_ARRAY_PLUS_ARRAYS) {
                contextValue = null;
            }
            if (contextValue == null) {
                // Create a copy of the contextPackages.  This new TreeSet will
                // contain only the valid contextPackages.
                // Note: The original contextPackage set is accessed by multiple 
                // threads and should not be altered.

                TreeSet<String> validContextPackages = new TreeSet<String>(contextPackages);

                List<String> classRefs = pruneDirectives(validContextPackages);

                int numPackages = validContextPackages.size();

                contextValue = createJAXBContextValue(validContextPackages, clKey, forceArrays, properties,
                        classRefs);

                synchronized (jaxbMap) {
                    // Add the context value with the original package set
                    ConcurrentHashMap<ClassLoader, JAXBContextValue> map1 = null;
                    SoftReference<ConcurrentHashMap<ClassLoader, JAXBContextValue>> softRef1 = jaxbMap.get(key);
                    if (softRef1 != null) {
                        map1 = softRef1.get();
                    }
                    if (map1 == null) {
                        map1 = new ConcurrentHashMap<ClassLoader, JAXBContextValue>();
                        softRef1 = new SoftReference<ConcurrentHashMap<ClassLoader, JAXBContextValue>>(map1);
                        jaxbMap.put(key, softRef1);
                    }
                    map1.put(clKey, contextValue);

                    String validPackagesKey = validContextPackages.toString();

                    // Add the context value with the new package set
                    ConcurrentHashMap<ClassLoader, JAXBContextValue> map2 = null;
                    SoftReference<ConcurrentHashMap<ClassLoader, JAXBContextValue>> softRef2 = jaxbMap
                            .get(validPackagesKey);
                    if (softRef2 != null) {
                        map2 = softRef2.get();
                    }
                    if (map2 == null) {
                        map2 = new ConcurrentHashMap<ClassLoader, JAXBContextValue>();
                        softRef2 = new SoftReference<ConcurrentHashMap<ClassLoader, JAXBContextValue>>(map2);
                        jaxbMap.put(validPackagesKey, softRef2);
                    }
                    map2.put(clKey, contextValue);

                    if (log.isDebugEnabled()) {
                        log.debug("JAXBContext [created] for " + key);
                        log.debug("JAXBContext also stored by the list of valid packages:" + validPackagesKey);
                    }
                }
            }
        }
    } else {
        if (log.isDebugEnabled()) {
            log.debug("JAXBContext [from pool] for " + key);
        }
    }
    if (log.isDebugEnabled()) {
        log.debug("JAXBContext constructionType= " + contextValue.constructionType);
        log.debug("JAXBContextValue = " + JavaUtils.getObjectIdentity(contextValue));
        log.debug("JAXBContext = " + JavaUtils.getObjectIdentity(contextValue.jaxbContext));
    }
    constructionType.value = contextValue.constructionType;
    return contextValue.jaxbContext;
}

From source file:com.mpdeimos.ct_tests.processors.CloneTrackingLoop.java

@AConQATParameter(name = "result", minOccurrences = 0, description = "detection result")
public void setFilename(
        @AConQATAttribute(name = "list", description = "detection result list") SoftReference<List<CloneDetectionResultElement>> cdResultList) {
    this.cdResultList = cdResultList.get();
}