Skip to content

Commit b153cac

Browse files
authored
Merge pull request #360 from JemyCheung/safety
safety code
2 parents 359bdfe + dc0139a commit b153cac

File tree

4 files changed

+29
-45
lines changed

4 files changed

+29
-45
lines changed

android-sdk.iml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<content url="file://$MODULE_DIR$">
1414
<excludeFolder url="file://$MODULE_DIR$/.gradle" />
1515
</content>
16-
<orderEntry type="jdk" jdkName="JDK" jdkType="JavaSDK" />
16+
<orderEntry type="inheritedJdk" />
1717
<orderEntry type="sourceFolder" forTests="false" />
1818
</component>
1919
</module>

library/src/main/java/com/qiniu/android/common/Constants.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33

44
public final class Constants {
5-
public static final String VERSION = "7.5.1";
5+
public static final String VERSION = "7.5.2";
66

77
public static final String UTF_8 = "utf-8";
88
}

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

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.HashSet;
2323
import java.util.Iterator;
2424
import java.util.List;
25+
import java.util.Map;
2526
import java.util.concurrent.ConcurrentHashMap;
2627
import 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
}

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +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-
new Thread(new Runnable() {
164-
@Override
165-
public void run() {
166-
DnsPrefetcher.startPrefetchDns(token, config);
167-
}
168-
}).start();
163+
new Thread(new Runnable() {
164+
@Override
165+
public void run() {
166+
DnsPrefetcher.startPrefetchDns(token, config);
167+
}
168+
}).start();
169169
}
170170

171171
Zone z = config.zone;
@@ -220,12 +220,12 @@ public void put(final File file, final String key, final String token,
220220
}
221221
//此处是每次上传时判断,对uc.qbox.me接口获取的host+sdk内置的host进行预取(去重)
222222
if (DnsPrefetcher.checkRePrefetchDns(token, config)) {
223-
new Thread(new Runnable() {
224-
@Override
225-
public void run() {
226-
DnsPrefetcher.startPrefetchDns(token, config);
227-
}
228-
}).start();
223+
new Thread(new Runnable() {
224+
@Override
225+
public void run() {
226+
DnsPrefetcher.startPrefetchDns(token, config);
227+
}
228+
}).start();
229229
}
230230

231231
Zone z = config.zone;

0 commit comments

Comments
 (0)