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

Side by Side Diff: adblockplussbrowser/src/org/adblockplus/sbrowser/contentblocker/engine/Downloader.java

Issue 29453722: Noissue - Lint adjustments and optimizations (Closed)
Patch Set: Created June 1, 2017, 9:30 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 Eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 13 matching lines...) Expand all
24 import java.net.URL; 24 import java.net.URL;
25 import java.nio.charset.StandardCharsets; 25 import java.nio.charset.StandardCharsets;
26 import java.util.HashMap; 26 import java.util.HashMap;
27 import java.util.HashSet; 27 import java.util.HashSet;
28 import java.util.Map; 28 import java.util.Map;
29 import java.util.Map.Entry; 29 import java.util.Map.Entry;
30 import java.util.concurrent.LinkedBlockingQueue; 30 import java.util.concurrent.LinkedBlockingQueue;
31 import java.util.concurrent.TimeUnit; 31 import java.util.concurrent.TimeUnit;
32 import java.util.concurrent.locks.ReentrantLock; 32 import java.util.concurrent.locks.ReentrantLock;
33 import android.annotation.SuppressLint; 33 import android.annotation.SuppressLint;
34 import android.content.Context;
35 import android.util.Log; 34 import android.util.Log;
36 35
37 @SuppressLint("DefaultLocale") 36 @SuppressLint("DefaultLocale")
38 final class Downloader 37 final class Downloader
39 { 38 {
40 private static final int MAX_RETRIES = 5; 39 private static final int MAX_RETRIES = 5;
41 40
42 private static final String TAG = Downloader.class.getSimpleName(); 41 private static final String TAG = Downloader.class.getSimpleName();
43 private final Engine engine; 42 private final Engine engine;
44 private final ReentrantLock accessLock = new ReentrantLock(); 43 private final ReentrantLock accessLock = new ReentrantLock();
45 private Thread downloaderThread; 44 private Thread downloaderThread;
46 private LinkedBlockingQueue<DownloadJob> downloadJobs = new LinkedBlockingQueu e<>(); 45 private final LinkedBlockingQueue<DownloadJob> downloadJobs = new LinkedBlocki ngQueue<>();
47 private HashSet<String> enqueuedIds = new HashSet<>(); 46 private final HashSet<String> enqueuedIds = new HashSet<>();
48 private boolean downloaderEnabled = true; 47 private boolean downloaderEnabled = true;
49 48
50 private Downloader(final Engine engine) 49 private Downloader(final Engine engine)
51 { 50 {
52 this.engine = engine; 51 this.engine = engine;
53 } 52 }
54 53
55 void lock() 54 void lock()
56 { 55 {
57 this.accessLock.lock(); 56 this.accessLock.lock();
(...skipping 12 matching lines...) Expand all
70 Log.d(TAG, "Re-checking download permission"); 69 Log.d(TAG, "Re-checking download permission");
71 } 70 }
72 this.downloaderEnabled = true; 71 this.downloaderEnabled = true;
73 this.unlock(); 72 this.unlock();
74 } 73 }
75 74
76 static void download(final DownloadJob job) throws IOException 75 static void download(final DownloadJob job) throws IOException
77 { 76 {
78 final HttpURLConnection connection = (HttpURLConnection) job.url.openConnect ion(); 77 final HttpURLConnection connection = (HttpURLConnection) job.url.openConnect ion();
79 connection.setRequestMethod("GET"); 78 connection.setRequestMethod("GET");
80 if (job.headers != null) 79 for (final Entry<String, String> e : job.headers.entrySet())
81 { 80 {
82 for (final Entry<String, String> e : job.headers.entrySet()) 81 connection.addRequestProperty(e.getKey(), e.getValue());
83 {
84 connection.addRequestProperty(e.getKey(), e.getValue());
85 }
86 } 82 }
87 connection.connect(); 83 connection.connect();
88 84
89 job.responseCode = connection.getResponseCode(); 85 job.responseCode = connection.getResponseCode();
90 job.responseHeaders.clear(); 86 job.responseHeaders.clear();
91 job.responseText = null; 87 job.responseText = null;
92 88
93 for (int i = 1;; i++) 89 for (int i = 1;; i++)
94 { 90 {
95 final String key = connection.getHeaderFieldKey(i); 91 final String key = connection.getHeaderFieldKey(i);
(...skipping 27 matching lines...) Expand all
123 this.enqueuedIds.add(id); 119 this.enqueuedIds.add(id);
124 this.downloadJobs.add(new DownloadJob(url, id, headers)); 120 this.downloadJobs.add(new DownloadJob(url, id, headers));
125 } 121 }
126 } 122 }
127 finally 123 finally
128 { 124 {
129 this.unlock(); 125 this.unlock();
130 } 126 }
131 } 127 }
132 128
133 public static Downloader create(final Context context, final Engine engine) 129 public static Downloader create(final Engine engine)
134 { 130 {
135 final Downloader downloader = new Downloader(engine); 131 final Downloader downloader = new Downloader(engine);
136 132
137 downloader.downloaderThread = new Thread(new DownloaderHandler(downloader)); 133 downloader.downloaderThread = new Thread(new DownloaderHandler(downloader));
138 downloader.downloaderThread.setDaemon(true); 134 downloader.downloaderThread.setDaemon(true);
139 downloader.downloaderThread.start(); 135 downloader.downloaderThread.start();
140 136
141 return downloader; 137 return downloader;
142 } 138 }
143 139
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
242 } 238 }
243 239
244 private static class DownloadJob 240 private static class DownloadJob
245 { 241 {
246 private final URL url; 242 private final URL url;
247 private final String id; 243 private final String id;
248 private final HashMap<String, String> headers = new HashMap<>(); 244 private final HashMap<String, String> headers = new HashMap<>();
249 private int retryCount = 0; 245 private int retryCount = 0;
250 246
251 private int responseCode = 0; 247 private int responseCode = 0;
252 private HashMap<String, String> responseHeaders = new HashMap<>(); 248 private final HashMap<String, String> responseHeaders = new HashMap<>();
253 private String responseText = null; 249 private String responseText = null;
254 250
255 public DownloadJob(final URL url, final String id, final Map<String, String> headers) 251 public DownloadJob(final URL url, final String id, final Map<String, String> headers)
256 { 252 {
257 this.url = url; 253 this.url = url;
258 this.id = id; 254 this.id = id;
259 if (headers != null) 255 if (headers != null)
260 { 256 {
261 this.headers.putAll(headers); 257 this.headers.putAll(headers);
262 } 258 }
263 } 259 }
264 } 260 }
265 } 261 }
OLDNEW

Powered by Google App Engine
This is Rietveld