Index: adblockplussbrowser/src/org/adblockplus/sbrowser/contentblocker/engine/Subscriptions.java |
=================================================================== |
--- a/adblockplussbrowser/src/org/adblockplus/sbrowser/contentblocker/engine/Subscriptions.java |
+++ b/adblockplussbrowser/src/org/adblockplus/sbrowser/contentblocker/engine/Subscriptions.java |
@@ -17,16 +17,17 @@ |
package org.adblockplus.sbrowser.contentblocker.engine; |
import java.io.BufferedWriter; |
import java.io.File; |
import java.io.FileOutputStream; |
import java.io.IOException; |
import java.io.OutputStreamWriter; |
+import java.nio.charset.StandardCharsets; |
import java.util.ArrayList; |
import java.util.HashMap; |
import java.util.HashSet; |
import java.util.List; |
import java.util.Map; |
import org.adblockplus.sbrowser.contentblocker.engine.Subscription.Type; |
@@ -39,17 +40,17 @@ import android.util.Log; |
final class Subscriptions |
{ |
private static final String TAG = Subscriptions.class.getSimpleName(); |
private static final String[] USER_SUBSCRIPTIONS = |
{ Engine.USER_FILTERS_TITLE, Engine.USER_EXCEPTIONS_TITLE }; |
// Filters that begin with '|$' , '||$' , '@@|$' or '@@||$' |
// See https://issues.adblockplus.org/ticket/4772 |
private static final String UNSUPPORTED_FILTERS_REGEX = "^(\\|\\$|\\|\\|\\$|@@\\|\\$|@@\\|\\|\\$).*"; |
- private final HashMap<String, Subscription> subscriptions = new HashMap<String, Subscription>(); |
+ private final HashMap<String, Subscription> subscriptions = new HashMap<>(); |
private final Engine engine; |
private final File subscriptionFolder; |
private final File cacheFolder; |
private final boolean wasUnitialized; |
private Subscriptions(final Engine engine, final File appFolder, final File cacheFolder) |
{ |
@@ -76,17 +77,17 @@ final class Subscriptions |
this.writeFile(file); |
return file; |
} |
} |
} |
List<SubscriptionInfo> getSubscriptions(final Engine engine) |
{ |
- final ArrayList<SubscriptionInfo> subs = new ArrayList<SubscriptionInfo>(); |
+ final ArrayList<SubscriptionInfo> subs = new ArrayList<>(); |
for (final Subscription sub : this.subscriptions.values()) |
{ |
subs.add(SubscriptionInfo.create(engine, sub)); |
} |
return subs; |
} |
void getSubscriptions(final List<Subscription> list) |
@@ -163,35 +164,34 @@ final class Subscriptions |
* This method combines all currently listed and enabled subscriptions into |
* one text file. |
* |
* @param output |
* @throws IOException |
*/ |
private void writeFile(final File output) throws IOException |
{ |
- final HashSet<String> filters = new HashSet<String>(); |
+ final HashSet<String> filters = new HashSet<>(); |
for (final Subscription s : this.subscriptions.values()) |
{ |
if (s.isEnabled()) |
{ |
Log.d(TAG, "Adding filters for '" + s.getId() + "'"); |
s.deserializeFilters(this.getFiltersFile(s)); |
s.copyFilters(filters); |
s.clearFilters(); |
} |
if ((!s.isMetaDataValid() || !s.isFiltersValid()) && s.getURL() != null) |
{ |
this.engine.enqueueDownload(s, true); |
} |
} |
- final BufferedWriter w = new BufferedWriter( |
- new OutputStreamWriter(new FileOutputStream(output), Engine.CHARSET_UTF_8)); |
- try |
+ try (final BufferedWriter w = new BufferedWriter( |
+ new OutputStreamWriter(new FileOutputStream(output), StandardCharsets.UTF_8))) |
{ |
Log.d(TAG, "Writing " + filters.size() + " filters"); |
Engine.writeFilterHeaders(w); |
for (final String filter : filters) |
{ |
// This is a temporary fix to not write filters that might crash Samsung Internet |
// See https://issues.adblockplus.org/ticket/4772 |
if (!filter.matches(UNSUPPORTED_FILTERS_REGEX)) |
@@ -200,20 +200,16 @@ final class Subscriptions |
w.write('\n'); |
} |
else |
{ |
Log.d(TAG, "Ignoring unsupported filter: " + filter); |
} |
} |
} |
- finally |
- { |
- w.close(); |
- } |
} |
public Subscription add(final Subscription sub) |
{ |
final String id = sub.getId(); |
if (!this.subscriptions.containsKey(id)) |
{ |
this.subscriptions.put(id, sub); |