diff options
author | Scott Jackson <daneren2005@gmail.com> | 2013-09-13 09:16:33 -0700 |
---|---|---|
committer | Scott Jackson <daneren2005@gmail.com> | 2013-09-13 09:16:33 -0700 |
commit | 84c0783d481081e3cde703c91ef9884714692fd3 (patch) | |
tree | 6750026e70308e58e0eb25c526d6ec548a18dccd /src/github/daneren2005 | |
parent | 3c4e9ce398aca937fe68c73a7eed2129db759efb (diff) | |
download | dsub-84c0783d481081e3cde703c91ef9884714692fd3.tar.gz dsub-84c0783d481081e3cde703c91ef9884714692fd3.tar.bz2 dsub-84c0783d481081e3cde703c91ef9884714692fd3.zip |
Switch to using Kryo lib, dramatically faster object serialization
Diffstat (limited to 'src/github/daneren2005')
6 files changed, 50 insertions, 33 deletions
diff --git a/src/github/daneren2005/dsub/audiofx/EqualizerController.java b/src/github/daneren2005/dsub/audiofx/EqualizerController.java index 039394bf..0e1c49b5 100644 --- a/src/github/daneren2005/dsub/audiofx/EqualizerController.java +++ b/src/github/daneren2005/dsub/audiofx/EqualizerController.java @@ -80,7 +80,7 @@ public class EqualizerController { public void loadSettings() { try { if (isAvailable()) { - EqualizerSettings settings = FileUtil.deserialize(context, "equalizer.dat"); + EqualizerSettings settings = FileUtil.deserialize(context, "equalizer.dat", EqualizerSettings.class); if (settings != null) { settings.apply(equalizer); } @@ -124,10 +124,13 @@ public class EqualizerController { private static class EqualizerSettings implements Serializable { - private final short[] bandLevels; + private short[] bandLevels; private short preset; - private final boolean enabled; + private boolean enabled; + public EqualizerSettings() { + + } public EqualizerSettings(Equalizer equalizer) { enabled = equalizer.getEnabled(); bandLevels = new short[equalizer.getNumberOfBands()]; diff --git a/src/github/daneren2005/dsub/domain/Indexes.java b/src/github/daneren2005/dsub/domain/Indexes.java index 0bc44158..671821a5 100644 --- a/src/github/daneren2005/dsub/domain/Indexes.java +++ b/src/github/daneren2005/dsub/domain/Indexes.java @@ -26,10 +26,13 @@ import java.io.Serializable; */ public class Indexes implements Serializable { - private final long lastModified; - private final List<Artist> shortcuts; - private final List<Artist> artists; + private long lastModified; + private List<Artist> shortcuts; + private List<Artist> artists; + public Indexes() { + + } public Indexes(long lastModified, List<Artist> shortcuts, List<Artist> artists) { this.lastModified = lastModified; this.shortcuts = shortcuts; diff --git a/src/github/daneren2005/dsub/domain/MusicFolder.java b/src/github/daneren2005/dsub/domain/MusicFolder.java index 68a22bcc..99e86e23 100644 --- a/src/github/daneren2005/dsub/domain/MusicFolder.java +++ b/src/github/daneren2005/dsub/domain/MusicFolder.java @@ -28,9 +28,12 @@ import java.io.Serializable; */ public class MusicFolder implements Serializable { - private final String id; - private final String name; + private String id; + private String name; + public MusicFolder() { + + } public MusicFolder(String id, String name) { this.id = id; this.name = name; diff --git a/src/github/daneren2005/dsub/service/DownloadServiceLifecycleSupport.java b/src/github/daneren2005/dsub/service/DownloadServiceLifecycleSupport.java index 4e3ec29c..dbdd830d 100644 --- a/src/github/daneren2005/dsub/service/DownloadServiceLifecycleSupport.java +++ b/src/github/daneren2005/dsub/service/DownloadServiceLifecycleSupport.java @@ -257,15 +257,12 @@ public class DownloadServiceLifecycleSupport { } private void deserializeDownloadQueueNow() { - State state = FileUtil.deserialize(downloadService, FILENAME_DOWNLOADS_SER); + State state = FileUtil.deserialize(downloadService, FILENAME_DOWNLOADS_SER, State.class); if (state == null) { return; } Log.i(TAG, "Deserialized currentPlayingIndex: " + state.currentPlayingIndex + ", currentPlayingPosition: " + state.currentPlayingPosition); downloadService.restore(state.songs, state.currentPlayingIndex, state.currentPlayingPosition); - - // Work-around: Serialize again, as the restore() method creates a serialization without current playing info. - serializeDownloadQueue(); } private void handleKeyEvent(KeyEvent event) { diff --git a/src/github/daneren2005/dsub/service/RESTMusicService.java b/src/github/daneren2005/dsub/service/RESTMusicService.java index 82148dc0..947f89e2 100644 --- a/src/github/daneren2005/dsub/service/RESTMusicService.java +++ b/src/github/daneren2005/dsub/service/RESTMusicService.java @@ -243,7 +243,7 @@ public class RESTMusicService implements MusicService { private Indexes readCachedIndexes(Context context, String musicFolderId) { String filename = getCachedIndexesFilename(context, musicFolderId); - return FileUtil.deserialize(context, filename); + return FileUtil.deserialize(context, filename, Indexes.class); } private void writeCachedIndexes(Context context, Indexes indexes, String musicFolderId) { @@ -258,7 +258,7 @@ public class RESTMusicService implements MusicService { private ArrayList<MusicFolder> readCachedMusicFolders(Context context) { String filename = getCachedMusicFoldersFilename(context); - return FileUtil.deserialize(context, filename); + return FileUtil.deserialize(context, filename, ArrayList.class); } private void writeCachedMusicFolders(Context context, List<MusicFolder> musicFolders) { diff --git a/src/github/daneren2005/dsub/util/FileUtil.java b/src/github/daneren2005/dsub/util/FileUtil.java index 34bc82bd..3f699524 100644 --- a/src/github/daneren2005/dsub/util/FileUtil.java +++ b/src/github/daneren2005/dsub/util/FileUtil.java @@ -21,8 +21,7 @@ package github.daneren2005.dsub.util; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; +import java.io.RandomAccessFile; import java.io.Serializable; import java.util.Arrays; import java.util.SortedSet; @@ -36,10 +35,16 @@ import android.graphics.BitmapFactory; import android.os.Environment; import android.util.Log; import github.daneren2005.dsub.domain.Artist; +import github.daneren2005.dsub.domain.Indexes; import github.daneren2005.dsub.domain.MusicDirectory; +import github.daneren2005.dsub.domain.MusicFolder; import github.daneren2005.dsub.domain.PodcastChannel; import github.daneren2005.dsub.domain.PodcastEpisode; +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; + /** * @author Sindre Mehus */ @@ -52,6 +57,14 @@ public class FileUtil { private static final List<String> VIDEO_FILE_EXTENSIONS = Arrays.asList("flv", "mp4", "m4v", "wmv", "avi", "mov", "mpg", "mkv"); private static final List<String> PLAYLIST_FILE_EXTENSIONS = Arrays.asList("m3u"); private static final File DEFAULT_MUSIC_DIR = createDirectory("music"); + private static final Kryo kryo = new Kryo(); + + static { + kryo.register(MusicDirectory.Entry.class); + kryo.register(Indexes.class); + kryo.register(Artist.class); + kryo.register(MusicFolder.class); + } public static File getAnySong(Context context) { File dir = getMusicDirectory(context); @@ -359,35 +372,33 @@ public class FileUtil { } public static <T extends Serializable> boolean serialize(Context context, T obj, String fileName) { - File file = new File(context.getCacheDir(), fileName); - ObjectOutputStream out = null; + Output out = null; try { - out = new ObjectOutputStream(new FileOutputStream(file)); - out.writeObject(obj); - Log.i(TAG, "Serialized object to " + file); + RandomAccessFile file = new RandomAccessFile(context.getCacheDir() + "/" + fileName, "rw"); + out = new Output(new FileOutputStream(file.getFD())); + kryo.writeObject(out, obj); + Log.i(TAG, "Serialized object to " + fileName); + file.close(); return true; } catch (Throwable x) { - Log.w(TAG, "Failed to serialize object to " + file); + Log.w(TAG, "Failed to serialize object to " + fileName); return false; } finally { Util.close(out); } } - public static <T extends Serializable> T deserialize(Context context, String fileName) { - File file = new File(context.getCacheDir(), fileName); - if (!file.exists() || !file.isFile()) { - return null; - } - - ObjectInputStream in = null; + public static <T extends Serializable> T deserialize(Context context, String fileName, Class<T> tClass) { + Input in = null; try { - in = new ObjectInputStream(new FileInputStream(file)); - T result = (T) in.readObject(); - Log.i(TAG, "Deserialized object from " + file); + RandomAccessFile file = new RandomAccessFile(context.getCacheDir() + "/" + fileName, "r"); + in = new Input(new FileInputStream(file.getFD())); + T result = (T) kryo.readObject(in, tClass); + Log.i(TAG, "Deserialized object from " + fileName); + file.close(); return result; } catch (Throwable x) { - Log.w(TAG, "Failed to deserialize object from " + file, x); + Log.w(TAG, "Failed to deserialize object from " + fileName, x); return null; } finally { Util.close(in); |