diff options
author | Scott Jackson <daneren2005@gmail.com> | 2013-12-23 23:04:11 -0800 |
---|---|---|
committer | Scott Jackson <daneren2005@gmail.com> | 2013-12-23 23:04:11 -0800 |
commit | adb44cd78d63d4ee18e2d963e601e413d777bf5a (patch) | |
tree | 8602affff3c2583d64923d68a1a392d9ceb26a60 /src | |
parent | a68fdd6d6588c2c094b2a8e48badb12cbd3b497a (diff) | |
download | dsub-adb44cd78d63d4ee18e2d963e601e413d777bf5a.tar.gz dsub-adb44cd78d63d4ee18e2d963e601e413d777bf5a.tar.bz2 dsub-adb44cd78d63d4ee18e2d963e601e413d777bf5a.zip |
Convert starred sync to compress serialization since saved paths can be large
Diffstat (limited to 'src')
-rw-r--r-- | src/github/daneren2005/dsub/service/sync/StarredSyncAdapter.java | 2 | ||||
-rw-r--r-- | src/github/daneren2005/dsub/util/FileUtil.java | 47 | ||||
-rw-r--r-- | src/github/daneren2005/dsub/util/SyncUtil.java | 5 |
3 files changed, 52 insertions, 2 deletions
diff --git a/src/github/daneren2005/dsub/service/sync/StarredSyncAdapter.java b/src/github/daneren2005/dsub/service/sync/StarredSyncAdapter.java index ff153e33..a363a470 100644 --- a/src/github/daneren2005/dsub/service/sync/StarredSyncAdapter.java +++ b/src/github/daneren2005/dsub/service/sync/StarredSyncAdapter.java @@ -75,7 +75,7 @@ public class StarredSyncAdapter extends SubsonicSyncAdapter { }
}
- FileUtil.serialize(context, syncedList, SyncUtil.getStarredSyncFile(context, instance));
+ SyncUtil.setSyncedStarred(syncedList, context, instance);
if(updated) {
SyncUtil.showSyncNotification(context, R.string.sync_new_starred, null);
}
diff --git a/src/github/daneren2005/dsub/util/FileUtil.java b/src/github/daneren2005/dsub/util/FileUtil.java index 852fc2ab..3adf3ad4 100644 --- a/src/github/daneren2005/dsub/util/FileUtil.java +++ b/src/github/daneren2005/dsub/util/FileUtil.java @@ -30,10 +30,15 @@ import java.util.SortedSet; import java.util.TreeSet; import java.util.Iterator; import java.util.List; +import java.util.zip.DeflaterInputStream; +import java.util.zip.DeflaterOutputStream; +import java.util.zip.InflaterInputStream; +import android.annotation.TargetApi; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; +import android.os.Build; import android.os.Environment; import android.util.Log; import github.daneren2005.dsub.domain.Artist; @@ -426,4 +431,46 @@ public class FileUtil { } } } + + public static <T extends Serializable> boolean serializeCompressed(Context context, T obj, String fileName) { + synchronized (kryo) { + Output out = null; + try { + RandomAccessFile file = new RandomAccessFile(context.getCacheDir() + "/" + fileName, "rw"); + out = new Output(new DeflaterOutputStream(new FileOutputStream(file.getFD()))); + kryo.writeObject(out, obj); + Log.i(TAG, "Serialized compressed object to " + fileName); + return true; + } catch (Throwable x) { + Log.w(TAG, "Failed to serialize compressed object to " + fileName); + return false; + } finally { + Util.close(out); + } + } + } + + @TargetApi(Build.VERSION_CODES.GINGERBREAD) + public static <T extends Serializable> T deserializeCompressed(Context context, String fileName, Class<T> tClass) { + synchronized (kryo) { + Input in = null; + try { + RandomAccessFile file = new RandomAccessFile(context.getCacheDir() + "/" + fileName, "r"); + + in = new Input(new InflaterInputStream(new FileInputStream(file.getFD()))); + T result = (T) kryo.readObject(in, tClass); + Log.i(TAG, "Deserialized compressed object from " + fileName); + return result; + } catch(FileNotFoundException e) { + // Different error message + Log.w(TAG, "No serialization compressed for object from " + fileName); + return null; + } catch (Throwable x) { + Log.w(TAG, "Failed to deserialize compressed object from " + fileName, x); + return null; + } finally { + Util.close(in); + } + } + } } diff --git a/src/github/daneren2005/dsub/util/SyncUtil.java b/src/github/daneren2005/dsub/util/SyncUtil.java index 09e87da9..21aaaccc 100644 --- a/src/github/daneren2005/dsub/util/SyncUtil.java +++ b/src/github/daneren2005/dsub/util/SyncUtil.java @@ -119,12 +119,15 @@ public final class SyncUtil { // Starred
public static ArrayList<String> getSyncedStarred(Context context, int instance) {
- ArrayList<String> list = FileUtil.deserialize(context, getStarredSyncFile(context, instance), ArrayList.class);
+ ArrayList<String> list = FileUtil.deserializeCompressed(context, getStarredSyncFile(context, instance), ArrayList.class);
if(list == null) {
list = new ArrayList<String>();
}
return list;
}
+ public static void setSyncedStarred(ArrayList<String> syncedList, Context context, int instance) {
+ FileUtil.serializeCompressed(context, syncedList, SyncUtil.getStarredSyncFile(context, instance));
+ }
public static String getStarredSyncFile(Context context, int instance) {
return "sync-starred-" + (Util.getRestUrl(context, null, instance)).hashCode() + ".ser";
}
|