private InputStream prepareInputStream(final InputStream is) throws IOException { InputStream src = new FlushedInputStream(is); if (!src.markSupported()) { src = manager.getBuffersPool().bufferize(src, IMAGES_BUFFER_SIZE); } return src; }
void writeBitmapToDisk(final Bitmap bitmap) throws IOException { EnhancedResponseCache cache = (EnhancedResponseCache) manager.getImagesResponseCache(); OutputStream output = new FileOutputStream(cache.getLocalPath(url)); output = manager.getBuffersPool().bufferize(output, IMAGES_BUFFER_SIZE); try { final int quality = 100; bitmap.compress(Bitmap.CompressFormat.PNG, quality, output); } finally { IoUtils.closeQuietly(output); } }
/** * Store image to the disk cache. If max allowed size is set image may be rescaled on disk. * * @throws IOException if error happens */ public void storeToDisk() throws IOException { if (manager.isPresentOnDisk(url)) { return; } if (!hasAllowedSize()) { IoUtils.consumeStream(getRemoteInputStream(), manager.getBuffersPool()); return; } ImageResult result = decodeStream(getRemoteInputStream(), true); if (result.getType() == ImageSourceType.NETWORK && result.getBitmap() != null) { // image was scaled writeBitmapToDisk(result.getBitmap()); } }
private ImageResult decodeStream(final InputStream is, boolean onlyIfNeedsRescale) throws IOException { final BitmapFactory.Options options = createBitmapOptions(); final InputStream src = prepareInputStream(is); try { ImageResult result = new ImageResult(); result.setType(manager.isPresentOnDisk(url) ? ImageSourceType.DISK : ImageSourceType.NETWORK); // get scale factor options.inSampleSize = resolveSampleFactor(src, options); if (options.inSampleSize > 1 || !onlyIfNeedsRescale) { // actually decode result.setBitmap(doStreamDecode(src, options)); } else { // consume input in order to cache it IoUtils.consumeStream(src, manager.getBuffersPool()); } if (manager.debug) { Log.d(TAG, "Image decoded: " + result); } return result; } catch (final OutOfMemoryError e) { // wrap OOM in IO final IOException wrapped = new IOException("out of memory for " + getKey()); wrapped.initCause(e); throw wrapped; } finally { recycle(options); src.close(); } }
private void recycle(final BitmapFactory.Options options) { manager.getBuffersPool().release(options.inTempStorage); }
private BitmapFactory.Options createBitmapOptions() { BitmapFactory.Options options = new BitmapFactory.Options(); options.inTempStorage = manager.getBuffersPool().get(IMAGES_BUFFER_SIZE); options.inPreferredConfig = format; return options; }