Skip to content

Commit 2b31fc3

Browse files
committed
Merge pull request #35 from chzyer/feature/v6
fix bugs and close idle connections
2 parents df392df + e500fe1 commit 2b31fc3

File tree

5 files changed

+55
-38
lines changed

5 files changed

+55
-38
lines changed

src/com/qiniu/auth/Client.java

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.apache.http.util.EntityUtils;
2323

2424
import java.io.IOException;
25+
import java.util.concurrent.TimeUnit;
2526

2627
public class Client {
2728

@@ -90,22 +91,16 @@ protected Object doInBackground(Object... objects) {
9091
try {
9192
HttpResponse resp = roundtrip(mHttpRequest);
9293
int statusCode = resp.getStatusLine().getStatusCode();
93-
if (statusCode == 401) { // android 2.3 will not response
94-
return new Exception("unauthorized!");
95-
}
96-
byte[] data = EntityUtils.toByteArray(resp.getEntity());
94+
String xl = resp.getFirstHeader("X-Log").getValue();
95+
96+
if (statusCode == 401) return new Exception("unauthorized!"); // android 2.3 will not response
97+
if (xl.contains("invalid BlockCtx")) return new Exception(xl);
9798

98-
if (statusCode / 100 != 2) {
99-
if (data.length == 0) {
100-
String xlog = resp.getFirstHeader("X-Log").getValue();
101-
if (xlog.length() > 0) {
102-
return new Exception(xlog);
103-
}
104-
return new Exception(resp.getStatusLine().getReasonPhrase());
105-
}
106-
return new Exception(new String(data));
107-
}
108-
return data;
99+
byte[] data = EntityUtils.toByteArray(resp.getEntity());
100+
if (statusCode / 100 == 2) return data;
101+
if (data.length > 0) return new Exception(new String(data));
102+
if (xl.length() > 0) return new Exception(xl);
103+
return new Exception(resp.getStatusLine().getStatusCode() + ":" + resp.getStatusLine().getReasonPhrase());
109104
} catch (IOException e) {
110105
e.printStackTrace();
111106
return e;
@@ -119,6 +114,7 @@ protected void onProgressUpdate(Object... values) {
119114

120115
@Override
121116
protected void onPostExecute(Object o) {
117+
mClient.getConnectionManager().closeIdleConnections(30, TimeUnit.SECONDS);
122118
if (o instanceof Exception) {
123119
mRet.onFailure((Exception) o);
124120
return;

src/com/qiniu/io/IO.java

Lines changed: 12 additions & 2 deletions
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,9 +45,18 @@ 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 + "");
49-
for (Map.Entry<String, String> i: extra.params.entrySet()) m.addField(i.getKey(), i.getValue());
57+
for (Map.Entry<String, String> i: extra.params.entrySet()) {
58+
m.addField(i.getKey(), i.getValue());
59+
}
5060

5161
m.addField("token", mUptoken);
5262
m.addFile("file", extra.mimeType, key == null ? "?" : key, isa);

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: 15 additions & 18 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,34 +120,31 @@ 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 {
142137
if (mFileStream == null) return null;
138+
long fileLength = mFileStream.length();
139+
if (length + offset > fileLength) length = (int) (fileLength - offset);
143140
byte[] data = new byte[length];
144141

145142
int read;
146143
int totalRead = 0;
147144
synchronized (data) {
148145
mFileStream.seek(offset);
149146
do {
150-
read = mFileStream.read(data, totalRead, length);
147+
read = mFileStream.read(data, totalRead, length - totalRead);
151148
if (read <= 0) break;
152149
totalRead += read;
153150
} while (length > totalRead);

0 commit comments

Comments
 (0)