use of java.lang.ref.SoftReference in project hackpad by dropbox.
the class PolicySecurityController method callWithDomain.
@Override
public Object callWithDomain(final Object securityDomain, final Context cx, Callable callable, Scriptable scope, Scriptable thisObj, Object[] args) {
// Run in doPrivileged as we might be checked for "getClassLoader"
// runtime permission
final ClassLoader classLoader = (ClassLoader) AccessController.doPrivileged(new PrivilegedAction<Object>() {
public Object run() {
return cx.getApplicationClassLoader();
}
});
final CodeSource codeSource = (CodeSource) securityDomain;
Map<ClassLoader, SoftReference<SecureCaller>> classLoaderMap;
synchronized (callers) {
classLoaderMap = callers.get(codeSource);
if (classLoaderMap == null) {
classLoaderMap = new WeakHashMap<ClassLoader, SoftReference<SecureCaller>>();
callers.put(codeSource, classLoaderMap);
}
}
SecureCaller caller;
synchronized (classLoaderMap) {
SoftReference<SecureCaller> ref = classLoaderMap.get(classLoader);
if (ref != null) {
caller = ref.get();
} else {
caller = null;
}
if (caller == null) {
try {
// Run in doPrivileged as we'll be checked for
// "createClassLoader" runtime permission
caller = (SecureCaller) AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
public Object run() throws Exception {
Loader loader = new Loader(classLoader, codeSource);
Class<?> c = loader.defineClass(SecureCaller.class.getName() + "Impl", secureCallerImplBytecode);
return c.newInstance();
}
});
classLoaderMap.put(classLoader, new SoftReference<SecureCaller>(caller));
} catch (PrivilegedActionException ex) {
throw new UndeclaredThrowableException(ex.getCause());
}
}
}
return caller.call(callable, cx, scope, thisObj, args);
}
use of java.lang.ref.SoftReference in project hackpad by dropbox.
the class SecureCaller method callSecurely.
/**
* Call the specified callable using a protection domain belonging to the
* specified code source.
*/
static Object callSecurely(final CodeSource codeSource, Callable callable, Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
final Thread thread = Thread.currentThread();
// Run in doPrivileged as we might be checked for "getClassLoader"
// runtime permission
final ClassLoader classLoader = (ClassLoader) AccessController.doPrivileged(new PrivilegedAction<Object>() {
public Object run() {
return thread.getContextClassLoader();
}
});
Map<ClassLoader, SoftReference<SecureCaller>> classLoaderMap;
synchronized (callers) {
classLoaderMap = callers.get(codeSource);
if (classLoaderMap == null) {
classLoaderMap = new WeakHashMap<ClassLoader, SoftReference<SecureCaller>>();
callers.put(codeSource, classLoaderMap);
}
}
SecureCaller caller;
synchronized (classLoaderMap) {
SoftReference<SecureCaller> ref = classLoaderMap.get(classLoader);
if (ref != null) {
caller = ref.get();
} else {
caller = null;
}
if (caller == null) {
try {
// Run in doPrivileged as we'll be checked for
// "createClassLoader" runtime permission
caller = (SecureCaller) AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
public Object run() throws Exception {
ClassLoader effectiveClassLoader;
Class<?> thisClass = getClass();
if (classLoader.loadClass(thisClass.getName()) != thisClass) {
effectiveClassLoader = thisClass.getClassLoader();
} else {
effectiveClassLoader = classLoader;
}
SecureClassLoaderImpl secCl = new SecureClassLoaderImpl(effectiveClassLoader);
Class<?> c = secCl.defineAndLinkClass(SecureCaller.class.getName() + "Impl", secureCallerImplBytecode, codeSource);
return c.newInstance();
}
});
classLoaderMap.put(classLoader, new SoftReference<SecureCaller>(caller));
} catch (PrivilegedActionException ex) {
throw new UndeclaredThrowableException(ex.getCause());
}
}
}
return caller.call(callable, cx, scope, thisObj, args);
}
use of java.lang.ref.SoftReference in project android-app by eoecn.
the class ImageUtil method loadThumbnailImage.
/**
* 从本地或者服务端异步加载缩略图图片
*
* @return
* @param imagePath
* 本地缓存路径
* @param imgUrl
* 拼接后的请求路径
* @param callback
* 得到数据后的处理方法回调
* @throws IOException
*/
public static Bitmap loadThumbnailImage(final String imagePath, final String imgUrl, final DBHelper dbHelper, final ImageCallback callback, final boolean b) {
// 在软链接缓存中,则返回Bitmap对象
if (imageCache.containsKey(imgUrl)) {
SoftReference reference = imageCache.get(imgUrl);
Bitmap bitmap = (Bitmap) reference.get();
if (bitmap != null) {
return bitmap;
}
}
// 若软链接缓存没有
Bitmap bitmap = null;
// 查询数据库 返回bitmap
// 从本地加载
bitmap = getImageFromDB(imagePath, imgUrl, dbHelper);
if (bitmap != null) {
return bitmap;
} else {
// 从网上加载
final Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.obj != null) {
Bitmap bitmap = (Bitmap) msg.obj;
callback.loadImage(bitmap, imagePath);
}
}
};
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
URL url = new URL(imgUrl);
URLConnection conn = url.openConnection();
conn.setConnectTimeout(5000);
conn.setReadTimeout(5000);
conn.connect();
InputStream in = conn.getInputStream();
BitmapFactory.Options options = new Options();
options.inSampleSize = 1;
Bitmap bitmap = BitmapFactory.decodeStream(in, new Rect(0, 0, 0, 0), options);
imageCache.put(imgUrl, new SoftReference(bitmap));
Message msg = handler.obtainMessage();
msg.obj = bitmap;
handler.sendMessage(msg);
if (bitmap != null) {
// 保存文件到sd卡
saveImage(imagePath, bitmap);
// 保存到数据库
saveImageByDb(imgUrl, dbHelper);
}
} catch (MalformedURLException e) {
e.printStackTrace();
Log.e(ImageUtil.class.getName(), "图片url不存在");
} catch (IOException e) {
e.printStackTrace();
}
}
};
ThreadPoolManager.getInstance().addTask(runnable);
}
return null;
}
use of java.lang.ref.SoftReference in project pinot by linkedin.
the class BitmapInvertedIndexReader method getImmutable.
/**
* {@inheritDoc}
* @see InvertedIndexReader#getImmutable(int)
*/
@Override
public ImmutableRoaringBitmap getImmutable(int idx) {
SoftReference<ImmutableRoaringBitmap>[] bitmapArrayReference = null;
// Return the bitmap if it's still on heap
if (bitmaps != null) {
bitmapArrayReference = bitmaps.get();
if (bitmapArrayReference != null) {
SoftReference<ImmutableRoaringBitmap> bitmapReference = bitmapArrayReference[idx];
if (bitmapReference != null) {
ImmutableRoaringBitmap value = bitmapReference.get();
if (value != null) {
return value;
}
}
} else {
bitmapArrayReference = new SoftReference[numberOfBitmaps];
bitmaps = new SoftReference<SoftReference<ImmutableRoaringBitmap>[]>(bitmapArrayReference);
}
} else {
bitmapArrayReference = new SoftReference[numberOfBitmaps];
bitmaps = new SoftReference<SoftReference<ImmutableRoaringBitmap>[]>(bitmapArrayReference);
}
synchronized (this) {
ImmutableRoaringBitmap value;
if (bitmapArrayReference[idx] == null || bitmapArrayReference[idx].get() == null) {
value = buildRoaringBitmapForIndex(idx);
bitmapArrayReference[idx] = new SoftReference<ImmutableRoaringBitmap>(value);
} else {
value = bitmapArrayReference[idx].get();
}
return value;
}
}
use of java.lang.ref.SoftReference in project StickerCamera by Skykai521.
the class ImageUtils method byteToBitmap.
public static Bitmap byteToBitmap(byte[] imgByte) {
InputStream input = null;
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;
}
Aggregations