Skip to content

Commit cd36b7e

Browse files
author
JemyCheung
committed
safety
1 parent d639dfb commit cd36b7e

File tree

2 files changed

+11
-38
lines changed

2 files changed

+11
-38
lines changed

library/src/main/java/com/qiniu/android/http/DnsPrefetcher.java

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.util.HashSet;
2323
import java.util.Iterator;
2424
import java.util.List;
25-
import java.util.Set;
2625
import java.util.concurrent.ConcurrentHashMap;
2726
import 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
}

library/src/main/java/com/qiniu/android/storage/UploadManager.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -160,16 +160,12 @@ public void put(final byte[] data, final String key, final String token,
160160
}
161161
//此处是对uc.qbox.me接口获取的域名进行预取
162162
if (DnsPrefetcher.checkRePrefetchDns(token, config)) {
163-
try {
164163
new Thread(new Runnable() {
165164
@Override
166165
public void run() {
167166
DnsPrefetcher.startPrefetchDns(token, config);
168167
}
169168
}).start();
170-
} catch (Exception e) {
171-
e.printStackTrace();
172-
}
173169
}
174170

175171
Zone z = config.zone;
@@ -224,16 +220,12 @@ public void put(final File file, final String key, final String token,
224220
}
225221
//此处是每次上传时判断,对uc.qbox.me接口获取的host+sdk内置的host进行预取(去重)
226222
if (DnsPrefetcher.checkRePrefetchDns(token, config)) {
227-
try {
228223
new Thread(new Runnable() {
229224
@Override
230225
public void run() {
231226
DnsPrefetcher.startPrefetchDns(token, config);
232227
}
233228
}).start();
234-
} catch (Exception e) {
235-
e.printStackTrace();
236-
}
237229
}
238230

239231
Zone z = config.zone;

0 commit comments

Comments
 (0)