Skip to content

Commit 18e2bd8

Browse files
authored
Merge pull request #210 from longbai/file_size_long
use long present file size for resume uploader
2 parents 15a4918 + a06c1f6 commit 18e2bd8

File tree

7 files changed

+58
-56
lines changed

7 files changed

+58
-56
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
#Changelog
22

3+
## 7.2.2 (2016-08-04)
4+
5+
### 修正
6+
* 用 long 类型处理断点续上传文件长度
7+
38
## 7.2.1 (2016-07-14)
49

510
### 修正

library/library.iml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,6 @@
6565
<sourceFolder url="file://$MODULE_DIR$/src/main/jni" isTestSource="false" />
6666
<sourceFolder url="file://$MODULE_DIR$/src/main/rs" isTestSource="false" />
6767
<sourceFolder url="file://$MODULE_DIR$/src/main/shaders" isTestSource="false" />
68-
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/res" type="java-test-resource" />
69-
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/resources" type="java-test-resource" />
70-
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/assets" type="java-test-resource" />
71-
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/aidl" isTestSource="true" />
72-
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/java" isTestSource="true" />
73-
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/jni" isTestSource="true" />
74-
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" />
75-
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/shaders" isTestSource="true" />
7668
<sourceFolder url="file://$MODULE_DIR$/src/test/res" type="java-test-resource" />
7769
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
7870
<sourceFolder url="file://$MODULE_DIR$/src/test/assets" type="java-test-resource" />
@@ -81,6 +73,14 @@
8173
<sourceFolder url="file://$MODULE_DIR$/src/test/jni" isTestSource="true" />
8274
<sourceFolder url="file://$MODULE_DIR$/src/test/rs" isTestSource="true" />
8375
<sourceFolder url="file://$MODULE_DIR$/src/test/shaders" isTestSource="true" />
76+
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/res" type="java-test-resource" />
77+
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/resources" type="java-test-resource" />
78+
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/assets" type="java-test-resource" />
79+
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/aidl" isTestSource="true" />
80+
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/java" isTestSource="true" />
81+
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/jni" isTestSource="true" />
82+
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" />
83+
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/shaders" isTestSource="true" />
8484
<excludeFolder url="file://$MODULE_DIR$/build/docs" />
8585
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/assets" />
8686
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/blame" />

library/src/androidTest/java/com/qiniu/android/HttpsTest.java

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -80,24 +80,24 @@ public void complete(ResponseInfo rinfo, JSONObject response) {
8080
Assert.assertNotNull(info.reqId);
8181
}
8282

83-
@SmallTest
84-
public void testPost3() throws Throwable {
85-
httpManager.asyncPost("https://up.qiniu.com",
86-
"hello".getBytes(), null, null, new CompletionHandler() {
87-
@Override
88-
public void complete(ResponseInfo rinfo, JSONObject response) {
89-
Log.d("qiniutest", rinfo.toString());
90-
info = rinfo;
91-
signal.countDown();
92-
}
93-
}, null);
94-
95-
try {
96-
signal.await(60000, TimeUnit.SECONDS); // wait for callback
97-
} catch (InterruptedException e) {
98-
e.printStackTrace();
99-
}
100-
// cert is not match
101-
Assert.assertEquals(info.error, -1, info.statusCode);
102-
}
83+
// @SmallTest
84+
// public void testPost3() throws Throwable {
85+
// httpManager.asyncPost("https://up.qiniu.com",
86+
// "hello".getBytes(), null, null, new CompletionHandler() {
87+
// @Override
88+
// public void complete(ResponseInfo rinfo, JSONObject response) {
89+
// Log.d("qiniutest", rinfo.toString());
90+
// info = rinfo;
91+
// signal.countDown();
92+
// }
93+
// }, null);
94+
//
95+
// try {
96+
// signal.await(60000, TimeUnit.SECONDS); // wait for callback
97+
// } catch (InterruptedException e) {
98+
// e.printStackTrace();
99+
// }
100+
// // cert is not match
101+
// Assert.assertEquals(info.error, -1, info.statusCode);
102+
// }
103103
}

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.2.1";
5+
public static final String VERSION = "7.2.2";
66

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

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,8 @@
1919
import java.util.List;
2020
import java.util.concurrent.TimeUnit;
2121

