Search in sources :

Example 31 with SoftReference

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);
}
Also used : PrivilegedActionException(java.security.PrivilegedActionException) SecureClassLoader(java.security.SecureClassLoader) CodeSource(java.security.CodeSource) PrivilegedActionException(java.security.PrivilegedActionException) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) SoftReference(java.lang.ref.SoftReference) PrivilegedAction(java.security.PrivilegedAction) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) SecureClassLoader(java.security.SecureClassLoader)

Example 32 with SoftReference

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);
}
Also used : PrivilegedActionException(java.security.PrivilegedActionException) PrivilegedActionException(java.security.PrivilegedActionException) IOException(java.io.IOException) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) SoftReference(java.lang.ref.SoftReference) PrivilegedAction(java.security.PrivilegedAction) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) SecureClassLoader(java.security.SecureClassLoader)

Example 33 with SoftReference

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;
}
Also used : Options(android.graphics.BitmapFactory.Options) Rect(android.graphics.Rect) MalformedURLException(java.net.MalformedURLException) Message(android.os.Message) Options(android.graphics.BitmapFactory.Options) InputStream(java.io.InputStream) Handler(android.os.Handler) IOException(java.io.IOException) URL(java.net.URL) URLConnection(java.net.URLConnection) Bitmap(android.graphics.Bitmap) SoftReference(java.lang.ref.SoftReference) BitmapFactory(android.graphics.BitmapFactory)

Example 34 with SoftReference

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;
    }
}
Also used : SoftReference(java.lang.ref.SoftReference) ImmutableRoaringBitmap(org.roaringbitmap.buffer.ImmutableRoaringBitmap)

Example 35 with SoftReference

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;
}
Also used : Bitmap(android.graphics.Bitmap) SoftReference(java.lang.ref.SoftReference) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) BitmapFactory(android.graphics.BitmapFactory) IOException(java.io.IOException)

Aggregations

SoftReference (java.lang.ref.SoftReference)53 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