Skip to content

Commit e4d9c54

Browse files
author
wubingheng
committed
update many rs operations to fix memory leak problem by closing okhttp response in BucketManager.
1 parent 4106938 commit e4d9c54

File tree

1 file changed

+25
-20
lines changed

1 file changed

+25
-20
lines changed

src/main/java/com/qiniu/storage/BucketManager.java

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
import java.util.Iterator;
1212
import java.util.Map;
1313

14-
1514
/**
1615
* 主要涉及了空间资源管理及批量操作接口的实现,具体的接口规格可以参考
1716
* 参考文档:<a href="http://developer.qiniu.com/kodo/api/rs">资源管理</a>
1817
*/
1918
public final class BucketManager {
19+
2020
/**
2121
* Auth 对象
2222
* 该类需要使用QBox鉴权,所以需要指定Auth对象
@@ -27,7 +27,6 @@ public final class BucketManager {
2727
* Configuration 对象
2828
* 该类相关的域名配置,解析配置,HTTP请求超时时间设置等
2929
*/
30-
3130
private Configuration configuration;
3231

3332
/**
@@ -53,7 +52,6 @@ public BucketManager(Auth auth, Client client) {
5352
this.client = client;
5453
}
5554

56-
5755
/**
5856
* EncodedEntryURI格式,其中 bucket+":"+key 称之为 entry
5957
*
@@ -83,7 +81,6 @@ public static String encodedEntry(String bucket) {
8381
return encodedEntry(bucket, null);
8482
}
8583

86-
8784
/**
8885
* 获取账号下所有空间名称列表
8986
*
@@ -92,19 +89,32 @@ public static String encodedEntry(String bucket) {
9289
public String[] buckets() throws QiniuException {
9390
// 获取 bucket 列表 写死用rs.qiniu.com or rs.qbox.me @冯立元
9491
String url = String.format("%s/buckets", configuration.rsHost());
95-
Response r = get(url);
96-
return r.jsonToObject(String[].class);
92+
Response res = get(url);
93+
if (!res.isOK()) {
94+
throw new QiniuException(res);
95+
}
96+
String[] buckets = res.jsonToObject(String[].class);
97+
res.close();
98+
return buckets;
9799
}
98100

99101
public void createBucket(String bucketName, String region) throws QiniuException {
100102
String url = String.format("%s/mkbucketv2/%s/region/%s", configuration.rsHost(),
101103
UrlSafeBase64.encodeToString(bucketName), region);
102-
post(url, null).close();
104+
Response res = post(url, null);
105+
if (!res.isOK()) {
106+
throw new QiniuException(res);
107+
}
108+
res.close();
103109
}
104110

105111
public void deleteBucket(String bucketname) throws QiniuException {
106112
String url = String.format("%s/drop/%s", configuration.rsHost(), bucketname);
107-
post(url, null).close();
113+
Response res = post(url, null);
114+
if (!res.isOK()) {
115+
throw new QiniuException(res);
116+
}
117+
res.close();
108118
}
109119

110120
/**
@@ -114,11 +124,15 @@ public void deleteBucket(String bucketname) throws QiniuException {
114124
* @return 该空间名下的domain
115125
* @throws QiniuException
116126
*/
117-
118127
public String[] domainList(String bucket) throws QiniuException {
119128
String url = String.format("%s/v6/domain/list?tbl=%s", configuration.apiHost(), bucket);
120-
Response r = get(url);
121-
return r.jsonToObject(String[].class);
129+
Response res = get(url);
130+
if (!res.isOK()) {
131+
throw new QiniuException(res);
132+
}
133+
String[] domains = res.jsonToObject(String[].class);
134+
res.close();
135+
return domains;
122136
}
123137

124138
/**
@@ -180,7 +194,6 @@ public FileInfo stat(String bucket, String fileKey) throws QiniuException {
180194
return r.jsonToObject(FileInfo.class);
181195
}
182196

183-
184197
/**
185198
* 删除指定空间、文件名的文件
186199
*
@@ -230,7 +243,6 @@ public Response changeHeaders(String bucket, String key, Map<String, String> hea
230243
return rsPost(bucket, path, null);
231244
}
232245

233-
234246
/**
235247
* 修改文件的类型(普通存储或低频存储)
236248
*
@@ -306,7 +318,6 @@ public void copy(String fromBucket, String fromFileKey, String toBucket, String
306318
copy(fromBucket, fromFileKey, toBucket, toFileKey, false);
307319
}
308320

309-
310321
/**
311322
* 移动文件,要求空间在同一账号下
312323
*
@@ -339,7 +350,6 @@ public Response move(String fromBucket, String fromFileKey, String toBucket, Str
339350
return move(fromBucket, fromFileKey, toBucket, toFileKey, false);
340351
}
341352

342-
343353
/**
344354
* 抓取指定地址的文件,以指定名称保存在指定空间
345355
* 要求指定url可访问,大文件不建议使用此接口抓取。可先下载再上传
@@ -381,7 +391,6 @@ public FetchRet fetch(String url, String bucket, String key) throws QiniuExcepti
381391
* @return Response
382392
* @throws QiniuException
383393
*/
384-
385394
public Response asynFetch(String url, String bucket, String key) throws QiniuException {
386395
String requesturl = configuration.apiHost(auth.accessKey, bucket) + "/sisyphus/fetch";
387396
StringMap stringMap = new StringMap().put("url", url).put("bucket", bucket).putNotNull("key", key);
@@ -520,18 +529,15 @@ public BucketInfo getBucketInfo(String bucket) throws QiniuException {
520529
return info;
521530
}
522531

523-
524532
public void setIndexPage(String bucket, IndexPageType type) throws QiniuException {
525533
String url = String.format("%s/noIndexPage?bucket=%s&noIndexPage=%s",
526534
configuration.ucHost(), bucket, type.getType());
527535
Response res = post(url, null);
528536
}
529537

530-
531538
/*
532539
* 相关请求的方法列表
533540
* */
534-
535541
private Response rsPost(String bucket, String path, byte[] body) throws QiniuException {
536542
check(bucket);
537543
String url = configuration.rsHost(auth.accessKey, bucket) + path;
@@ -592,7 +598,6 @@ public BatchOperations() {
592598
/**
593599
* 添加chgm指令
594600
*/
595-
596601
public BatchOperations addChgmOp(String bucket, String key, String newMimeType) {
597602
String resource = encodedEntry(bucket, key);
598603
String encodedMime = UrlSafeBase64.encodeToString(newMimeType);

0 commit comments

Comments
 (0)