Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: adblockplussbrowser/src/org/adblockplus/sbrowser/contentblocker/engine/Subscription.java

Issue 29436555: Issue 5231 - Use Java 7 features (Closed)
Patch Set: Created May 11, 2017, 6:30 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: adblockplussbrowser/src/org/adblockplus/sbrowser/contentblocker/engine/Subscription.java
===================================================================
--- a/adblockplussbrowser/src/org/adblockplus/sbrowser/contentblocker/engine/Subscription.java
+++ b/adblockplussbrowser/src/org/adblockplus/sbrowser/contentblocker/engine/Subscription.java
@@ -23,16 +23,17 @@ import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.net.URL;
+import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
@@ -61,35 +62,35 @@ final class Subscription
public static final String KEY_DOWNLOAD_COUNT = "_download_count";
public static final String KEY_ENABLED = "_enabled";
public static final String KEY_HAS_FILTERS = "_has_filters";
public static final String KEY_META_HASH = "_meta_hash";
public static final long MINIMAL_DOWNLOAD_INTERVAL = Engine.MILLIS_PER_HOUR / 4;
public static final long DOWNLOAD_RETRY_INTERVAL = Engine.MILLIS_PER_HOUR;
- private static final HashSet<String> ALLOWED_META_KEYS = new HashSet<String>();
+ private static final HashSet<String> ALLOWED_META_KEYS = new HashSet<>();
private static final Locale LOCALE_EN = Locale.ENGLISH;
private final long updateInterval = Engine.MILLIS_PER_DAY
+ (long) (Engine.MILLIS_PER_HOUR * 8. * Math.random());
/**
* List of meta keys that are allowed to import from a downloaded
* subscription.
*/
private static final String[] ALLOWED_META_KEYS_ARRAY =
{
"checksum", KEY_VERSION, KEY_TITLE, "last modified", "expires", "homepage", "licence"
};
private final URL url;
private final Type type;
- private final HashMap<String, String> meta = new HashMap<String, String>();
- private final HashSet<String> filters = new HashSet<String>();
+ private final HashMap<String, String> meta = new HashMap<>();
+ private final HashSet<String> filters = new HashSet<>();
private boolean metaDataValid = true;
private boolean filtersValid = true;
static
{
for (final String s : ALLOWED_META_KEYS_ARRAY)
{
@@ -366,17 +367,17 @@ final class Subscription
}
sb.append(Integer.toHexString(value));
}
return sb.toString();
}
private static String createMetaDataHash(final HashMap<String, String> meta) throws IOException
{
- final ArrayList<String> keyValues = new ArrayList<String>();
+ final ArrayList<String> keyValues = new ArrayList<>();
for (final Entry<String, String> e : meta.entrySet())
{
if (!KEY_META_HASH.equals(e.getKey()))
{
keyValues.add(e.getKey() + ":" + e.getValue());
}
}
return createFilterHash(keyValues);
@@ -385,155 +386,113 @@ final class Subscription
private static String createFilterHash(List<String> filters) throws IOException
{
try
{
final MessageDigest md5 = MessageDigest.getInstance("MD5");
Collections.sort(filters);
for (final String filter : filters)
{
- md5.update(filter.getBytes(Engine.CHARSET_UTF_8));
+ md5.update(filter.getBytes(StandardCharsets.UTF_8));
}
return byteArrayToHexString(md5.digest());
}
catch (final NoSuchAlgorithmException e)
{
throw new IOException("MD5 is unavailable: " + e.getMessage(), e);
}
}
public void serializeMetaData(final File metaFile) throws IOException
{
this.putMeta(KEY_META_HASH, createMetaDataHash(this.meta));
- final DataOutputStream metaOut = new DataOutputStream(new GZIPOutputStream(
- new BufferedOutputStream(new FileOutputStream(metaFile))));
- try
+ try (final DataOutputStream metaOut = new DataOutputStream(new GZIPOutputStream(
+ new BufferedOutputStream(new FileOutputStream(metaFile)))))
{
metaOut.writeUTF(this.url != null ? this.url.toString() : "");
metaOut.writeInt(this.meta.size());
for (final Entry<String, String> e : this.meta.entrySet())
{
metaOut.writeUTF(e.getKey());
metaOut.writeUTF(e.getValue());
}
}
- finally
- {
- metaOut.close();
- }
}
public void serializeFilters(final File filtersFile) throws IOException
{
- final DataOutputStream filtersOut = new DataOutputStream(new GZIPOutputStream(
- new BufferedOutputStream(new FileOutputStream(filtersFile))));
- try
+ try (final DataOutputStream filtersOut = new DataOutputStream(new GZIPOutputStream(
+ new BufferedOutputStream(new FileOutputStream(filtersFile)))))
{
filtersOut.writeInt(this.filters.size());
filtersOut.writeUTF(createFilterHash(new ArrayList<String>(this.filters)));
for (final String s : this.filters)
{
- final byte[] b = s.getBytes(Engine.CHARSET_UTF_8);
+ final byte[] b = s.getBytes(StandardCharsets.UTF_8);
filtersOut.writeInt(b.length);
filtersOut.write(b);
}
}
- finally
- {
- filtersOut.close();
- }
}
public void serializeSubscription(final File metaFile, final File filtersFile) throws IOException
{
this.serializeMetaData(metaFile);
this.serializeFilters(filtersFile);
}
public static Subscription deserializeSubscription(final File metaFile)
{
Subscription sub = null;
- DataInputStream in = null;
- try
+ try (final DataInputStream in = new DataInputStream(new GZIPInputStream(new BufferedInputStream(
+ new FileInputStream(metaFile)))))
{
- in = new DataInputStream(new GZIPInputStream(new BufferedInputStream(
- new FileInputStream(metaFile))));
final String urlString = in.readUTF();
sub = new Subscription(!TextUtils.isEmpty(urlString) ? new URL(urlString) : null);
sub.metaDataValid = false;
final int numMetaEntries = in.readInt();
for (int i = 0; i < numMetaEntries; i++)
{
final String key = in.readUTF();
final String value = in.readUTF();
sub.meta.put(key, value);
}
sub.metaDataValid = createMetaDataHash(sub.meta).equals(sub.getMeta(KEY_META_HASH));
}
catch (Throwable t)
{
// We catch Throwable here in order to return whatever we could retrieve from the meta file
}
- finally
- {
- if (in != null)
- {
- try
- {
- in.close();
- }
- catch (IOException e)
- {
- // Ignored
- }
- }
- }
return sub;
}
public void deserializeFilters(final File filtersFile)
{
this.clearFilters();
this.filtersValid = false;
- DataInputStream in = null;
- try
+ try (final DataInputStream in = new DataInputStream(new GZIPInputStream(new BufferedInputStream(
+ new FileInputStream(filtersFile)))))
{
- in = new DataInputStream(new GZIPInputStream(new BufferedInputStream(
- new FileInputStream(filtersFile))));
final int numFilters = in.readInt();
final String filtersHash = in.readUTF();
for (int i = 0; i < numFilters; i++)
{
final int length = in.readInt();
final byte[] b = new byte[length];
in.readFully(b);
- this.filters.add(new String(b, Engine.CHARSET_UTF_8));
+ this.filters.add(new String(b, StandardCharsets.UTF_8));
}
this.filtersValid = createFilterHash(new ArrayList<String>(this.filters)).equals(
filtersHash);
Log.d(TAG, "Filters valid: " + this.filtersValid);
}
catch (Throwable t)
{
// We catch Throwable here in order to load whatever we could retrieve from the filters file
}
- finally
- {
- if (in != null)
- {
- try
- {
- in.close();
- }
- catch (IOException e)
- {
- // Ignored
- }
- }
- }
}
/**
* Adds the given string, which should be a single filter to this
* subscription.
*
* @param input
*/
@@ -574,24 +533,22 @@ final class Subscription
{
this.parseLine(line);
}
return this;
}
public Subscription parseText(final String string)
{
- final BufferedReader r = new BufferedReader(new StringReader(string));
- try
+ try (final BufferedReader r = new BufferedReader(new StringReader(string)))
{
for (String line = r.readLine(); line != null; line = r.readLine())
{
this.parseLine(line);
}
- r.close();
}
catch (final IOException e)
{
// ignored ... we're reading from a String
}
return this;
}

Powered by Google App Engine
This is Rietveld