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

Unified Diff: src/org/adblockplus/android/AndroidWebRequest.java

Issue 6680224334872576: Issue 303 - Don't load element hiding rules (Closed)
Patch Set: getPath(), char-by-char Created Nov. 11, 2014, 11:55 a.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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/org/adblockplus/android/AndroidWebRequest.java
diff --git a/src/org/adblockplus/android/AndroidWebRequest.java b/src/org/adblockplus/android/AndroidWebRequest.java
index 35102acc610982d0bc7b5aad471bd9cf1431628a..56e8ca5ec9263077497a15c64d3df0eeadc0f2eb 100644
--- a/src/org/adblockplus/android/AndroidWebRequest.java
+++ b/src/org/adblockplus/android/AndroidWebRequest.java
@@ -17,25 +17,32 @@
package org.adblockplus.android;
-import java.io.InputStream;
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
+import java.util.Locale;
import org.adblockplus.libadblockplus.AdblockPlusException;
import org.adblockplus.libadblockplus.HeaderEntry;
import org.adblockplus.libadblockplus.ServerResponse;
-import org.adblockplus.libadblockplus.WebRequest;
import org.adblockplus.libadblockplus.ServerResponse.NsStatus;
+import org.adblockplus.libadblockplus.WebRequest;
+import org.apache.commons.lang.StringUtils;
import android.util.Log;
public class AndroidWebRequest extends WebRequest
{
- public final String TAG = Utils.getTag(WebRequest.class);
+ public final static String TAG = Utils.getTag(WebRequest.class);
+
+ private static boolean mightBeFilterList(URL url)
+ {
+ final String filename = url.getPath();
- private static final int INITIAL_BUFFER_SIZE = 65536;
- private static final int BUFFER_GROWTH_DELTA = 65536;
+ return StringUtils.isNotEmpty(filename) && filename.toLowerCase(Locale.US).trim().endsWith(".txt");
+ }
@Override
public ServerResponse httpGET(final String urlStr, final List<HeaderEntry> headers)
@@ -43,7 +50,9 @@ public class AndroidWebRequest extends WebRequest
try
{
final URL url = new URL(urlStr);
- Log.d(this.TAG, "Downloading from: " + url);
+ Log.d(TAG, "Downloading from: " + url);
+
+ final boolean mightBeFilterList = mightBeFilterList(url);
final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
@@ -54,34 +63,37 @@ public class AndroidWebRequest extends WebRequest
if (response.getResponseStatus() == 200)
{
- final InputStream in = connection.getInputStream();
+ final BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
+ final StringBuilder sb = new StringBuilder();
- final byte[] buffer = new byte[4096];
-
- byte[] out = new byte[INITIAL_BUFFER_SIZE];
-
- int pos = 0;
- for (;;)
+ if (mightBeFilterList)
{
- final int read = in.read(buffer);
- if (read < 0)
+ String line;
+ while ((line = reader.readLine()) != null)
{
- break;
+ // We're only appending non-element-hiding filters here.
+ // See: https://issues.adblockplus.org/ticket/303
+ if (line.indexOf('#') == -1)
+ {
+ sb.append(line);
+ sb.append('\n');
+ }
}
- if (pos + read > out.length)
+ }
+ else
+ {
+ int character;
+
+ while ((character = reader.read()) != -1)
{
- final byte[] old = out;
- out = new byte[out.length + BUFFER_GROWTH_DELTA];
- System.arraycopy(old, 0, out, 0, pos);
+ sb.append((char) character);
}
- System.arraycopy(buffer, 0, out, pos, read);
- pos += read;
}
connection.disconnect();
response.setStatus(NsStatus.OK);
- response.setResponse(new String(out, 0, pos, "utf-8"));
+ response.setResponse(sb.toString());
}
else
{
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld