diff options
author | Scott Jackson <daneren2005@gmail.com> | 2013-12-15 22:23:23 -0800 |
---|---|---|
committer | Scott Jackson <daneren2005@gmail.com> | 2013-12-15 22:23:23 -0800 |
commit | 0538eb437649b80392badddb6105e8a5e360152e (patch) | |
tree | 64db5591e4b6bdf7d609d4cdee1a0eedc0226b9f /src/github | |
parent | 64ae4373dc702713dfb8bff568f7db9c896ebc77 (diff) | |
download | dsub-0538eb437649b80392badddb6105e8a5e360152e.tar.gz dsub-0538eb437649b80392badddb6105e8a5e360152e.tar.bz2 dsub-0538eb437649b80392badddb6105e8a5e360152e.zip |
#169 Synchronize use of kryo
Diffstat (limited to 'src/github')
-rw-r--r-- | src/github/daneren2005/dsub/util/FileUtil.java | 66 |
1 files changed, 35 insertions, 31 deletions
diff --git a/src/github/daneren2005/dsub/util/FileUtil.java b/src/github/daneren2005/dsub/util/FileUtil.java index d18bddb8..2c717416 100644 --- a/src/github/daneren2005/dsub/util/FileUtil.java +++ b/src/github/daneren2005/dsub/util/FileUtil.java @@ -379,39 +379,43 @@ public class FileUtil { } public static <T extends Serializable> boolean serialize(Context context, T obj, String fileName) { - Output out = null; - try { - 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); - return true; - } catch (Throwable x) { - Log.w(TAG, "Failed to serialize object to " + fileName); - return false; - } finally { - Util.close(out); - } + synchronized (kryo) { + Output out = null; + try { + 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); + return true; + } catch (Throwable x) { + 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, Class<T> tClass) { - Input in = null; - try { - 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); - return result; - } catch(FileNotFoundException e) { - // Different error message - Log.w(TAG, "No serialization for object from " + fileName); - return null; - } catch (Throwable x) { - Log.w(TAG, "Failed to deserialize object from " + fileName, x); - return null; - } finally { - Util.close(in); - } + synchronized (kryo) { + Input in = null; + try { + 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); + return result; + } catch(FileNotFoundException e) { + // Different error message + Log.w(TAG, "No serialization for object from " + fileName); + return null; + } catch (Throwable x) { + Log.w(TAG, "Failed to deserialize object from " + fileName, x); + return null; + } finally { + Util.close(in); + } + } } } |