22
22
import java .util .HashSet ;
23
23
import java .util .Iterator ;
24
24
import java .util .List ;
25
+ import java .util .Map ;
25
26
import java .util .concurrent .ConcurrentHashMap ;
26
27
import java .util .concurrent .atomic .AtomicReference ;
27
28
@@ -37,7 +38,6 @@ public class DnsPrefetcher {
37
38
private static Configuration config ;
38
39
39
40
private static ConcurrentHashMap <String , List <InetAddress >> mConcurrentHashMap = new ConcurrentHashMap <String , List <InetAddress >>();
40
- private static List <String > mHosts = new ArrayList <String >();
41
41
private static AtomicReference mDnsCacheKey = new AtomicReference ();
42
42
43
43
private DnsPrefetcher () {
@@ -75,7 +75,6 @@ public void localFetch() {
75
75
preFetch (localHosts );
76
76
}
77
77
78
-
79
78
public DnsPrefetcher init (String token , Configuration config ) throws UnknownHostException {
80
79
this .token = token ;
81
80
this .config = config ;
@@ -89,15 +88,6 @@ public void setConcurrentHashMap(ConcurrentHashMap<String, List<InetAddress>> mC
89
88
this .mConcurrentHashMap = mConcurrentHashMap ;
90
89
}
91
90
92
- //use for test
93
- public List <String > getHosts () {
94
- return this .mHosts ;
95
- }
96
-
97
- public void setHosts (List mHosts ) {
98
- this .mHosts = mHosts ;
99
- }
100
-
101
91
//use for test
102
92
public ConcurrentHashMap <String , List <InetAddress >> getConcurrentHashMap () {
103
93
return this .mConcurrentHashMap ;
@@ -145,7 +135,6 @@ private void preFetch(List<String> fetchHost) {
145
135
try {
146
136
inetAddresses = okhttp3 .Dns .SYSTEM .lookup (host );
147
137
mConcurrentHashMap .put (host , inetAddresses );
148
- mHosts .add (host );
149
138
} catch (UnknownHostException e ) {
150
139
e .printStackTrace ();
151
140
rePreHosts .add (host );
@@ -181,7 +170,6 @@ private boolean rePreFetch(String host, Dns customeDns) {
181
170
inetAddresses = customeDns .lookup (host );
182
171
}
183
172
mConcurrentHashMap .put (host , inetAddresses );
184
- mHosts .add (host );
185
173
return true ;
186
174
} catch (UnknownHostException e ) {
187
175
e .printStackTrace ();
@@ -198,14 +186,20 @@ private boolean rePreFetch(String host, Dns customeDns) {
198
186
*/
199
187
public void dnsPreByCustom (Dns dns ) {
200
188
List <String > rePreHosts = new ArrayList <String >();
201
- for (String host : mHosts ) {
202
- List <InetAddress > inetAddresses = null ;
203
- try {
204
- inetAddresses = dns .lookup (host );
205
- mConcurrentHashMap .put (host , inetAddresses );
206
- } catch (UnknownHostException e ) {
207
- e .printStackTrace ();
208
- rePreHosts .add (host );
189
+ if (mConcurrentHashMap != null && mConcurrentHashMap .size () > 0 ) {
190
+ Iterator iter = mConcurrentHashMap .keySet ().iterator ();
191
+ while (iter .hasNext ()) {
192
+ String tmpkey = (String ) iter .next ();
193
+ if (!(tmpkey == null ) && !(tmpkey .length () == 0 )) {
194
+ List <InetAddress > inetAddresses = null ;
195
+ try {
196
+ inetAddresses = dns .lookup (tmpkey );
197
+ mConcurrentHashMap .put (tmpkey , inetAddresses );
198
+ } catch (UnknownHostException e ) {
199
+ e .printStackTrace ();
200
+ rePreHosts .add (tmpkey );
201
+ }
202
+ }
209
203
}
210
204
}
211
205
rePreFetch (rePreHosts , dns );
@@ -405,16 +399,6 @@ public static boolean recoverDnsCache(byte[] data) {
405
399
return true ;
406
400
}
407
401
DnsPrefetcher .getDnsPrefetcher ().setConcurrentHashMap (concurrentHashMap );
408
-
409
- ArrayList <String > list = new ArrayList <String >();
410
- Iterator iter = concurrentHashMap .keySet ().iterator ();
411
- while (iter .hasNext ()) {
412
- String tmpkey = (String ) iter .next ();
413
- if (tmpkey == null || tmpkey .length () == 0 )
414
- continue ;
415
- list .add (tmpkey );
416
- }
417
- DnsPrefetcher .getDnsPrefetcher ().setHosts (list );
418
402
return false ;
419
403
}
420
404
}
0 commit comments