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.aapbd.utils.image.CacheImageDownloader.java

/**
 * @param url/* w  w w.j  a  v  a  2  s .  com*/
 *            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 = 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.secretlisa.lib.utils.BaseImageLoader.java

/**
 * @param url//from w w  w . ja va  2  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 (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);
        }
    }

    Log.d(TAG, "look from local file:" + url);
    // look from local file.
    Bitmap bitmap = getBitmapFromCacheFile(url);

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

From source file:www.image.ImageManager.java

/**
 * ?//from ww w. j  a  v a 2 s  .c o m
 * @param file file URL/file PATH
 * @param bitmap
 * @param quality
 */
public Bitmap get(String file) {
    SoftReference<Bitmap> ref;
    Bitmap bitmap;
    //?
    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;
    }
    //
    return mDefaultBitmap;
}

From source file:com.synconset.ImageFetcher.java

/**
 * @param position/*from www  .  ja va 2 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(Integer position) {
    // First try the hard reference cache
    synchronized (sHardBitmapCache) {
        final Bitmap bitmap = sHardBitmapCache.get(position);
        if (bitmap != null) {
            // Log.d("CACHE ****** ", "Hard hit!");
            // Bitmap found in hard cache
            // Move element to first position, so that it is removed last
            return bitmap;
        }
    }

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

    return null;
}

From source file:com.hunch.ImageManager.java

protected Drawable getDrawableFromMemoryCache(final Context context, final URL url) {
    SoftReference<Bitmap> drawableRef = drawableMap.get(url);
    if (drawableRef != null) {
        // the key exists
        Bitmap image = drawableRef.get();
        if (image != null) {
            Log.v(Const.TAG, "found image in memory cache (" + fileNameFromURL(url) + ")");

            // the image hasn't been gc'd yet.
            return new BitmapDrawable(context.getResources(), image);
        } else {/* ww w.j a  v  a  2  s  . c  o  m*/
            // the drawable has been gc'd, remove the key
            drawableMap.remove(url);
        }
    }

    return null;
}

From source file:se.dw.okhttpwrapper.ImageRequest.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  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) {
            // Bitmap found in soft cache
            return bitmap;
        } else {
            // Soft reference has been Garbage Collected
            sSoftBitmapCache.remove(url);
        }
    }

    //Then try from file...TODO
    Bitmap fromFile = getFromFileCache(url);
    if (fromFile != null) {
        return fromFile;
    }

    return null;
}

From source file:com.lastorder.pushnotifications.data.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 a v a 2 s.  co  m
private Bitmap getBitmapFromCache(String url) {
    // First try the hard reference cache
    try {
        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);
            }
        }

        final String pathName = cacheDir + "/" + url.hashCode() + "-t.jpg";
        final Bitmap bitmap = BitmapFactory.decodeFile(pathName);

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

        return null;
    } catch (OutOfMemoryError e) {
        return null;
    }
}

From source file:org.red5.cache.impl.CacheImpl.java

/** {@inheritDoc} */
public boolean offer(String name, Object obj) {
    boolean accepted = false;
    // check map size
    if (CACHE.size() < capacity) {
        SoftReference<?> tmp = CACHE.get(name);
        // because soft references can be garbage collected when a system is
        // in need of memory, we will check that the cacheable object is
        // valid/*from  w  w  w  .j a va  2s .  co  m*/
        // log.debug("Softreference: " + (null == tmp));
        // if (null != tmp) {
        // log.debug("Softreference value: " + (null == tmp.get()));
        // }
        if (null == tmp || null == tmp.get()) {
            ICacheable cacheable = null;
            if (obj instanceof ICacheable) {
                cacheable = (ICacheable) obj;
            } else {
                cacheable = new CacheableImpl(obj);
            }
            // set the objects name
            cacheable.setName(name);
            // set a registry entry
            registry.put(name, 1);
            // create a soft reference
            SoftReference<ICacheable> value = new SoftReference<ICacheable>(cacheable);
            CACHE.put(name, value);
            // set acceptance
            accepted = true;
            log.info("{} has been added to the cache. Current size: {}", name, CACHE.size());
        }
    } else {
        log.warn("Cache has reached max element size: " + capacity);
    }
    return accepted;
}

From source file:org.red5.server.cache.CacheImpl.java

/** {@inheritDoc} */
public boolean offer(String name, Object obj) {
    boolean accepted = false;
    // check map size
    if (CACHE.size() < capacity) {
        SoftReference tmp = CACHE.get(name);
        // because soft references can be garbage collected when a system is
        // in need of memory, we will check that the cacheable object is
        // valid/*from   ww  w. j  a v  a2 s. c  o m*/
        // log.debug("Softreference: " + (null == tmp));
        // if (null != tmp) {
        // log.debug("Softreference value: " + (null == tmp.get()));
        // }
        if (null == tmp || null == tmp.get()) {
            ICacheable cacheable = null;
            if (obj instanceof ICacheable) {
                cacheable = (ICacheable) obj;
            } else {
                cacheable = new CacheableImpl(obj);
            }
            // set the objects name
            cacheable.setName(name);
            // set a registry entry
            registry.put(name, 1);
            // create a soft reference
            SoftReference<ICacheable> value = new SoftReference<ICacheable>(cacheable);
            CACHE.put(name, value);
            // set acceptance
            accepted = true;
            log.info(name + " has been added to the cache. Current size: " + CACHE.size());
        }
    } else {
        log.warn("Cache has reached max element size: " + capacity);
    }
    return accepted;
}

From source file:com.googlecode.fightinglayoutbugs.ScreenshotCache.java

public @Nonnull Screenshot getScreenshot(Condition condition) {
    SoftReference<Screenshot> softReference = _memoryCache.get(condition);
    Screenshot screenshot;/*from  w  ww .j  a  v  a  2 s.c om*/
    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;
}