@Override protected void saveData(T data, Object cacheKey) throws IOException, CacheSavingException { String resultJson; // transform the content in json to store it in the cache resultJson = mJsonMapper.writeValueAsString(data); // finally store the json in the cache if (!StringUtils.isEmpty(resultJson)) { FileUtils.writeStringToFile(getCacheFile(cacheKey), resultJson, CharEncoding.UTF_8); } else { throw new CacheSavingException("Data was null and could not be serialized in json"); } }
@Override protected void saveData(T data, Object cacheKey) throws IOException, CacheSavingException { String resultJson; // transform the content in json to store it in the cache resultJson = gson.toJson(data); // finally store the json in the cache if (!StringUtils.isEmpty(resultJson)) { FileUtils.writeStringToFile(getCacheFile(cacheKey), resultJson, CharEncoding.UTF_8); } else { throw new CacheSavingException("Data was null and could not be serialized in json"); } }
@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); } }
@Override protected void saveData(T data, Object cacheKey) throws IOException, CacheSavingException { String resultJson; // transform the content in json to store it in the cache resultJson = mJsonMapper.writeValueAsString(data); synchronized (getCacheFile(cacheKey).getAbsolutePath().intern()) { // finally store the json in the cache if (!StringUtils.isEmpty(resultJson)) { FileUtils.writeStringToFile(getCacheFile(cacheKey), resultJson, CharEncoding.UTF_8); } else { throw new CacheSavingException("Data was null and could not be serialized in json"); } } }
@Override protected void saveData(T data, Object cacheKey) throws IOException, CacheSavingException { try { serializer.write(data, getCacheFile(cacheKey)); } catch (Exception e) { throw new CacheSavingException("Data was null and could not be serialized in xml"); } } }
@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 T saveDataToCacheAndReturnData(final T data, final Object cacheKey) throws CacheSavingException { try { if (isAsyncSaveEnabled()) { Thread t = new Thread() { @Override public void run() { try { saveData(data, cacheKey); } catch (IOException e) { Ln.e(e, "An error occured on saving request " + cacheKey + " data asynchronously"); } catch (CacheSavingException e) { Ln.e(e, "An error occured on saving request " + cacheKey + " data asynchronously"); } }; }; t.start(); } else { saveData(data, cacheKey); } } catch (CacheSavingException e) { throw e; } catch (Exception e) { throw new CacheSavingException(e); } return data; }
@Override public T saveDataToCacheAndReturnData(final T data, final Object cacheKey) throws CacheSavingException { try { if (isAsyncSaveEnabled()) { Thread t = new Thread() { @Override public void run() { try { saveData(data, cacheKey); } catch (IOException e) { Ln.e(e, "An error occured on saving request " + cacheKey + " data asynchronously"); } catch (CacheSavingException e) { Ln.e(e, "An error occured on saving request " + cacheKey + " data asynchronously"); } }; }; t.start(); } else { saveData(data, cacheKey); } } catch (CacheSavingException e) { throw e; } catch (Exception e) { throw new CacheSavingException(e); } return data; }
@Override public T saveDataToCacheAndReturnData(final T data, final Object cacheKey) throws CacheSavingException { try { if (isAsyncSaveEnabled()) { Thread t = new Thread() { @Override public void run() { try { saveData(data, cacheKey); } catch (IOException e) { Ln.e(e, "An error occured on saving request " + cacheKey + " data asynchronously"); } catch (CacheSavingException e) { Ln.e(e, "An error occured on saving request " + cacheKey + " data asynchronously"); } }; }; t.start(); } else { saveData(data, cacheKey); } } catch (CacheSavingException e) { throw e; } catch (Exception e) { throw new CacheSavingException(e); } return data; }
@Override public String saveDataToCacheAndReturnData(final String data, final Object cacheKey) throws CacheSavingException { Ln.v("Saving String " + data + " into cacheKey = " + cacheKey); try { if (isAsyncSaveEnabled()) { Thread t = new Thread() { @Override public void run() { try { FileUtils.writeStringToFile(getCacheFile(cacheKey), data, CharEncoding.UTF_8); } catch (IOException e) { Ln.e(e, "An error occured on saving request " + cacheKey + " data asynchronously"); } }; }; t.start(); } else { FileUtils.writeStringToFile(getCacheFile(cacheKey), data, CharEncoding.UTF_8); } } catch (Exception e) { throw new CacheSavingException(e); } return data; } }