2222import java .util .HashSet ;
2323import java .util .Iterator ;
2424import java .util .List ;
25- import java .util .Set ;
2625import java .util .concurrent .ConcurrentHashMap ;
2726import java .util .concurrent .atomic .AtomicReference ;
2827
@@ -38,8 +37,6 @@ public class DnsPrefetcher {
3837 private static Configuration config ;
3938
4039 private static ConcurrentHashMap <String , List <InetAddress >> mConcurrentHashMap = new ConcurrentHashMap <String , List <InetAddress >>();
41- private static List <String > mHosts = new ArrayList <String >();
42- private static Set <String > mHostSets = new HashSet <String >();
4340 private static AtomicReference mDnsCacheKey = new AtomicReference ();
4441
4542 private DnsPrefetcher () {
@@ -77,7 +74,6 @@ public void localFetch() {
7774 preFetch (localHosts );
7875 }
7976
80-
8177 public DnsPrefetcher init (String token , Configuration config ) throws UnknownHostException {
8278 this .token = token ;
8379 this .config = config ;
@@ -91,15 +87,6 @@ public void setConcurrentHashMap(ConcurrentHashMap<String, List<InetAddress>> mC
9187 this .mConcurrentHashMap = mConcurrentHashMap ;
9288 }
9389
94- //use for test
95- public List <String > getHosts () {
96- return this .mHosts ;
97- }
98-
99- public void setHosts (List mHosts ) {
100- this .mHosts = mHosts ;
101- }
102-
10390 //use for test
10491 public ConcurrentHashMap <String , List <InetAddress >> getConcurrentHashMap () {
10592 return this .mConcurrentHashMap ;
@@ -147,10 +134,6 @@ private void preFetch(List<String> fetchHost) {
147134 try {
148135 inetAddresses = okhttp3 .Dns .SYSTEM .lookup (host );
149136 mConcurrentHashMap .put (host , inetAddresses );
150- synchronized (this ) {
151- if (mHostSets .add (host ))
152- mHosts .add (host );//ArrayIndexOutOfBoundsException fix by synchronized,set保证元素不重复
153- }
154137 } catch (UnknownHostException e ) {
155138 e .printStackTrace ();
156139 rePreHosts .add (host );
@@ -186,10 +169,6 @@ private boolean rePreFetch(String host, Dns customeDns) {
186169 inetAddresses = customeDns .lookup (host );
187170 }
188171 mConcurrentHashMap .put (host , inetAddresses );
189- synchronized (this ) {
190- if (mHostSets .add (host ))
191- mHosts .add (host );
192- }
193172 return true ;
194173 } catch (UnknownHostException e ) {
195174 e .printStackTrace ();
@@ -206,14 +185,17 @@ private boolean rePreFetch(String host, Dns customeDns) {
206185 */
207186 public void dnsPreByCustom (Dns dns ) {
208187 List <String > rePreHosts = new ArrayList <String >();
209- for (String host : mHosts ) {
210- List <InetAddress > inetAddresses = null ;
211- try {
212- inetAddresses = dns .lookup (host );
213- mConcurrentHashMap .put (host , inetAddresses );
214- } catch (UnknownHostException e ) {
215- e .printStackTrace ();
216- rePreHosts .add (host );
188+ if (mConcurrentHashMap != null && mConcurrentHashMap .size () > 0 ) {
189+ ArrayList <String > mHosts = (ArrayList <String >) mConcurrentHashMap .keySet ();
190+ for (String host : mHosts ) {
191+ List <InetAddress > inetAddresses = null ;
192+ try {
193+ inetAddresses = dns .lookup (host );
194+ mConcurrentHashMap .put (host , inetAddresses );
195+ } catch (UnknownHostException e ) {
196+ e .printStackTrace ();
197+ rePreHosts .add (host );
198+ }
217199 }
218200 }
219201 rePreFetch (rePreHosts , dns );
@@ -422,7 +404,6 @@ public static boolean recoverDnsCache(byte[] data) {
422404 continue ;
423405 list .add (tmpkey );
424406 }
425- DnsPrefetcher .getDnsPrefetcher ().setHosts (list );
426407 return false ;
427408 }
428409}
0 commit comments