Skip to content

Release 6.1.6 #128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Jul 4, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
## CHANGE LOG

### v6.1.5

2014-07-04 [#128](https://github.com/qiniu/java-sdk/pull/128)

- [#127] 普通上传前计算文件大小


### v6.1.5

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


### v6.1.4

Expand Down
44 changes: 25 additions & 19 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
<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">

<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">
<modelVersion>4.0.0</modelVersion>

<groupId>com.qiniu</groupId>
<artifactId>sdk</artifactId>
<version>6.0.3</version>
<version>6.1.6</version>
<packaging>jar</packaging>
<name>java-sdk</name>
<url>http://www.qiniutek.com/</url>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>utf-8</encoding>
</configuration>
</plugin>
<url>http://www.qiniu.com/</url>
<description> Qiniu Resource (Cloud) Storage SDK demo for Java</description>
<licenses>
<license>
<name>The MIT License</name>
<url>http://opensource.org/licenses/MIT</url>
</license>
</licenses>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>utf-8</encoding>
</configuration>
</plugin>
</plugins>

</plugins>

</build>
</build>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/qiniu/api/auth/digest/Mac.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public String signRequest(HttpPost post) throws AuthException {
if (entity != null) {
org.apache.http.Header ct = entity.getContentType();
if (ct != null
&& ct.getValue() == "application/x-www-form-urlencoded") {
&& "application/x-www-form-urlencoded".equals(ct.getValue())) {
ByteArrayOutputStream w = new ByteArrayOutputStream();
try {
entity.writeTo(w);
Expand Down
97 changes: 87 additions & 10 deletions src/main/java/com/qiniu/api/io/IoApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
Expand Down Expand Up @@ -33,6 +34,7 @@ private static PutRet put(String uptoken, String key, File file,
return new PutRet(new CallRet(Config.ERROR_CODE, new Exception(
"File does not exist or not readable.")));
}
extra = extra == null ? new PutExtra() : extra;
MultipartEntity requestEntity = new MultipartEntity();
try {
requestEntity.addPart("token", new StringBody(uptoken));
Expand Down Expand Up @@ -85,11 +87,14 @@ private static void setParam(MultipartEntity requestEntity, Map<String, String>
}
}

private static PutRet putStream(String uptoken, String key, InputStream reader,PutExtra extra, String fileName) {
private static PutRet putStream(String uptoken, String key, InputStream reader,
PutExtra extra, long length) {
extra = extra == null ? new PutExtra() : extra;
MultipartEntity requestEntity = new MultipartEntity();
try {
requestEntity.addPart("token", new StringBody(uptoken));
AbstractContentBody inputBody = buildInputStreamBody(reader, extra, fileName != null ? fileName : "null");
String fileName = key != null ? key : "null";
AbstractContentBody inputBody = buildInputStreamBody(reader, extra, fileName, length);
requestEntity.addPart("file", inputBody);
setKey(requestEntity, key);
setParam(requestEntity, extra.params);
Expand All @@ -109,26 +114,97 @@ private static PutRet putStream(String uptoken, String key, InputStream reader,P
return new PutRet(ret);
}

private static InputStreamBody buildInputStreamBody(InputStream reader,PutExtra extra, String fileName){
private static AbstractContentBody buildInputStreamBody(InputStream reader,
PutExtra extra, String fileName, final long length){
if(extra.mimeType != null){
return new InputStreamBody(reader, extra.mimeType, fileName);
return new InputStreamBody(reader, extra.mimeType, fileName){
public long getContentLength() {
return length;
}
};
}else{
return new InputStreamBody(reader, fileName);
return new InputStreamBody(reader, fileName){
public long getContentLength() {
return length;
}
};
}
}

public static PutRet put(String uptoken,String key,InputStream reader,PutExtra extra){
return putStream(uptoken,key,reader,extra, null);

private static PutRet putStream0(String uptoken, String key, InputStream reader,
PutExtra extra, long length){
length = length <= 0 ? getLength(reader) : length;
if(length != -1) {
return putStream(uptoken,key,reader,extra, length);
}else{
return toPutFile(uptoken, key, reader, extra);
}

}

public static PutRet put(String uptoken,String key,InputStream reader,PutExtra extra, String fileName){
return putStream(uptoken,key,reader,extra, fileName);
private static long getLength(InputStream is){
try {
return is.available();
} catch (Exception e) {
return -1;
}
}

private static PutRet toPutFile(String uptoken, String key,
InputStream reader, PutExtra extra) {
File file = null;
try{
file = copyToTmpFile(reader);
return put(uptoken, key, file, extra);
}finally{
if(file != null){
try{file.delete();}catch(Exception e){}
}
}
}


private static File copyToTmpFile(InputStream from){
FileOutputStream os = null;
try{
File to = File.createTempFile("qiniu_", ".tmp");
os = new FileOutputStream(to);
byte[] b = new byte[64 * 1024];
int l;
while ((l = from.read(b)) != -1) {
os.write(b, 0, l);
}
os.flush();
return to;
}catch(Exception e){
throw new RuntimeException("create tmp file failed.", e);
}finally{
if (os != null){
try{os.close();}catch(Exception e){}
}
if (from != null){
try{from.close();}catch(Exception e){}
}
}
}


/**
* @param uptoken
* @param key
* @param reader
* @param extra
* @param length 部分流 is.available() == 0,此时可指定内容长度
* @return
*/
public static PutRet Put(String uptoken,String key,InputStream reader,PutExtra extra, long length){
return putStream0(uptoken,key,reader,extra, length);
}

public static PutRet Put(String uptoken,String key,InputStream reader,PutExtra extra)
{
return put(uptoken,key,reader,extra);
return Put(uptoken,key,reader,extra, -1);
}


Expand Down Expand Up @@ -171,4 +247,5 @@ private static long getCRC32(File file) throws Exception {
}
return crc;
}

}
28 changes: 6 additions & 22 deletions src/main/java/com/qiniu/api/net/EncodeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,14 @@
/**
* URLEncoding is the alternate base64 encoding defined in RFC 4648. It is
* typically used in URLs and file names.
*
*
*/
public class EncodeUtils {

public static byte[] urlsafeEncodeBytes(byte[] src) {
if (src.length % 3 == 0) {
return encodeBase64Ex(src);
}

byte[] b = encodeBase64Ex(src);
if (b.length % 4 == 0) {
return b;
}

int pad = 4 - b.length % 4;
byte[] b2 = new byte[b.length + pad];
System.arraycopy(b, 0, b2, 0, b.length);
b2[b.length] = '=';
if (pad > 1) {
b2[b.length + 1] = '=';
}
return b2;
return encodeBase64Ex(src);
}

public static byte[] urlsafeBase64Decode(String encoded){
byte[] rawbs = toByte(encoded);
for(int i=0;i<rawbs.length;i++){
Expand All @@ -51,7 +35,7 @@ public static byte[] urlsafeBase64Decode(String encoded){
}
return Base64.decodeBase64(rawbs);
}

public static String urlsafeEncodeString(byte[] src) {
return toString(urlsafeEncodeBytes(src));
}
Expand Down Expand Up @@ -91,15 +75,15 @@ public static String encodeParams(Object params) {
}
return null;
}

public static byte[] toByte(String s){
try {
return s.getBytes(Config.CHARSET);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}

public static String toString(byte[] bs){
try {
return new String(bs, Config.CHARSET);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public StreamSliceUpload(InputStream is,
@Override
protected boolean hasNext() {
try {
return is != null && is.available() > 0;
// 部分流 is.available() == 0,此时通过设定的内容长度判断,
return is != null && (is.available() > 0 || currentBlockIdx * BLOCK_SIZE < contentLength);
} catch (IOException e) {
return false;
}
Expand All @@ -39,6 +40,7 @@ protected boolean hasNext() {
@Override
protected UploadBlock buildNextBlockUpload() throws IOException {
long left = is.available();
left = left > 0 ? left : (contentLength - currentBlockIdx * BLOCK_SIZE);
long start = currentBlockIdx * BLOCK_SIZE;
int len = (int) Math.min(BLOCK_SIZE, left);

Expand Down
4 changes: 4 additions & 0 deletions src/test/java/com/qiniu/testing/HttpClientTimeOutTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public void testCONNECTION_TIMEOUT() {
Config.SO_TIMEOUT = 20 * 1000;

HttpClient client = Http.getClient();
tearDown();

HttpGet httpget = new HttpGet("http://www.qiniu.com");

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

HttpClient client = Http.getClient();
tearDown();

HttpGet httpget = new HttpGet("http://www.qiniu.com");

s = System.currentTimeMillis();
Expand Down
Loading