Skip to content

Commit 7df84b1

Browse files
committed
fix ResumableIO: ignore other exception when upload failure
1 parent fc00c85 commit 7df84b1

File tree

4 files changed

+38
-21
lines changed

4 files changed

+38
-21
lines changed

src/com/qiniu/io/IO.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.json.JSONObject;
1313

1414
import java.io.File;
15+
import java.io.IOException;
1516
import java.net.URI;
1617
import java.net.URISyntaxException;
1718
import java.util.Map;
@@ -44,7 +45,14 @@ private static Client defaultClient() {
4445
public void put(String key, InputStreamAt isa, PutExtra extra, JSONObjectRet ret) {
4546
MultipartEntity m = new MultipartEntity();
4647
if (key != null) m.addField("key", key);
47-
if (extra.checkCrc == PutExtra.AUTO_CRC32) extra.crc32 = isa.crc32();
48+
if (extra.checkCrc == PutExtra.AUTO_CRC32) {
49+
try {
50+
extra.crc32 = isa.crc32();
51+
} catch (IOException e) {
52+
ret.onFailure(e);
53+
return;
54+
}
55+
}
4856
if (extra.checkCrc != PutExtra.UNUSE_CRC32) m.addField("crc32", extra.crc32 + "");
4957
for (Map.Entry<String, String> i: extra.params.entrySet()) m.addField(i.getKey(), i.getValue());
5058

src/com/qiniu/resumableio/ResumableClient.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,24 @@ public void onInit(int flag) {
4747

4848
public void putInit() {
4949
int chunkSize = Math.min(writeNeed, CHUNK_SIZE);
50-
crc32 = input.getCrc32(offset, chunkSize);
50+
try {
51+
crc32 = input.getCrc32(offset, chunkSize);
52+
} catch (IOException e) {
53+
onFailure(e);
54+
return;
55+
}
5156
canceler[0] = mkblk(input, offset, writeNeed, chunkSize, this);
5257
}
5358

5459
public void putNext() {
5560
wrote = putRet.offset;
5661
int remainLength = Math.min((int) (input.length() - offset - putRet.offset), CHUNK_SIZE);
57-
crc32 = input.getCrc32(offset+putRet.offset, remainLength);
62+
try {
63+
crc32 = input.getCrc32(offset+putRet.offset, remainLength);
64+
} catch (IOException e) {
65+
onFailure(e);
66+
return;
67+
}
5868
canceler[0] = bput(putRet.host, input, putRet.ctx, offset, putRet.offset, remainLength, this);
5969
}
6070

src/com/qiniu/resumableio/ResumableIO.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ private synchronized void removeTask(Integer id) {
3636
idCancels.remove(id);
3737
}
3838

39-
public int putAndClose(final String key, final InputStreamAt input, final PutExtra extra, final JSONObjectRet ret) {
39+
private int putAndClose(final String key, final InputStreamAt input, final PutExtra extra, final JSONObjectRet ret) {
4040
return put(key, input, extra, new JSONObjectRet() {
4141
@Override
4242
public void onSuccess(JSONObject obj) {
@@ -121,6 +121,10 @@ public void onProcess(long current, long total) {
121121

122122
@Override
123123
public void onFailure(Exception ex) {
124+
if (failure[0]) {
125+
ex.printStackTrace();
126+
return;
127+
}
124128
if (--retryTime <= 0 || (ex.getMessage() != null && ex.getMessage().contains("Unauthorized"))) {
125129
removeTask(taskId);
126130
failure[0] = true;

src/com/qiniu/utils/InputStreamAt.java

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,14 @@ public InputStreamAt(byte[] data) {
5858
mData = data;
5959
}
6060

61-
public long getCrc32(long offset, int length) {
61+
public long getCrc32(long offset, int length) throws IOException {
6262
CRC32 crc32 = new CRC32();
6363
byte[] data = read(offset, length);
6464
crc32.update(data);
6565
return crc32.getValue();
6666
}
6767

68-
public long crc32() {
68+
public long crc32() throws IOException {
6969
if (mCrc32 >= 0) return mCrc32;
7070
CRC32 crc32 = new CRC32();
7171
long index = 0;
@@ -120,22 +120,17 @@ protected static File storeToFile(Context context, InputStream is) {
120120
}
121121
}
122122

123-
public byte[] read(long offset, int length) {
124-
if (mClosed) return null;
125-
try {
126-
if (mFileStream != null) {
127-
return fileStreamRead(offset, length);
128-
}
129-
if (mData != null) {
130-
byte[] ret = new byte[length];
131-
System.arraycopy(mData, (int) offset, ret, 0, length);
132-
return ret;
133-
}
134-
} catch (IOException e) {
135-
e.printStackTrace();
123+
public byte[] read(long offset, int length) throws IOException {
124+
if (mClosed) throw new IOException("inputStreamAt closed");
125+
if (mFileStream != null) {
126+
return fileStreamRead(offset, length);
136127
}
137-
138-
return null;
128+
if (mData != null) {
129+
byte[] ret = new byte[length];
130+
System.arraycopy(mData, (int) offset, ret, 0, length);
131+
return ret;
132+
}
133+
throw new IOException("inputStreamAt not init");
139134
}
140135

141136
protected byte[] fileStreamRead(long offset, int length) throws IOException {

0 commit comments

Comments
 (0)