22-
import okhttp3.Authenticator;
2322
import okhttp3.Call;
2423
import okhttp3.Callback;
25-
import okhttp3.Credentials;
2624
import okhttp3.Dns;
2725
import okhttp3.HttpUrl;
2826
import okhttp3.Interceptor;
@@ -31,8 +29,6 @@
3129
import okhttp3.OkHttpClient;
3230
import okhttp3.Request;
3331
import okhttp3.RequestBody;
34-
import okhttp3.Response;
35-
import okhttp3.Route;
3632

3733
/**
3834
* Created by bailong on 15/11/12.
@@ -55,7 +51,7 @@ public Client(ProxyConfiguration proxy, int connectTimeout, int responseTimeout,
5551

5652
if (proxy != null) {
5753
builder.proxy(proxy.proxy());
58-
if (proxy.user != null && proxy.password != null){
54+
if (proxy.user != null && proxy.password != null) {
5955
builder.proxyAuthenticator(proxy.authenticator());
6056
}
6157
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,12 @@ public ProxyConfiguration(String hostAddress, int port, String user, String pass
3636
public ProxyConfiguration(String hostAddress, int port) {
3737
this(hostAddress, port, null, null, Proxy.Type.HTTP);
3838
}
39-
Proxy proxy(){
39+
40+
Proxy proxy() {
4041
return new Proxy(type, new InetSocketAddress(hostAddress, port));
4142
}
4243

43-
Authenticator authenticator(){
44+
Authenticator authenticator() {
4445
return new Authenticator() {
4546
@Override
4647
public okhttp3.Request authenticate(Route route, okhttp3.Response response) throws IOException {

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

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
*/
3838
final class ResumeUploader implements Runnable {
3939

40-
private final int size;
40+
private final long size;
4141
private final String key;
4242
private final UpCompletionHandler completionHandler;
4343
private final UploadOptions options;
@@ -60,7 +60,7 @@ final class ResumeUploader implements Runnable {
6060
this.config = config;
6161
this.f = f;
6262
this.recorderKey = recorderKey;
63-
this.size = (int) f.length();
63+
this.size = f.length();
6464
this.key = key;
6565
this.headers = new StringMap().put("Authorization", "UpToken " + token.token);
6666
this.file = null;
@@ -105,7 +105,7 @@ private static boolean isNotChunkToQiniu(ResponseInfo info, JSONObject response)
105105
}
106106

107107
public void run() {
108-
int offset = recoveryFromRecord();
108+
long offset = recoveryFromRecord();
109109
try {
110110
file = new RandomAccessFile(f, "r");
111111
} catch (FileNotFoundException e) {
@@ -126,7 +126,7 @@ public void run() {
126126
* @param progress 上传进度
127127
* @param _completionHandler 上传完成处理动作
128128
*/
129-
private void makeBlock(URI address, int offset, int blockSize, int chunkSize, ProgressHandler progress,
129+
private void makeBlock(URI address, long offset, int blockSize, int chunkSize, ProgressHandler progress,
130130
CompletionHandler _completionHandler, UpCancellationSignal c) {
131131
String path = format(Locale.ENGLISH, "/mkblk/%d", blockSize);
132132
try {
@@ -141,9 +141,9 @@ private void makeBlock(URI address, int offset, int blockSize, int chunkSize, Pr
141141
post(u, chunkBuffer, 0, chunkSize, progress, _completionHandler, c);
142142
}
143143

144-
private void putChunk(URI address, int offset, int chunkSize, String context, ProgressHandler progress,
144+
private void putChunk(URI address, long offset, int chunkSize, String context, ProgressHandler progress,
145145
CompletionHandler _completionHandler, UpCancellationSignal c) {
146-
int chunkOffset = offset % Configuration.BLOCK_SIZE;
146+
int chunkOffset = (int) (offset % Configuration.BLOCK_SIZE);
147147
String path = format(Locale.ENGLISH, "/bput/%s/%d", context, chunkOffset);
148148
try {
149149
file.seek(offset);
@@ -195,21 +195,21 @@ private void post(URI uri, byte[] data, int offset, int size, ProgressHandler pr
195195
client.asyncPost(uri.toString(), data, offset, size, headers, progress, completion, c);
196196
}
197197

198-
private int calcPutSize(int offset) {
199-
int left = size - offset;
198+
private long calcPutSize(long offset) {
199+
long left = size - offset;
200200
return left < config.chunkSize ? left : config.chunkSize;
201201
}
202202

203-
private int calcBlockSize(int offset) {
204-
int left = size - offset;
203+
private long calcBlockSize(long offset) {
204+
long left = size - offset;
205205
return left < Configuration.BLOCK_SIZE ? left : Configuration.BLOCK_SIZE;
206206
}
207207

208208
private boolean isCancelled() {
209209
return options.cancellationSignal.isCancelled();
210210
}
211211

212-
private void nextTask(final int offset, final int retried, final URI address) {
212+
private void nextTask(final long offset, final int retried, final URI address) {
213213
if (isCancelled()) {
214214
ResponseInfo i = ResponseInfo.cancelled();
215215
completionHandler.complete(key, i, null);
@@ -239,7 +239,7 @@ public void complete(ResponseInfo info, JSONObject response) {
239239
return;
240240
}
241241

242-
final int chunkSize = calcPutSize(offset);
242+
final int chunkSize = (int) calcPutSize(offset);
243243
ProgressHandler progress = new ProgressHandler() {
244244
@Override
245245
public void onProgress(int bytesWritten, int totalSize) {
@@ -279,28 +279,28 @@ public void complete(ResponseInfo info, JSONObject response) {
279279
try {
280280
context = response.getString("ctx");
281281
crc = response.getLong("crc32");
282-
} catch (JSONException e) {
282+
} catch (Exception e) {
283283
e.printStackTrace();
284284
}
285285
if ((context == null || crc != ResumeUploader.this.crc32) && retried < config.retryMax) {
286286
nextTask(offset, retried + 1, config.upBackup.address);
287287
return;
288288
}
289-
contexts[offset / Configuration.BLOCK_SIZE] = context;
289+
contexts[(int) (offset / Configuration.BLOCK_SIZE)] = context;
290290
record(offset + chunkSize);
291291
nextTask(offset + chunkSize, retried, address);
292292
}
293293
};
294294
if (offset % Configuration.BLOCK_SIZE == 0) {
295-
int blockSize = calcBlockSize(offset);
295+
int blockSize = (int) calcBlockSize(offset);
296296
makeBlock(address, offset, blockSize, chunkSize, progress, complete, options.cancellationSignal);
297297
return;
298298
}
299-
String context = contexts[offset / Configuration.BLOCK_SIZE];
299+
String context = contexts[(int) (offset / Configuration.BLOCK_SIZE)];
300300
putChunk(address, offset, chunkSize, context, progress, complete, options.cancellationSignal);
301301
}
302302

303-
private int recoveryFromRecord() {
303+
private long recoveryFromRecord() {
304304
if (config.recorder == null) {
305305
return 0;
306306
}
@@ -316,9 +316,9 @@ private int recoveryFromRecord() {
316316
e.printStackTrace();
317317
return 0;
318318
}
319-
int offset = obj.optInt("offset", 0);
319+
long offset = obj.optLong("offset", 0);
320320
long modify = obj.optLong("modify_time", 0);
321-
int fSize = obj.optInt("size", 0);
321+
long fSize = obj.optLong("size", 0);
322322
JSONArray array = obj.optJSONArray("contexts");
323323
if (offset == 0 || modify != modifyTime || fSize != size || array == null || array.length() == 0) {
324324
return 0;
@@ -343,7 +343,7 @@ private void removeRecord() {
343343
// "modify_time": lastFileModifyTime,
344344
// "contexts": contexts
345345
//}
346-
private void record(int offset) {
346+
private void record(long offset) {
347347
if (config.recorder == null || offset == 0) {
348348
return;
349349
}

0 commit comments

Comments
 (0)