Skip to content
This repository was archived by the owner on Jan 14, 2018. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
package com.octo.android.robospice.persistence.springandroid;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.CharEncoding;
import org.apache.commons.lang3.StringUtils;

import roboguice.util.temp.Ln;
import android.app.Application;

import com.octo.android.robospice.persistence.exception.CacheCreationException;
import com.octo.android.robospice.persistence.exception.CacheLoadingException;
import com.octo.android.robospice.persistence.exception.CacheSavingException;
import com.octo.android.robospice.persistence.file.InFileObjectPersister;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.CharEncoding;
import org.apache.commons.lang3.StringUtils;
import roboguice.util.temp.Ln;

import java.io.File;
import java.io.IOException;

public abstract class SpringAndroidObjectPersister<T> extends InFileObjectPersister<T> {

Expand All @@ -35,26 +32,23 @@ public SpringAndroidObjectPersister(Application application, Class<T> clazz) thr

@Override
protected T readCacheDataFromFile(File file) throws CacheLoadingException {
try {
String resultJson = null;
synchronized (file.getAbsolutePath().intern()) {
String resultJson = null;
synchronized (file.getAbsolutePath().intern()) {
try {
resultJson = FileUtils.readFileToString(file, CharEncoding.UTF_8);
} catch (IOException e) {
throw new CacheLoadingException(e);
}
}
try {
if (!StringUtils.isEmpty(resultJson)) {
T result = deserializeData(resultJson);
return result;
}
throw new CacheLoadingException("Unable to restore cache content : cache file is empty");
} catch (FileNotFoundException e) {
// Should not occur (we test before if file exists)
// Do not throw, file is not cached
Ln.w("file " + file.getAbsolutePath() + " does not exists", e);
return null;
} catch (CacheLoadingException e) {
throw e;
} catch (Exception e) {
throw new CacheLoadingException(e);
}
throw new CacheLoadingException("Unable to restore cache content : cache file is empty");
}

protected abstract T deserializeData(String json) throws CacheLoadingException;
Expand Down