Search in sources :

Example 16 with SoftReference

use of java.lang.ref.SoftReference in project robovm by robovm.

the class SoftReferenceTest method test_ConstructorLjava_lang_Object.

/**
     * java.lang.ref.SoftReference#SoftReference(java.lang.Object)
     */
public void test_ConstructorLjava_lang_Object() {
    bool = new Boolean(true);
    try {
        SoftReference sr = new SoftReference(bool);
        assertTrue("Initialization failed.", ((Boolean) sr.get()).booleanValue());
    } catch (Exception e) {
        fail("Exception during test : " + e.getMessage());
    }
}
Also used : SoftReference(java.lang.ref.SoftReference)

Example 17 with SoftReference

use of java.lang.ref.SoftReference in project robovm by robovm.

the class SoftReferenceTest method test_get.

/**
     * java.lang.ref.SoftReference#get()
     */
public void test_get() {
    bool = new Boolean(false);
    SoftReference sr = new SoftReference(bool);
    assertTrue("Same object not returned.", bool == sr.get());
}
Also used : SoftReference(java.lang.ref.SoftReference)

Example 18 with SoftReference

use of java.lang.ref.SoftReference in project robovm by robovm.

the class SoftReferenceTest method test_get_SoftReference.

@SideEffect("Causes OutOfMemoryError to test finalization")
public void test_get_SoftReference() {
    class TestObject {

        public boolean finalized;

        public TestObject() {
            finalized = false;
        }

        protected void finalize() {
            finalized = true;
        }
    }
    final ReferenceQueue rq = new ReferenceQueue();
    class TestThread extends Thread {

        public void run() {
            Object testObj = new TestObject();
            r = new SoftReference(testObj, rq);
        }
    }
    Reference ref;
    try {
        TestThread t = new TestThread();
        t.start();
        t.join();
        Vector<StringBuffer> v = new Vector<StringBuffer>();
        try {
            while (true) {
                v.add(new StringBuffer(10000));
            }
        } catch (OutOfMemoryError ofme) {
            v = null;
        }
    } catch (InterruptedException e) {
        fail("InterruptedException : " + e.getMessage());
    }
    assertNull("get() should return null " + "if OutOfMemoryError is thrown.", r.get());
    try {
        TestThread t = new TestThread();
        t.start();
        t.join();
        FinalizationTester.induceFinalization();
        ref = rq.poll();
        assertNotNull("Object not garbage collected.", ref);
        assertNull("Object is not null.", ref.get());
        assertNotNull("Object could not be reclaimed.", r.get());
    } catch (Exception e) {
        fail("Exception : " + e.getMessage());
    }
}
Also used : ReferenceQueue(java.lang.ref.ReferenceQueue) Reference(java.lang.ref.Reference) SoftReference(java.lang.ref.SoftReference) SoftReference(java.lang.ref.SoftReference) Vector(java.util.Vector) SideEffect(dalvik.annotation.SideEffect)

Example 19 with SoftReference

use of java.lang.ref.SoftReference in project robovm by robovm.

the class ReferenceQueueTest method test_poll.

/**
     * java.lang.ref.ReferenceQueue#poll()
     */
public void test_poll() {
    // store in a static so it won't be gc'ed because the jit
    // optimized it out
    b = new Boolean(true);
    Object obj = new Object();
    String str = "Test";
    SoftReference sr = new SoftReference(b, rq);
    WeakReference wr = new WeakReference(obj, rq);
    PhantomReference pr = new PhantomReference(str, rq);
    assertNull(rq.poll());
    sr.enqueue();
    wr.enqueue();
    pr.enqueue();
    try {
        assertNull("Remove failed.", rq.poll().get());
    } catch (Exception e) {
        fail("Exception during the test : " + e.getMessage());
    }
    try {
        assertEquals("Remove failed.", obj, (rq.poll().get()));
    } catch (Exception e) {
        fail("Exception during the test : " + e.getMessage());
    }
    try {
        assertTrue("Remove failed.", ((Boolean) rq.poll().get()).booleanValue());
    } catch (Exception e) {
        fail("Exception during the test : " + e.getMessage());
    }
    assertNull(rq.poll());
    sr.enqueue();
    wr.enqueue();
    FinalizationTester.induceFinalization();
    assertNull(rq.poll());
}
Also used : SoftReference(java.lang.ref.SoftReference) WeakReference(java.lang.ref.WeakReference) PhantomReference(java.lang.ref.PhantomReference)

Example 20 with SoftReference

use of java.lang.ref.SoftReference in project ceylon-compiler by ceylon.

the class ZipFileIndex method inflate.

private int inflate(byte[] src, byte[] dest) {
    Inflater inflater = (inflaterRef == null ? null : inflaterRef.get());
    // construct the inflater object or reuse an existing one
    if (inflater == null)
        inflaterRef = new SoftReference<Inflater>(inflater = new Inflater(true));
    inflater.reset();
    inflater.setInput(src);
    try {
        return inflater.inflate(dest);
    } catch (DataFormatException ex) {
        return -1;
    }
}
Also used : DataFormatException(java.util.zip.DataFormatException) SoftReference(java.lang.ref.SoftReference) Inflater(java.util.zip.Inflater)

Aggregations

SoftReference (java.lang.ref.SoftReference)56 ReferenceQueue (java.lang.ref.ReferenceQueue)7 Reference (java.lang.ref.Reference)6 WeakReference (java.lang.ref.WeakReference)6 PhantomReference (java.lang.ref.PhantomReference)5 Random (java.util.Random)5 IOException (java.io.IOException)4 Bitmap (android.graphics.Bitmap)3 Context (android.content.Context)2 BitmapFactory (android.graphics.BitmapFactory)2 Handler (android.os.Handler)2 Message (android.os.Message)2 MetaClass (groovy.lang.MetaClass)2 Shape (java.awt.Shape)2 File (java.io.File)2 InputStream (java.io.InputStream)2 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)2 PrivilegedAction (java.security.PrivilegedAction)2 PrivilegedActionException (java.security.PrivilegedActionException)2 SecureClassLoader (java.security.SecureClassLoader)2