Browse Source

Breaking out parsing code into different method

master
anthony restaino 8 years ago
parent
commit
ad47bc1fdb
  1. 39
      app/src/main/java/acr/browser/lightning/adblock/AdBlock.java

39
app/src/main/java/acr/browser/lightning/adblock/AdBlock.java

@ -16,7 +16,9 @@ import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
import java.util.List;
import java.util.Set; import java.util.Set;
import javax.inject.Inject; import javax.inject.Inject;
@ -134,9 +136,28 @@ public class AdBlock {
String line; String line;
long time = System.currentTimeMillis(); long time = System.currentTimeMillis();
final List<String> domains = new ArrayList<>(1);
while ((line = reader.readLine()) != null) { while ((line = reader.readLine()) != null) {
lineBuilder.append(line); lineBuilder.append(line);
parseString(lineBuilder, domains);
lineBuilder.setLength(0);
}
mBlockedDomainsList.addAll(domains);
Log.d(TAG, "Loaded ad list in: " + (System.currentTimeMillis() - time) + " ms");
} catch (IOException e) {
Log.wtf(TAG, "Reading blocked domains list from file '"
+ BLOCKED_DOMAINS_LIST_FILE_NAME + "' failed.", e);
} finally {
Utils.close(reader);
}
}
});
}
private static void parseString(@NonNull StringBuilder lineBuilder, @NonNull List<String> parsedList) {
if (!StringBuilderUtils.isEmpty(lineBuilder) && if (!StringBuilderUtils.isEmpty(lineBuilder) &&
!StringBuilderUtils.startsWith(lineBuilder, COMMENT)) { !StringBuilderUtils.startsWith(lineBuilder, COMMENT)) {
StringBuilderUtils.replace(lineBuilder, LOCAL_IP_V4, EMPTY); StringBuilderUtils.replace(lineBuilder, LOCAL_IP_V4, EMPTY);
@ -159,26 +180,18 @@ public class AdBlock {
StringBuilderUtils.trim(partial); StringBuilderUtils.trim(partial);
String partialLine = partial.toString(); String partialLine = partial.toString();
mBlockedDomainsList.add(partialLine);
// Add string to list
parsedList.add(partialLine);
StringBuilderUtils.replace(lineBuilder, partialLine, EMPTY); StringBuilderUtils.replace(lineBuilder, partialLine, EMPTY);
StringBuilderUtils.trim(lineBuilder); StringBuilderUtils.trim(lineBuilder);
} }
if (lineBuilder.length() > 0) { if (lineBuilder.length() > 0) {
mBlockedDomainsList.add(lineBuilder.toString()); // Add string to list.
parsedList.add(lineBuilder.toString());
} }
} }
} }
lineBuilder.setLength(0);
}
Log.d(TAG, "Loaded ad list in: " + (System.currentTimeMillis() - time) + " ms");
} catch (IOException e) {
Log.wtf(TAG, "Reading blocked domains list from file '"
+ BLOCKED_DOMAINS_LIST_FILE_NAME + "' failed.", e);
} finally {
Utils.close(reader);
}
}
});
} }
} }

Loading…
Cancel
Save