2222import java .util .HashSet ;
2323import java .util .Iterator ;
2424import java .util .List ;
25+ import java .util .Map ;
2526import java .util .concurrent .ConcurrentHashMap ;
2627import java .util .concurrent .atomic .AtomicReference ;
2728
@@ -37,7 +38,6 @@ public class DnsPrefetcher {
3738 private static Configuration config ;
3839
3940 private static ConcurrentHashMap <String , List <InetAddress >> mConcurrentHashMap = new ConcurrentHashMap <String , List <InetAddress >>();
40- private static List <String > mHosts = new ArrayList <String >();
4141 private static AtomicReference mDnsCacheKey = new AtomicReference ();
4242
4343 private DnsPrefetcher () {
@@ -75,7 +75,6 @@ public void localFetch() {
7575 preFetch (localHosts );
7676 }
7777
78-
7978 public DnsPrefetcher init (String token , Configuration config ) throws UnknownHostException {
8079 this .token = token ;
8180 this .config = config ;
@@ -89,15 +88,6 @@ public void setConcurrentHashMap(ConcurrentHashMap<String, List<InetAddress>> mC
8988 this .mConcurrentHashMap = mConcurrentHashMap ;
9089 }
9190
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-
10191 //use for test
10292 public ConcurrentHashMap <String , List <InetAddress >> getConcurrentHashMap () {
10393 return this .mConcurrentHashMap ;
@@ -145,7 +135,6 @@ private void preFetch(List<String> fetchHost) {
145135 try {
146136 inetAddresses = okhttp3 .Dns .SYSTEM .lookup (host );
147137 mConcurrentHashMap .put (host , inetAddresses );
148- mHosts .add (host );
149138 } catch (UnknownHostException e ) {
150139 e .printStackTrace ();
151140 rePreHosts .add (host );
@@ -181,7 +170,6 @@ private boolean rePreFetch(String host, Dns customeDns) {
181170 inetAddresses = customeDns .lookup (host );
182171 }
183172 mConcurrentHashMap .put (host , inetAddresses );
184- mHosts .add (host );
185173 return true ;
186174 } catch (UnknownHostException e ) {
187175 e .printStackTrace ();
@@ -198,14 +186,20 @@ private boolean rePreFetch(String host, Dns customeDns) {
198186 */
199187 public void dnsPreByCustom (Dns dns ) {
200188 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+ }
209203 }
210204 }
211205 rePreFetch (rePreHosts , dns );
@@ -405,16 +399,6 @@ public static boolean recoverDnsCache(byte[] data) {
405399 return true ;
406400 }
407401 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 );
418402 return false ;
419403 }
420404}
0 commit comments