Skip to content

Commit c816c17

Browse files
committed
Merge pull request #128 from qiniu/develop
Release 6.1.6
2 parents 7bc34c1 + ccd9c7b commit c816c17

File tree

9 files changed

+227
-54
lines changed

9 files changed

+227
-54
lines changed

CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
## CHANGE LOG
22

3+
### v6.1.5
4+
5+
2014-07-04 [#128](https://github.com/qiniu/java-sdk/pull/128)
6+
7+
- [#127] 普通上传前计算文件大小
8+
9+
310
### v6.1.5
411

512
2014-06-10 [#121](https://github.com/qiniu/java-sdk/pull/121)
@@ -9,7 +16,7 @@
916
- [#117] 整理未连接的error code
1017
- [#116] 增加pipeline
1118
- [#112] 增加一个 put 方法
12-
-
19+
1320

1421
### v6.1.4
1522

pom.xml

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,36 @@
1-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
1+
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
33
<modelVersion>4.0.0</modelVersion>
44

55
<groupId>com.qiniu</groupId>
66
<artifactId>sdk</artifactId>
7-
<version>6.0.3</version>
7+
<version>6.1.6</version>
88
<packaging>jar</packaging>
99
<name>java-sdk</name>
10-
<url>http://www.qiniutek.com/</url>
11-
12-
<build>
13-
<plugins>
14-
<plugin>
15-
<groupId>org.apache.maven.plugins</groupId>
16-
<artifactId>maven-compiler-plugin</artifactId>
17-
<configuration>
18-
<source>1.6</source>
19-
<target>1.6</target>
20-
<encoding>utf-8</encoding>
21-
</configuration>
22-
</plugin>
10+
<url>http://www.qiniu.com/</url>
11+
<description> Qiniu Resource (Cloud) Storage SDK demo for Java</description>
12+
<licenses>
13+
<license>
14+
<name>The MIT License</name>
15+
<url>http://opensource.org/licenses/MIT</url>
16+
</license>
17+
</licenses>
2318

19+
<build>
20+
<plugins>
21+
<plugin>
22+
<groupId>org.apache.maven.plugins</groupId>
23+
<artifactId>maven-compiler-plugin</artifactId>
24+
<version>3.1</version>
25+
<configuration>
26+
<source>1.6</source>
27+
<target>1.6</target>
28+
<encoding>utf-8</encoding>
29+
</configuration>
30+
</plugin>
31+
</plugins>
2432

25-
</plugins>
26-
27-
</build>
33+
</build>
2834

2935
<properties>
3036
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

src/main/java/com/qiniu/api/auth/digest/Mac.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public String signRequest(HttpPost post) throws AuthException {
115115
if (entity != null) {
116116
org.apache.http.Header ct = entity.getContentType();
117117
if (ct != null
118-
&& ct.getValue() == "application/x-www-form-urlencoded") {
118+
&& "application/x-www-form-urlencoded".equals(ct.getValue())) {
119119
ByteArrayOutputStream w = new ByteArrayOutputStream();
120120
try {
121121
entity.writeTo(w);

src/main/java/com/qiniu/api/io/IoApi.java

Lines changed: 87 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.File;
44
import java.io.FileInputStream;
5+
import java.io.FileOutputStream;
56
import java.io.InputStream;
67
import java.io.UnsupportedEncodingException;
78
import java.nio.charset.Charset;
@@ -33,6 +34,7 @@ private static PutRet put(String uptoken, String key, File file,
3334
return new PutRet(new CallRet(Config.ERROR_CODE, new Exception(
3435
"File does not exist or not readable.")));
3536
}
37+
extra = extra == null ? new PutExtra() : extra;
3638
MultipartEntity requestEntity = new MultipartEntity();
3739
try {
3840
requestEntity.addPart("token", new StringBody(uptoken));
@@ -85,11 +87,14 @@ private static void setParam(MultipartEntity requestEntity, Map<String, String>
8587
}
8688
}
8789

88-
private static PutRet putStream(String uptoken, String key, InputStream reader,PutExtra extra, String fileName) {
90+
private static PutRet putStream(String uptoken, String key, InputStream reader,
91+
PutExtra extra, long length) {
92+
extra = extra == null ? new PutExtra() : extra;
8993
MultipartEntity requestEntity = new MultipartEntity();
9094
try {
9195
requestEntity.addPart("token", new StringBody(uptoken));
92-
AbstractContentBody inputBody = buildInputStreamBody(reader, extra, fileName != null ? fileName : "null");
96+
String fileName = key != null ? key : "null";
97+
AbstractContentBody inputBody = buildInputStreamBody(reader, extra, fileName, length);
9398
requestEntity.addPart("file", inputBody);
9499
setKey(requestEntity, key);
95100
setParam(requestEntity, extra.params);
@@ -109,26 +114,97 @@ private static PutRet putStream(String uptoken, String key, InputStream reader,P
109114
return new PutRet(ret);
110115
}
111116

112-
private static InputStreamBody buildInputStreamBody(InputStream reader,PutExtra extra, String fileName){
117+
private static AbstractContentBody buildInputStreamBody(InputStream reader,
118+
PutExtra extra, String fileName, final long length){
113119
if(extra.mimeType != null){
114-
return new InputStreamBody(reader, extra.mimeType, fileName);
120+
return new InputStreamBody(reader, extra.mimeType, fileName){
121+
public long getContentLength() {
122+
return length;
123+
}
124+
};
115125
}else{
116-
return new InputStreamBody(reader, fileName);
126+
return new InputStreamBody(reader, fileName){
127+
public long getContentLength() {
128+
return length;
129+
}
130+
};
117131
}
118132
}
119133

120-
public static PutRet put(String uptoken,String key,InputStream reader,PutExtra extra){
121-
return putStream(uptoken,key,reader,extra, null);
134+
135+
private static PutRet putStream0(String uptoken, String key, InputStream reader,
136+
PutExtra extra, long length){
137+
length = length <= 0 ? getLength(reader) : length;
138+
if(length != -1) {
139+
return putStream(uptoken,key,reader,extra, length);
140+
}else{
141+
return toPutFile(uptoken, key, reader, extra);
142+
}
143+
122144
}
123145

124-
public static PutRet put(String uptoken,String key,InputStream reader,PutExtra extra, String fileName){
125-
return putStream(uptoken,key,reader,extra, fileName);
146+
private static long getLength(InputStream is){
147+
try {
148+
return is.available();
149+
} catch (Exception e) {
150+
return -1;
151+
}
126152
}
127153

154+
private static PutRet toPutFile(String uptoken, String key,
155+
InputStream reader, PutExtra extra) {
156+
File file = null;
157+
try{
158+
file = copyToTmpFile(reader);
159+
return put(uptoken, key, file, extra);
160+
}finally{
161+
if(file != null){
162+
try{file.delete();}catch(Exception e){}
163+
}
164+
}
165+
}
166+
167+
168+
private static File copyToTmpFile(InputStream from){
169+
FileOutputStream os = null;
170+
try{
171+
File to = File.createTempFile("qiniu_", ".tmp");
172+
os = new FileOutputStream(to);
173+
byte[] b = new byte[64 * 1024];
174+
int l;
175+
while ((l = from.read(b)) != -1) {
176+
os.write(b, 0, l);
177+
}
178+
os.flush();
179+
return to;
180+
}catch(Exception e){
181+
throw new RuntimeException("create tmp file failed.", e);
182+
}finally{
183+
if (os != null){
184+
try{os.close();}catch(Exception e){}
185+
}
186+
if (from != null){
187+
try{from.close();}catch(Exception e){}
188+
}
189+
}
190+
}
191+
192+
193+
/**
194+
* @param uptoken
195+
* @param key
196+
* @param reader
197+
* @param extra
198+
* @param length 部分流 is.available() == 0,此时可指定内容长度
199+
* @return
200+
*/
201+
public static PutRet Put(String uptoken,String key,InputStream reader,PutExtra extra, long length){
202+
return putStream0(uptoken,key,reader,extra, length);
203+
}
128204

129205
public static PutRet Put(String uptoken,String key,InputStream reader,PutExtra extra)
130206
{
131-
return put(uptoken,key,reader,extra);
207+
return Put(uptoken,key,reader,extra, -1);
132208
}
133209

134210

@@ -171,4 +247,5 @@ private static long getCRC32(File file) throws Exception {
171247
}
172248
return crc;
173249
}
250+
174251
}

src/main/java/com/qiniu/api/net/EncodeUtils.java

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,14 @@
1616
/**
1717
* URLEncoding is the alternate base64 encoding defined in RFC 4648. It is
1818
* typically used in URLs and file names.
19-
*
19+
*
2020
*/
2121
public class EncodeUtils {
2222

2323
public static byte[] urlsafeEncodeBytes(byte[] src) {
24-
if (src.length % 3 == 0) {
25-
return encodeBase64Ex(src);
26-
}
27-
28-
byte[] b = encodeBase64Ex(src);
29-
if (b.length % 4 == 0) {
30-
return b;
31-
}
32-
33-
int pad = 4 - b.length % 4;
34-
byte[] b2 = new byte[b.length + pad];
35-
System.arraycopy(b, 0, b2, 0, b.length);
36-
b2[b.length] = '=';
37-
if (pad > 1) {
38-
b2[b.length + 1] = '=';
39-
}
40-
return b2;
24+
return encodeBase64Ex(src);
4125
}
42-
26+
4327
public static byte[] urlsafeBase64Decode(String encoded){
4428
byte[] rawbs = toByte(encoded);
4529
for(int i=0;i<rawbs.length;i++){
@@ -51,7 +35,7 @@ public static byte[] urlsafeBase64Decode(String encoded){
5135
}
5236
return Base64.decodeBase64(rawbs);
5337
}
54-
38+
5539
public static String urlsafeEncodeString(byte[] src) {
5640
return toString(urlsafeEncodeBytes(src));
5741
}
@@ -91,15 +75,15 @@ public static String encodeParams(Object params) {
9175
}
9276
return null;
9377
}
94-
78+
9579
public static byte[] toByte(String s){
9680
try {
9781
return s.getBytes(Config.CHARSET);
9882
} catch (UnsupportedEncodingException e) {
9983
throw new RuntimeException(e);
10084
}
10185
}
102-
86+
10387
public static String toString(byte[] bs){
10488
try {
10589
return new String(bs, Config.CHARSET);

src/main/java/com/qiniu/api/resumableio/StreamSliceUpload.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ public StreamSliceUpload(InputStream is,
3030
@Override
3131
protected boolean hasNext() {
3232
try {
33-
return is != null && is.available() > 0;
33+
// 部分流 is.available() == 0,此时通过设定的内容长度判断,
34+
return is != null && (is.available() > 0 || currentBlockIdx * BLOCK_SIZE < contentLength);
3435
} catch (IOException e) {
3536
return false;
3637
}
@@ -39,6 +40,7 @@ protected boolean hasNext() {
3940
@Override
4041
protected UploadBlock buildNextBlockUpload() throws IOException {
4142
long left = is.available();
43+
left = left > 0 ? left : (contentLength - currentBlockIdx * BLOCK_SIZE);
4244
long start = currentBlockIdx * BLOCK_SIZE;
4345
int len = (int) Math.min(BLOCK_SIZE, left);
4446

src/test/java/com/qiniu/testing/HttpClientTimeOutTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public void testCONNECTION_TIMEOUT() {
3838
Config.SO_TIMEOUT = 20 * 1000;
3939

4040
HttpClient client = Http.getClient();
41+
tearDown();
42+
4143
HttpGet httpget = new HttpGet("http://www.qiniu.com");
4244

4345
s = System.currentTimeMillis();
@@ -62,6 +64,8 @@ public void testSO_TIMEOUT() {
6264
Config.SO_TIMEOUT = 5;
6365

6466
HttpClient client = Http.getClient();
67+
tearDown();
68+
6569
HttpGet httpget = new HttpGet("http://www.qiniu.com");
6670

6771
s = System.currentTimeMillis();

0 commit comments

Comments
 (0)