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

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

Issue 29372653: Issue 4813 - UTFDataFormatException while serializing filter (Closed)
Patch Set: Including UTF-8 constant and adding migration/file corruption recovery logic Created March 9, 2017, 7:38 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: src/org/adblockplus/sbrowser/contentblocker/engine/Engine.java
===================================================================
--- a/src/org/adblockplus/sbrowser/contentblocker/engine/Engine.java
+++ b/src/org/adblockplus/sbrowser/contentblocker/engine/Engine.java
@@ -44,16 +44,17 @@ import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ResolveInfo;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.preference.PreferenceManager;
+import android.text.TextUtils;
import android.util.Log;
public final class Engine
{
private static final String TAG = Engine.class.getSimpleName();
// TODO make use of this regex's
public static final Pattern RE_SUBSCRIPTION_HEADER = Pattern.compile(
@@ -72,17 +73,17 @@ public final class Engine
public static final String USER_EXCEPTIONS_TITLE = "__exceptions";
public static final String ACTION_OPEN_SETTINGS = "com.samsung.android.sbrowser.contentBlocker.ACTION_SETTING";
public static final String ACTION_UPDATE = "com.samsung.android.sbrowser.contentBlocker.ACTION_UPDATE";
public static final String EASYLIST_URL = "https://easylist-downloads.adblockplus.org/easylist.txt";
public static final String SUBSCRIPTIONS_EXCEPTIONSURL = "subscriptions_exceptionsurl";
- private static final String URL_ENCODE_CHARSET = "UTF-8";
+ public static final String CHARSET_UTF_8 = "UTF-8";
private static final String PREFS_KEY_PREVIOUS_VERSION = "key_previous_version";
// The value below specifies an interval of [x, 2*x[, where x =
// INITIAL_UPDATE_CHECK_DELAY_SECONDS
private static final long INITIAL_UPDATE_CHECK_DELAY_SECONDS = 5;
private static final long UPDATE_CHECK_INTERVAL_MINUTES = 30;
private static final long BROADCAST_COMBINATION_DELAY_MILLIS = 2500;
@@ -435,28 +436,28 @@ public final class Engine
engine.downloader = Downloader.create(context, engine);
return engine;
}
public static String readFileAsString(InputStream instream) throws IOException
{
final StringBuilder sb = new StringBuilder();
- final BufferedReader r = new BufferedReader(new InputStreamReader(instream, "UTF-8"));
+ final BufferedReader r = new BufferedReader(new InputStreamReader(instream, CHARSET_UTF_8));
for (int ch = r.read(); ch != -1; ch = r.read())
{
sb.append((char) ch);
}
return sb.toString();
}
public static List<String> readLines(InputStream instream) throws IOException
{
final ArrayList<String> list = new ArrayList<String>();
- final BufferedReader r = new BufferedReader(new InputStreamReader(instream, "UTF-8"));
+ final BufferedReader r = new BufferedReader(new InputStreamReader(instream, CHARSET_UTF_8));
for (String line = r.readLine(); line != null; line = r.readLine())
{
list.add(line);
}
return list;
}
public static File getOrCreateCachedFilterFile(Context context) throws IOException
@@ -470,17 +471,17 @@ public final class Engine
Log.d(TAG, "Cached filter file not found. Using dummy filter file");
final File dummyFilterFile = getDummyFilterFile(context);
if (!dummyFilterFile.exists())
{
Log.d(TAG, "Creating dummy filter file...");
dummyFilterFile.getParentFile().mkdirs();
final BufferedWriter writer = new BufferedWriter(
- new OutputStreamWriter(new FileOutputStream(dummyFilterFile), "UTF-8"));
+ new OutputStreamWriter(new FileOutputStream(dummyFilterFile), CHARSET_UTF_8));
try
{
writeFilterHeaders(writer);
}
finally
{
writer.close();
}
@@ -531,27 +532,27 @@ public final class Engine
sb.append('&');
}
else
{
sb.append('?');
}
sb.append("addonName=");
- sb.append(URLEncoder.encode(this.appInfo.addonName, URL_ENCODE_CHARSET));
+ sb.append(URLEncoder.encode(this.appInfo.addonName, CHARSET_UTF_8));
sb.append("&addonVersion=");
- sb.append(URLEncoder.encode(this.appInfo.addonVersion, URL_ENCODE_CHARSET));
+ sb.append(URLEncoder.encode(this.appInfo.addonVersion, CHARSET_UTF_8));
sb.append("&application=");
- sb.append(URLEncoder.encode(this.appInfo.application, URL_ENCODE_CHARSET));
+ sb.append(URLEncoder.encode(this.appInfo.application, CHARSET_UTF_8));
sb.append("&applicationVersion=");
- sb.append(URLEncoder.encode(this.appInfo.applicationVersion, URL_ENCODE_CHARSET));
+ sb.append(URLEncoder.encode(this.appInfo.applicationVersion, CHARSET_UTF_8));
sb.append("&platform=");
- sb.append(URLEncoder.encode(this.appInfo.platform, URL_ENCODE_CHARSET));
+ sb.append(URLEncoder.encode(this.appInfo.platform, CHARSET_UTF_8));
sb.append("&platformVersion=");
- sb.append(URLEncoder.encode(this.appInfo.platformVersion, URL_ENCODE_CHARSET));
+ sb.append(URLEncoder.encode(this.appInfo.platformVersion, CHARSET_UTF_8));
sb.append("&lastVersion=");
sb.append(sub.getVersion());
sb.append("&downloadCount=");
final long downloadCount = sub.getDownloadCount();
if (downloadCount < 5)
{
sb.append(downloadCount);
}
@@ -706,25 +707,28 @@ public final class Engine
}
}
public void enqueueDownload(final Subscription sub, final boolean forced) throws IOException
{
if (sub.getURL() != null && sub.shouldUpdate(forced))
{
final HashMap<String, String> headers = new HashMap<String, String>();
- final String lastModified = sub.getMeta(Subscription.KEY_HTTP_LAST_MODIFIED);
- if (lastModified != null)
+ if (sub.isMetaDataValid() && sub.isFiltersValid())
{
- headers.put("If-Modified-Since", lastModified);
- }
- final String etag = sub.getMeta(Subscription.KEY_HTTP_ETAG);
- if (etag != null)
- {
- headers.put("If-None-Match", etag);
+ final String lastModified = sub.getMeta(Subscription.KEY_HTTP_LAST_MODIFIED);
+ if (!TextUtils.isEmpty(lastModified))
+ {
+ headers.put("If-Modified-Since", lastModified);
+ }
+ final String etag = sub.getMeta(Subscription.KEY_HTTP_ETAG);
+ if (!TextUtils.isEmpty(etag))
+ {
+ headers.put("If-None-Match", etag);
+ }
}
Log.d(TAG, headers.toString());
this.downloader.enqueueDownload(this.createDownloadURL(sub), sub.getId(), headers);
}
}
public void connectivityChanged()
{

Powered by Google App Engine
This is Rietveld