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

Unified Diff: adblockplussbrowser/src/org/adblockplus/sbrowser/contentblocker/engine/Downloader.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/Downloader.java
===================================================================
--- a/adblockplussbrowser/src/org/adblockplus/sbrowser/contentblocker/engine/Downloader.java
+++ b/adblockplussbrowser/src/org/adblockplus/sbrowser/contentblocker/engine/Downloader.java
@@ -17,16 +17,17 @@
package org.adblockplus.sbrowser.contentblocker.engine;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
+import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
import android.annotation.SuppressLint;
@@ -37,18 +38,18 @@ import android.util.Log;
final class Downloader
{
private static final int MAX_RETRIES = 5;
private static final String TAG = Downloader.class.getSimpleName();
private final Engine engine;
private final ReentrantLock accessLock = new ReentrantLock();
private Thread downloaderThread;
- private LinkedBlockingQueue<DownloadJob> downloadJobs = new LinkedBlockingQueue<DownloadJob>();
- private HashSet<String> enqueuedIds = new HashSet<String>();
+ private LinkedBlockingQueue<DownloadJob> downloadJobs = new LinkedBlockingQueue<>();
+ private HashSet<String> enqueuedIds = new HashSet<>();
private boolean downloaderEnabled = true;
private Downloader(final Engine engine)
{
this.engine = engine;
}
void lock()
@@ -96,30 +97,25 @@ final class Downloader
{
break;
}
final String value = connection.getHeaderField(i);
job.responseHeaders.put(key.toLowerCase(), value);
}
final StringBuilder sb = new StringBuilder();
- final BufferedReader r = new BufferedReader(new InputStreamReader(connection.getInputStream(),
- Engine.CHARSET_UTF_8));
- try
+ try (final BufferedReader r = new BufferedReader(new InputStreamReader(
+ connection.getInputStream(), StandardCharsets.UTF_8)))
{
for (int ch = r.read(); ch != -1; ch = r.read())
{
sb.append((char) ch);
}
job.responseText = sb.toString();
}
- finally
- {
- r.close();
- }
}
public void enqueueDownload(final URL url, final String id, final Map<String, String> headers)
{
this.lock();
try
{
if (!this.enqueuedIds.contains(id))
@@ -155,17 +151,17 @@ final class Downloader
{
this.downloader = downloader;
}
@Override
public void run()
{
Log.d(TAG, "Handler thread started");
- final LinkedBlockingQueue<DownloadJob> reQueue = new LinkedBlockingQueue<DownloadJob>();
+ final LinkedBlockingQueue<DownloadJob> reQueue = new LinkedBlockingQueue<>();
boolean interrupted = false;
while (!interrupted)
{
DownloadJob job = null;
try
{
if (!this.downloader.downloaderEnabled)
{
@@ -244,21 +240,21 @@ final class Downloader
Log.d(TAG, "Handler thread finished");
}
}
private static class DownloadJob
{
private final URL url;
private final String id;
- private final HashMap<String, String> headers = new HashMap<String, String>();
+ private final HashMap<String, String> headers = new HashMap<>();
private int retryCount = 0;
private int responseCode = 0;
- private HashMap<String, String> responseHeaders = new HashMap<String, String>();
+ private HashMap<String, String> responseHeaders = new HashMap<>();
private String responseText = null;
public DownloadJob(final URL url, final String id, final Map<String, String> headers)
{
this.url = url;
this.id = id;
if (headers != null)
{

Powered by Google App Engine
This is Rietveld