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());
}
}
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());
}
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());
}
}
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());
}
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;
}
}
Aggregations