From c66597b085ebe10276ab37ffcc6ff4e106dd69f2 Mon Sep 17 00:00:00 2001 From: Mariotaku Lee Date: Tue, 29 Aug 2017 15:53:32 +0800 Subject: [PATCH] migrating to try-with-resources statements --- .../twidere/model/CustomAPIConfig.java | 7 +- .../twidere/util/DataImportExportUtils.java | 191 +++++++++--------- 2 files changed, 95 insertions(+), 103 deletions(-) diff --git a/twidere/src/main/java/org/mariotaku/twidere/model/CustomAPIConfig.java b/twidere/src/main/java/org/mariotaku/twidere/model/CustomAPIConfig.java index abbc3c00c..bb473fc56 100644 --- a/twidere/src/main/java/org/mariotaku/twidere/model/CustomAPIConfig.java +++ b/twidere/src/main/java/org/mariotaku/twidere/model/CustomAPIConfig.java @@ -12,7 +12,6 @@ import com.bluelinelabs.logansquare.annotation.JsonField; import com.bluelinelabs.logansquare.annotation.JsonObject; import com.hannesdorfmann.parcelableplease.annotation.ParcelablePlease; -import org.mariotaku.restfu.RestFuUtils; import org.mariotaku.twidere.R; import org.mariotaku.twidere.annotation.AccountType; import org.mariotaku.twidere.model.account.cred.Credentials; @@ -236,14 +235,10 @@ public final class CustomAPIConfig implements Parcelable { @NonNull public static List listDefault(@NonNull Context context) { final AssetManager assets = context.getAssets(); - InputStream is = null; - try { - is = assets.open("data/default_api_configs.json"); + try (InputStream is = assets.open("data/default_api_configs.json")) { return JsonSerializer.parseList(is, CustomAPIConfig.class); } catch (IOException e) { return listBuiltin(context); - } finally { - RestFuUtils.closeSilently(is); } } diff --git a/twidere/src/main/java/org/mariotaku/twidere/util/DataImportExportUtils.java b/twidere/src/main/java/org/mariotaku/twidere/util/DataImportExportUtils.java index a769c0934..ff4875ba3 100644 --- a/twidere/src/main/java/org/mariotaku/twidere/util/DataImportExportUtils.java +++ b/twidere/src/main/java/org/mariotaku/twidere/util/DataImportExportUtils.java @@ -37,7 +37,6 @@ import com.fasterxml.jackson.core.JsonToken; import org.mariotaku.library.exportablepreferences.PreferencesExporter; import org.mariotaku.library.exportablepreferences.annotation.PreferenceType; import org.mariotaku.library.objectcursor.ObjectCursor; -import org.mariotaku.restfu.RestFuUtils; import org.mariotaku.twidere.Constants; import org.mariotaku.twidere.constant.SharedPreferenceConstants; import org.mariotaku.twidere.model.FiltersData; @@ -82,23 +81,27 @@ public class DataImportExportUtils implements Constants { @WorkerThread public static void exportData(final Context context, @NonNull final File dst, final int flags) throws IOException { dst.delete(); - final FileOutputStream fos = new FileOutputStream(dst); - final ZipOutputStream zos = new ZipOutputStream(fos); - try { + try (FileOutputStream fos = new FileOutputStream(dst); + ZipOutputStream zos = new ZipOutputStream(fos)) { if (hasFlag(flags, FLAG_PREFERENCES)) { - exportSharedPreferencesData(zos, context, SHARED_PREFERENCES_NAME, ENTRY_PREFERENCES, new PreferencesExporterStrategy(SharedPreferenceConstants.class)); + exportSharedPreferencesData(zos, context, SHARED_PREFERENCES_NAME, ENTRY_PREFERENCES, + new PreferencesExporterStrategy(SharedPreferenceConstants.class)); } if (hasFlag(flags, FLAG_NICKNAMES)) { - exportSharedPreferencesData(zos, context, USER_NICKNAME_PREFERENCES_NAME, ENTRY_NICKNAMES, ConvertToStringProcessStrategy.SINGLETON); + exportSharedPreferencesData(zos, context, USER_NICKNAME_PREFERENCES_NAME, ENTRY_NICKNAMES, + ConvertToStringProcessStrategy.SINGLETON); } if (hasFlag(flags, FLAG_USER_COLORS)) { - exportSharedPreferencesData(zos, context, USER_COLOR_PREFERENCES_NAME, ENTRY_USER_COLORS, ConvertToIntProcessStrategy.SINGLETON); + exportSharedPreferencesData(zos, context, USER_COLOR_PREFERENCES_NAME, ENTRY_USER_COLORS, + ConvertToIntProcessStrategy.SINGLETON); } if (hasFlag(flags, FLAG_HOST_MAPPING)) { - exportSharedPreferencesData(zos, context, HOST_MAPPING_PREFERENCES_NAME, ENTRY_HOST_MAPPING, ConvertToStringProcessStrategy.SINGLETON); + exportSharedPreferencesData(zos, context, HOST_MAPPING_PREFERENCES_NAME, ENTRY_HOST_MAPPING, + ConvertToStringProcessStrategy.SINGLETON); } if (hasFlag(flags, FLAG_KEYBOARD_SHORTCUTS)) { - exportSharedPreferencesData(zos, context, KEYBOARD_SHORTCUTS_PREFERENCES_NAME, ENTRY_KEYBOARD_SHORTCUTS, ConvertToStringProcessStrategy.SINGLETON); + exportSharedPreferencesData(zos, context, KEYBOARD_SHORTCUTS_PREFERENCES_NAME, ENTRY_KEYBOARD_SHORTCUTS, + ConvertToStringProcessStrategy.SINGLETON); } if (hasFlag(flags, FLAG_FILTERS)) { // TODO export filters @@ -136,9 +139,6 @@ public class DataImportExportUtils implements Constants { } zos.finish(); zos.flush(); - } finally { - RestFuUtils.closeSilently(zos); - RestFuUtils.closeSilently(fos); } } @@ -162,96 +162,94 @@ public class DataImportExportUtils implements Constants { @WorkerThread public static int getImportedSettingsFlags(@NonNull final File src) throws IOException { - final ZipFile zipFile = new ZipFile(src); - int flags = 0; - if (zipFile.getEntry(ENTRY_PREFERENCES) != null) { - flags |= FLAG_PREFERENCES; + try (ZipFile zipFile = new ZipFile(src)) { + int flags = 0; + if (zipFile.getEntry(ENTRY_PREFERENCES) != null) { + flags |= FLAG_PREFERENCES; + } + if (zipFile.getEntry(ENTRY_NICKNAMES) != null) { + flags |= FLAG_NICKNAMES; + } + if (zipFile.getEntry(ENTRY_USER_COLORS) != null) { + flags |= FLAG_USER_COLORS; + } + if (zipFile.getEntry(ENTRY_HOST_MAPPING) != null) { + flags |= FLAG_HOST_MAPPING; + } + if (zipFile.getEntry(ENTRY_KEYBOARD_SHORTCUTS) != null) { + flags |= FLAG_KEYBOARD_SHORTCUTS; + } + if (zipFile.getEntry(ENTRY_FILTERS) != null) { + flags |= FLAG_FILTERS; + } + if (zipFile.getEntry(ENTRY_TABS) != null) { + flags |= FLAG_TABS; + } + return flags; } - if (zipFile.getEntry(ENTRY_NICKNAMES) != null) { - flags |= FLAG_NICKNAMES; - } - if (zipFile.getEntry(ENTRY_USER_COLORS) != null) { - flags |= FLAG_USER_COLORS; - } - if (zipFile.getEntry(ENTRY_HOST_MAPPING) != null) { - flags |= FLAG_HOST_MAPPING; - } - if (zipFile.getEntry(ENTRY_KEYBOARD_SHORTCUTS) != null) { - flags |= FLAG_KEYBOARD_SHORTCUTS; - } - if (zipFile.getEntry(ENTRY_FILTERS) != null) { - flags |= FLAG_FILTERS; - } - if (zipFile.getEntry(ENTRY_TABS) != null) { - flags |= FLAG_TABS; - } - zipFile.close(); - return flags; } public static void importData(final Context context, final File src, final int flags) throws IOException { if (src == null) throw new FileNotFoundException(); - final ZipFile zipFile = new ZipFile(src); - if (hasFlag(flags, FLAG_PREFERENCES)) { - importSharedPreferencesData(zipFile, context, SHARED_PREFERENCES_NAME, ENTRY_PREFERENCES, - new PreferencesExporterStrategy(SharedPreferenceConstants.class)); - } - if (hasFlag(flags, FLAG_NICKNAMES)) { - importSharedPreferencesData(zipFile, context, USER_NICKNAME_PREFERENCES_NAME, ENTRY_NICKNAMES, - ConvertToStringProcessStrategy.SINGLETON); - } - if (hasFlag(flags, FLAG_USER_COLORS)) { - importSharedPreferencesData(zipFile, context, USER_COLOR_PREFERENCES_NAME, ENTRY_USER_COLORS, - ConvertToIntProcessStrategy.SINGLETON); - } - if (hasFlag(flags, FLAG_HOST_MAPPING)) { - importSharedPreferencesData(zipFile, context, HOST_MAPPING_PREFERENCES_NAME, ENTRY_HOST_MAPPING, - ConvertToStringProcessStrategy.SINGLETON); - } - if (hasFlag(flags, FLAG_KEYBOARD_SHORTCUTS)) { - importSharedPreferencesData(zipFile, context, KEYBOARD_SHORTCUTS_PREFERENCES_NAME, - ENTRY_KEYBOARD_SHORTCUTS, ConvertToStringProcessStrategy.SINGLETON); - } - if (hasFlag(flags, FLAG_FILTERS)) { - importItem(context, zipFile, ENTRY_FILTERS, FiltersData.class, new ContentResolverProcessStrategy() { - @Override - public boolean importItem(ContentResolver cr, FiltersData filtersData) throws IOException { - if (filtersData == null) return false; - insertBase(cr, Filters.Keywords.CONTENT_URI, filtersData.getKeywords()); - insertBase(cr, Filters.Sources.CONTENT_URI, filtersData.getSources()); - insertBase(cr, Filters.Links.CONTENT_URI, filtersData.getLinks()); - insertUser(cr, Filters.Users.CONTENT_URI, filtersData.getUsers()); - return true; - } - - void insertBase(ContentResolver cr, Uri uri, List items) throws IOException { - if (items == null) return; - final ObjectCursor.ValuesCreator baseItemCreator = - ObjectCursor.valuesCreatorFrom(FiltersData.BaseItem.class); - List values = new ArrayList<>(items.size()); - for (FiltersData.BaseItem item : items) { - values.add(baseItemCreator.create(item)); + try (ZipFile zipFile = new ZipFile(src)) { + if (hasFlag(flags, FLAG_PREFERENCES)) { + importSharedPreferencesData(zipFile, context, SHARED_PREFERENCES_NAME, ENTRY_PREFERENCES, + new PreferencesExporterStrategy(SharedPreferenceConstants.class)); + } + if (hasFlag(flags, FLAG_NICKNAMES)) { + importSharedPreferencesData(zipFile, context, USER_NICKNAME_PREFERENCES_NAME, ENTRY_NICKNAMES, + ConvertToStringProcessStrategy.SINGLETON); + } + if (hasFlag(flags, FLAG_USER_COLORS)) { + importSharedPreferencesData(zipFile, context, USER_COLOR_PREFERENCES_NAME, ENTRY_USER_COLORS, + ConvertToIntProcessStrategy.SINGLETON); + } + if (hasFlag(flags, FLAG_HOST_MAPPING)) { + importSharedPreferencesData(zipFile, context, HOST_MAPPING_PREFERENCES_NAME, ENTRY_HOST_MAPPING, + ConvertToStringProcessStrategy.SINGLETON); + } + if (hasFlag(flags, FLAG_KEYBOARD_SHORTCUTS)) { + importSharedPreferencesData(zipFile, context, KEYBOARD_SHORTCUTS_PREFERENCES_NAME, + ENTRY_KEYBOARD_SHORTCUTS, ConvertToStringProcessStrategy.SINGLETON); + } + if (hasFlag(flags, FLAG_FILTERS)) { + importItem(context, zipFile, ENTRY_FILTERS, FiltersData.class, new ContentResolverProcessStrategy() { + @Override + public boolean importItem(ContentResolver cr, FiltersData filtersData) throws IOException { + if (filtersData == null) return false; + insertBase(cr, Filters.Keywords.CONTENT_URI, filtersData.getKeywords()); + insertBase(cr, Filters.Sources.CONTENT_URI, filtersData.getSources()); + insertBase(cr, Filters.Links.CONTENT_URI, filtersData.getLinks()); + insertUser(cr, Filters.Users.CONTENT_URI, filtersData.getUsers()); + return true; } - ContentResolverUtils.bulkInsert(cr, uri, values); - } - void insertUser(ContentResolver cr, Uri uri, List items) throws IOException { - if (items == null) return; - final ObjectCursor.ValuesCreator userItemCreator = - ObjectCursor.valuesCreatorFrom(FiltersData.UserItem.class); - List values = new ArrayList<>(items.size()); - for (FiltersData.UserItem item : items) { - values.add(userItemCreator.create(item)); + void insertBase(ContentResolver cr, Uri uri, List items) throws IOException { + if (items == null) return; + final ObjectCursor.ValuesCreator baseItemCreator = + ObjectCursor.valuesCreatorFrom(FiltersData.BaseItem.class); + List values = new ArrayList<>(items.size()); + for (FiltersData.BaseItem item : items) { + values.add(baseItemCreator.create(item)); + } + ContentResolverUtils.bulkInsert(cr, uri, values); } - ContentResolverUtils.bulkInsert(cr, uri, values); - } - }); - } - if (hasFlag(flags, FLAG_TABS)) { - final ObjectCursor.ValuesCreator creator = ObjectCursor.valuesCreatorFrom(Tab.class); - importItemsList(context, zipFile, ENTRY_TABS, Tab.class, new ContentResolverProcessStrategy>() { - @Override - public boolean importItem(ContentResolver cr, List items) throws IOException { + + void insertUser(ContentResolver cr, Uri uri, List items) throws IOException { + if (items == null) return; + final ObjectCursor.ValuesCreator userItemCreator = + ObjectCursor.valuesCreatorFrom(FiltersData.UserItem.class); + List values = new ArrayList<>(items.size()); + for (FiltersData.UserItem item : items) { + values.add(userItemCreator.create(item)); + } + ContentResolverUtils.bulkInsert(cr, uri, values); + } + }); + } + if (hasFlag(flags, FLAG_TABS)) { + final ObjectCursor.ValuesCreator creator = ObjectCursor.valuesCreatorFrom(Tab.class); + importItemsList(context, zipFile, ENTRY_TABS, Tab.class, (cr, items) -> { if (items == null) return false; List values = new ArrayList<>(items.size()); for (Tab item : items) { @@ -260,10 +258,9 @@ public class DataImportExportUtils implements Constants { cr.delete(Tabs.CONTENT_URI, null, null); ContentResolverUtils.bulkInsert(cr, Tabs.CONTENT_URI, values); return true; - } - }); + }); + } } - zipFile.close(); } private static boolean hasFlag(final int flags, final int flag) {