Example usage for java.lang.ref SoftReference SoftReference

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

Introduction

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

Prototype

public SoftReference(T referent) 

Source Link

Document

Creates a new soft reference that refers to the given object.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    SoftReference<String> sr = new SoftReference<String>("object");

    Object o = sr.get();/*from  www  . j  a va 2 s .c om*/
    if (o != null) {
        System.out.println(o);
    } else {
        System.out.println("collected or has been reclaimed");
    }
}

From source file:Main.java

private static StringBuffer getStrBuf() {
    StringBuffer result = null;/*from   ww w .  j av a  2s .  com*/
    if (strBfeCache != null)
        result = strBfeCache.get();
    if (result == null) {
        result = new StringBuffer();
        strBfeCache = new SoftReference<StringBuffer>(result);
    }
    return result;
}

From source file:Main.java

private static void putStringForSoftReference(String requestUrl, String result) {
    // TODO Auto-generated method stub
    SoftReference<String> referece = new SoftReference<String>(result);
    RequestCache.put(requestUrl, referece);
}

From source file:Main.java

public static Bitmap byteToBitmap(byte[] imgByte) {
    InputStream input = null;/*w  ww .  ja v a 2 s  .co  m*/
    Bitmap bitmap = null;
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 1;
    input = new ByteArrayInputStream(imgByte);
    SoftReference softRef = new SoftReference(BitmapFactory.decodeStream(input, null, options));
    bitmap = (Bitmap) softRef.get();
    if (imgByte != null) {
        imgByte = null;
    }

    try {
        if (input != null) {
            input.close();
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return bitmap;
}

From source file:Main.java

public static void showToast(final Context context, final String s) {

    if (Looper.getMainLooper() == Looper.myLooper()) {
        Toast toast = Toast.makeText(context, s, Toast.LENGTH_SHORT);
        hideToast();//  www.  j av a  2s  .  c  om
        toast.show();
        sToastRef = new SoftReference<Toast>(toast);
    } else {
        if (context instanceof Activity) {
            ((Activity) context).runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    showToast(context, s);
                }
            });
        }
    }
}

From source file:Main.java

private static BufferedImage getPooledImage(int width, int height, int index) {
    String key = String.format("%dx%d#%d",
            new Object[] { Integer.valueOf(width), Integer.valueOf(height), Integer.valueOf(index) });
    Reference ref = (Reference) imagePool.get(key);
    BufferedImage image = ref == null ? null : (BufferedImage) ref.get();

    if (image == null) {
        image = new BufferedImage(width, height, 2);
        imagePool.put(key, new SoftReference(image));
    }//  w  w w  .  j av a2s .co m

    return image;
}

From source file:Main.java

public static SoftReference<Bitmap> getBitmap(Resources resources, int drawable) {
    Bitmap bitmap = BitmapFactory.decodeResource(resources, drawable);
    return new SoftReference<Bitmap>(bitmap);
}

From source file:Main.java

/**
 * Returns a cached <i>JAXB Context</i>.
 *
 * @since 3.8//from  w  ww  .j  av a 2 s.  c  o  m
 */
public synchronized static JAXBContext createContext(final Class<?> bind) throws JAXBException {
    JAXBContext context = null;
    SoftReference<JAXBContext> ref = contextCache.get(bind);
    if (ref != null) {
        context = ref.get();
        if (context == null)
            contextCache.remove(bind);
    }

    if (context != null)
        return context;

    synchronized (bind) {
        synchronized (JAXBContext.class) {
            context = JAXBContext.newInstance(bind);
        }
    }

    contextCache.put(bind, new SoftReference<JAXBContext>(context));

    return context;
}

From source file:Main.java

private static void putIntoCache(final String fontname, final Typeface font) {
    synchronized (sTypeCache) {
        sTypeCache.put(fontname, new SoftReference<Typeface>(font));
    }//from  w ww  . j a v  a 2  s .c o  m
}

From source file:Main.java

public static XPath getSharedXPath() {
    XPath xPath = null;/*  w  w w  .j  a va2s  . c om*/
    if (sharedXPath != null)
        xPath = sharedXPath.get();
    if (xPath == null) {
        XPathFactory xPathFactory = XPathFactory.newInstance();
        sharedXPath = new SoftReference<XPath>(xPath = xPathFactory.newXPath());
    }
    return xPath;
}