Skip to content

Commit 79eb019

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

File tree

1 file changed

+31
-22
lines changed

1 file changed

+31
-22
lines changed

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

Lines changed: 31 additions & 22 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
*
@@ -114,7 +111,6 @@ public void deleteBucket(String bucketname) throws QiniuException {
114111
* @return 该空间名下的domain
115112
* @throws QiniuException
116113
*/
117-
118114
public String[] domainList(String bucket) throws QiniuException {
119115
String url = String.format("%s/v6/domain/list?tbl=%s", configuration.apiHost(), bucket);
120116
Response r = get(url);
@@ -176,11 +172,15 @@ public FileListing listFiles(String bucket, String prefix, String marker, int li
176172
* @link http://developer.qiniu.com/kodo/api/stat
177173
*/
178174
public FileInfo stat(String bucket, String fileKey) throws QiniuException {
179-
Response r = rsGet(bucket, String.format("/stat/%s", encodedEntry(bucket, fileKey)));
180-
return r.jsonToObject(FileInfo.class);
175+
Response res = rsGet(bucket, String.format("/stat/%s", encodedEntry(bucket, fileKey)));
176+
if (!res.isOK()) {
177+
throw new QiniuException(res);
178+
}
179+
FileInfo fileInfo = res.jsonToObject(FileInfo.class);
180+
res.close();
181+
return fileInfo;
181182
}
182183

183-
184184
/**
185185
* 删除指定空间、文件名的文件
186186
*
@@ -230,7 +230,6 @@ public Response changeHeaders(String bucket, String key, Map<String, String> hea
230230
return rsPost(bucket, path, null);
231231
}
232232

233-
234233
/**
235234
* 修改文件的类型(普通存储或低频存储)
236235
*
@@ -303,10 +302,13 @@ public Response copy(String fromBucket, String fromFileKey, String toBucket, Str
303302
*/
304303
public void copy(String fromBucket, String fromFileKey, String toBucket, String toFileKey)
305304
throws QiniuException {
306-
copy(fromBucket, fromFileKey, toBucket, toFileKey, false);
305+
Response res = copy(fromBucket, fromFileKey, toBucket, toFileKey, false);
306+
if (!res.isOK()) {
307+
throw new QiniuException(res);
308+
}
309+
res.close();
307310
}
308311

309-
310312
/**
311313
* 移动文件,要求空间在同一账号下
312314
*
@@ -339,7 +341,6 @@ public Response move(String fromBucket, String fromFileKey, String toBucket, Str
339341
return move(fromBucket, fromFileKey, toBucket, toFileKey, false);
340342
}
341343

342-
343344
/**
344345
* 抓取指定地址的文件,以指定名称保存在指定空间
345346
* 要求指定url可访问,大文件不建议使用此接口抓取。可先下载再上传
@@ -366,8 +367,13 @@ public FetchRet fetch(String url, String bucket, String key) throws QiniuExcepti
366367
String resource = UrlSafeBase64.encodeToString(url);
367368
String to = encodedEntry(bucket, key);
368369
String path = String.format("/fetch/%s/to/%s", resource, to);
369-
Response r = ioPost(bucket, path);
370-
return r.jsonToObject(FetchRet.class);
370+
Response res = ioPost(bucket, path);
371+
if (!res.isOK()) {
372+
throw new QiniuException(res);
373+
}
374+
FetchRet fetchRet = res.jsonToObject(FetchRet.class);
375+
res.close();
376+
return fetchRet;
371377
}
372378

373379
/**
@@ -381,7 +387,6 @@ public FetchRet fetch(String url, String bucket, String key) throws QiniuExcepti
381387
* @return Response
382388
* @throws QiniuException
383389
*/
384-
385390
public Response asynFetch(String url, String bucket, String key) throws QiniuException {
386391
String requesturl = configuration.apiHost(auth.accessKey, bucket) + "/sisyphus/fetch";
387392
StringMap stringMap = new StringMap().put("url", url).put("bucket", bucket).putNotNull("key", key);
@@ -446,7 +451,11 @@ public Response checkAsynFetchid(String region, String fetchWorkId) throws Qiniu
446451
public void prefetch(String bucket, String key) throws QiniuException {
447452
String resource = encodedEntry(bucket, key);
448453
String path = String.format("/prefetch/%s", resource);
449-
ioPost(bucket, path);
454+
Response res = ioPost(bucket, path);
455+
if (!res.isOK()) {
456+
throw new QiniuException(res);
457+
}
458+
res.close();
450459
}
451460

452461
/**
@@ -503,35 +512,36 @@ public Response deleteAfterDays(String bucket, String key, int days) throws Qini
503512
public void setBucketAcl(String bucket, AclType acl) throws QiniuException {
504513
String url = String.format("%s/private?bucket=%s&private=%s", configuration.ucHost(), bucket, acl.getType());
505514
Response res = post(url, null);
506-
res.close();
507515
if (!res.isOK()) {
508516
throw new QiniuException(res);
509517
}
518+
res.close();
510519
}
511520

512521
public BucketInfo getBucketInfo(String bucket) throws QiniuException {
513522
String url = String.format("%s/v2/bucketInfo?bucket=%s", configuration.ucHost(), bucket);
514523
Response res = post(url, null);
515524
if (!res.isOK()) {
516-
res.close();
517525
throw new QiniuException(res);
518526
}
519527
BucketInfo info = res.jsonToObject(BucketInfo.class);
528+
res.close();
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);
536+
if (!res.isOK()) {
537+
throw new QiniuException(res);
538+
}
539+
res.close();
528540
}
529541

530-
531542
/*
532543
* 相关请求的方法列表
533544
* */
534-
535545
private Response rsPost(String bucket, String path, byte[] body) throws QiniuException {
536546
check(bucket);
537547
String url = configuration.rsHost(auth.accessKey, bucket) + path;
@@ -592,7 +602,6 @@ public BatchOperations() {
592602
/**
593603
* 添加chgm指令
594604
*/
595-
596605
public BatchOperations addChgmOp(String bucket, String key, String newMimeType) {
597606
String resource = encodedEntry(bucket, key);
598607
String encodedMime = UrlSafeBase64.encodeToString(newMimeType);

0 commit comments

Comments
 (0)