@Override public void run() { try { FileUtils.writeByteArrayToFile(getCacheFile(cacheKey), byteArray); } catch (IOException e) { Ln.e(e, "An error occured on saving request " + cacheKey + " data asynchronously"); } }; };
@Override public InputStream saveDataToCacheAndReturnData(InputStream data, Object cacheKey) throws CacheSavingException { FileOutputStream output = null; // special case for big inputstream object : as it can be read // only once and is too big to be locally // duplicated, // 1) we save it in file // 2) we load and return it from the file try { output = new FileOutputStream(getCacheFile(cacheKey)); IOUtils.copy(data, output); return new FileInputStream(getCacheFile(cacheKey)); } catch (IOException e) { throw new CacheSavingException(e); } finally { IOUtils.closeQuietly(output); } }
@Override public InputStream saveDataToCacheAndReturnData(InputStream data, final Object cacheKey) throws CacheSavingException { // special case for inputstream object : as it can be read only // once, // 0) we extract the content of the input stream as a byte[] // 1) we save it in file asynchronously if enabled // 2) the result will be a new InputStream on the byte[] final byte[] byteArray; try { byteArray = IOUtils.toByteArray(data); if (isAsyncSaveEnabled()) { Thread t = new Thread() { @Override public void run() { try { FileUtils.writeByteArrayToFile(getCacheFile(cacheKey), byteArray); } catch (IOException e) { Ln.e(e, "An error occured on saving request " + cacheKey + " data asynchronously"); } }; }; t.start(); } else { FileUtils.writeByteArrayToFile(getCacheFile(cacheKey), byteArray); } return new ByteArrayInputStream(byteArray); } catch (IOException e) { throw new CacheSavingException(e); } }
@Override public CacheManager createCacheManager(Application application) { CacheManager cacheManager = new CacheManager(); // init InFileStringObjectPersister inFileStringObjectPersister = new InFileStringObjectPersister(application); InFileInputStreamObjectPersister inFileInputStreamObjectPersister = new InFileInputStreamObjectPersister(application); JacksonObjectPersisterFactory inJSonFileObjectPersisterFactory = new JacksonObjectPersisterFactory(application); inFileStringObjectPersister.setAsyncSaveEnabled(true); inFileInputStreamObjectPersister.setAsyncSaveEnabled(true); inJSonFileObjectPersisterFactory.setAsyncSaveEnabled(true); cacheManager.addPersister(inFileStringObjectPersister); cacheManager.addPersister(inFileInputStreamObjectPersister); cacheManager.addPersister(inJSonFileObjectPersisterFactory); return cacheManager; }
@Override public Bitmap saveDataToCacheAndReturnData(Bitmap data, Object cacheKey) throws CacheSavingException { BufferedOutputStream out = null; try { File cacheFile = getCacheFile(cacheKey); out = new BufferedOutputStream(new FileOutputStream(cacheFile)); boolean didCompress = data.compress(compressFormat, quality, out); if (!didCompress) { throw new CacheSavingException(String.format("Could not compress bitmap for path: %s", getCacheFile(cacheKey).getAbsolutePath())); } return data; } catch (IOException e) { throw new CacheSavingException(e); } finally { IOUtils.closeQuietly(out); } }