From f86d4434243f5a5526d96abafcb1615b661eabb0 Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Mon, 20 Apr 2015 17:19:34 -0700 Subject: Put the synchronization further into serialization process to avoid freezes --- src/github/daneren2005/dsub/util/FileUtil.java | 126 ++++++++++++------------- 1 file changed, 63 insertions(+), 63 deletions(-) (limited to 'src') diff --git a/src/github/daneren2005/dsub/util/FileUtil.java b/src/github/daneren2005/dsub/util/FileUtil.java index b9d7fd1b..2903960b 100644 --- a/src/github/daneren2005/dsub/util/FileUtil.java +++ b/src/github/daneren2005/dsub/util/FileUtil.java @@ -751,19 +751,19 @@ public class FileUtil { } public static boolean serialize(Context context, T obj, String fileName) { - synchronized (kryo) { - Output out = null; - try { - RandomAccessFile file = new RandomAccessFile(context.getCacheDir() + "/" + fileName, "rw"); - out = new Output(new FileOutputStream(file.getFD())); + Output out = null; + try { + RandomAccessFile file = new RandomAccessFile(context.getCacheDir() + "/" + fileName, "rw"); + out = new Output(new FileOutputStream(file.getFD())); + synchronized (kryo) { kryo.writeObject(out, obj); - return true; - } catch (Throwable x) { - Log.w(TAG, "Failed to serialize object to " + fileName); - return false; - } finally { - Util.close(out); } + return true; + } catch (Throwable x) { + Log.w(TAG, "Failed to serialize object to " + fileName); + return false; + } finally { + Util.close(out); } } @@ -772,78 +772,78 @@ public class FileUtil { } public static T deserialize(Context context, String fileName, Class tClass, int hoursOld) { - synchronized (kryo) { - Input in = null; - try { - File file = new File(context.getCacheDir(), fileName); - if(!file.exists()) { - return null; - } + Input in = null; + try { + File file = new File(context.getCacheDir(), fileName); + if(!file.exists()) { + return null; + } - if(hoursOld != 0) { - Date fileDate = new Date(file.lastModified()); - // Convert into hours - long age = (new Date().getTime() - fileDate.getTime()) / 1000 / 3600; - if(age > hoursOld) { - return null; - } + if(hoursOld != 0) { + Date fileDate = new Date(file.lastModified()); + // Convert into hours + long age = (new Date().getTime() - fileDate.getTime()) / 1000 / 3600; + if(age > hoursOld) { + return null; } + } - RandomAccessFile randomFile = new RandomAccessFile(file, "r"); + RandomAccessFile randomFile = new RandomAccessFile(file, "r"); - in = new Input(new FileInputStream(randomFile.getFD())); - T result = (T) kryo.readObject(in, tClass); + in = new Input(new FileInputStream(randomFile.getFD())); + synchronized (kryo) { + T result = kryo.readObject(in, tClass); 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); - return null; - } finally { - Util.close(in); } + } 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); + return null; + } finally { + Util.close(in); } } public static 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()))); + Output out = null; + try { + RandomAccessFile file = new RandomAccessFile(context.getCacheDir() + "/" + fileName, "rw"); + out = new Output(new DeflaterOutputStream(new FileOutputStream(file.getFD()))); + synchronized (kryo) { kryo.writeObject(out, obj); - return true; - } catch (Throwable x) { - Log.w(TAG, "Failed to serialize compressed object to " + fileName); - return false; - } finally { - Util.close(out); } + 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 deserializeCompressed(Context context, String fileName, Class tClass) { - synchronized (kryo) { - Input in = null; - try { - RandomAccessFile file = new RandomAccessFile(context.getCacheDir() + "/" + fileName, "r"); + 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); + in = new Input(new InflaterInputStream(new FileInputStream(file.getFD()))); + synchronized (kryo) { + T result = kryo.readObject(in, tClass); 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); - return null; - } finally { - Util.close(in); } + } 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); + return null; + } finally { + Util.close(in); } } } -- cgit v1.2.3