diff --git a/oss-android-sdk/src/main/java/com/alibaba/sdk/android/oss/network/NetworkProgressHelper.java b/oss-android-sdk/src/main/java/com/alibaba/sdk/android/oss/network/NetworkProgressHelper.java index 1c72fc97..bafb9971 100644 --- a/oss-android-sdk/src/main/java/com/alibaba/sdk/android/oss/network/NetworkProgressHelper.java +++ b/oss-android-sdk/src/main/java/com/alibaba/sdk/android/oss/network/NetworkProgressHelper.java @@ -1,6 +1,9 @@ package com.alibaba.sdk.android.oss.network; +import android.net.Uri; + +import java.io.File; import java.io.IOException; import java.io.InputStream; @@ -37,10 +40,31 @@ public Response intercept(Chain chain) throws IOException { /** * process request progress */ + @Deprecated public static ProgressTouchableRequestBody addProgressRequestBody(InputStream input, long contentLength, String contentType, ExecutionContext context) { return new ProgressTouchableRequestBody(input, contentLength, contentType, context); } + + public static ProgressTouchableRequestBody addProgressRequestBody(InputStream input, + long contentLength, + String contentType, + ExecutionContext context, + boolean checkCRC64) { + return new ProgressTouchableRequestBody(input, contentLength, contentType, context, checkCRC64); + } + + public static ProgressTouchableRequestBody addProgressRequestBody(byte[] content, String contentType, ExecutionContext context, boolean checkCRC64) { + return new ProgressTouchableRequestBody(content, contentType, context, checkCRC64); + } + + public static ProgressTouchableRequestBody addProgressRequestBody(String filePath, String contentType, ExecutionContext context, boolean checkCRC64) { + return new ProgressTouchableRequestBody(filePath, contentType, context, checkCRC64); + } + + public static ProgressTouchableRequestBody addProgressRequestBody(Uri fileUri, String contentType, ExecutionContext context, boolean checkCRC64) throws IOException { + return new ProgressTouchableRequestBody(fileUri, contentType, context, checkCRC64); + } } diff --git a/oss-android-sdk/src/main/java/com/alibaba/sdk/android/oss/network/OSSRequestTask.java b/oss-android-sdk/src/main/java/com/alibaba/sdk/android/oss/network/OSSRequestTask.java index e4c01056..7980e774 100644 --- a/oss-android-sdk/src/main/java/com/alibaba/sdk/android/oss/network/OSSRequestTask.java +++ b/oss-android-sdk/src/main/java/com/alibaba/sdk/android/oss/network/OSSRequestTask.java @@ -116,48 +116,23 @@ public T call() throws Exception { case POST: case PUT: OSSUtils.assertTrue(contentType != null, "Content type can't be null when upload!"); - InputStream inputStream = null; - String stringBody = null; - long length = 0; if (message.getUploadData() != null) { - inputStream = new ByteArrayInputStream(message.getUploadData()); - length = message.getUploadData().length; + requestBuilder = requestBuilder.method(message.getMethod().toString(), + NetworkProgressHelper.addProgressRequestBody(message.getUploadData(), contentType, context, message.isCheckCRC64())); } else if (message.getUploadFilePath() != null) { - File file = new File(message.getUploadFilePath()); - inputStream = new FileInputStream(file); - length = file.length(); - if (length <= 0) { - throw new ClientException("the length of file is 0!"); - } + requestBuilder = requestBuilder.method(message.getMethod().toString(), + NetworkProgressHelper.addProgressRequestBody(message.getUploadFilePath(), contentType, context, message.isCheckCRC64())); } else if (message.getUploadUri() != null) { - inputStream = context.getApplicationContext().getContentResolver().openInputStream(message.getUploadUri()); - ParcelFileDescriptor parcelFileDescriptor = null; - try { - parcelFileDescriptor = context.getApplicationContext().getContentResolver().openFileDescriptor(message.getUploadUri(), "r"); - length = parcelFileDescriptor.getStatSize(); - } finally { - if (parcelFileDescriptor != null) { - parcelFileDescriptor.close(); - } - } + requestBuilder = requestBuilder.method(message.getMethod().toString(), + NetworkProgressHelper.addProgressRequestBody(message.getUploadUri(), contentType, context, message.isCheckCRC64())); } else if (message.getContent() != null) { - inputStream = message.getContent(); - length = message.getContentLength(); - } else { - stringBody = message.getStringBody(); - } - - if (inputStream != null) { - if (message.isCheckCRC64()) { - inputStream = new CheckedInputStream(inputStream, new CRC64()); - } - message.setContent(inputStream); - message.setContentLength(length); + InputStream inputStream = message.getContent(); + long length = message.getContentLength(); requestBuilder = requestBuilder.method(message.getMethod().toString(), - NetworkProgressHelper.addProgressRequestBody(inputStream, length, contentType, context)); - } else if (stringBody != null) { + NetworkProgressHelper.addProgressRequestBody(inputStream, length, contentType, context, message.isCheckCRC64())); + } else if (message.getStringBody() != null) { requestBuilder = requestBuilder.method(message.getMethod().toString() - , RequestBody.create(MediaType.parse(contentType), stringBody.getBytes("UTF-8"))); + , RequestBody.create(MediaType.parse(contentType), message.getStringBody().getBytes("UTF-8"))); } else { requestBuilder = requestBuilder.method(message.getMethod().toString() , RequestBody.create(null, new byte[0])); diff --git a/oss-android-sdk/src/main/java/com/alibaba/sdk/android/oss/network/ProgressTouchableRequestBody.java b/oss-android-sdk/src/main/java/com/alibaba/sdk/android/oss/network/ProgressTouchableRequestBody.java index e2d56d84..13d2c50d 100644 --- a/oss-android-sdk/src/main/java/com/alibaba/sdk/android/oss/network/ProgressTouchableRequestBody.java +++ b/oss-android-sdk/src/main/java/com/alibaba/sdk/android/oss/network/ProgressTouchableRequestBody.java @@ -1,10 +1,19 @@ package com.alibaba.sdk.android.oss.network; +import android.net.Uri; +import android.os.ParcelFileDescriptor; + import com.alibaba.sdk.android.oss.callback.OSSProgressCallback; +import com.alibaba.sdk.android.oss.common.utils.CRC64; import com.alibaba.sdk.android.oss.model.OSSRequest; +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; +import java.util.zip.CheckedInputStream; import okhttp3.MediaType; import okhttp3.RequestBody; @@ -25,6 +34,63 @@ public class ProgressTouchableRequestBody extends RequestB private OSSProgressCallback callback; private T request; + private ExecutionContext context; + + private byte[] content; + private String filePath; + private Uri fileUri; + + private boolean isCheckCRC64 = false; + + public ProgressTouchableRequestBody(byte[] content, String contentType, ExecutionContext context, boolean checkCRC64) { + this.content = content; + this.contentType = contentType; + this.contentLength = content.length; + this.callback = context.getProgressCallback(); + this.request = (T) context.getRequest(); + this.context = context; + this.isCheckCRC64 = checkCRC64; + } + + public ProgressTouchableRequestBody(String filePath, String contentType, ExecutionContext context, boolean checkCRC64) { + this.filePath = filePath; + File file = new File(filePath); + this.contentType = contentType; + this.contentLength = file.length(); + this.callback = context.getProgressCallback(); + this.request = (T) context.getRequest(); + this.context = context; + this.isCheckCRC64 = checkCRC64; + } + + public ProgressTouchableRequestBody(Uri fileUri, String contentType, ExecutionContext context, boolean checkCRC64) throws IOException { + ParcelFileDescriptor parcelFileDescriptor = null; + try { + parcelFileDescriptor = context.getApplicationContext().getContentResolver().openFileDescriptor(fileUri, "r"); + this.contentLength = parcelFileDescriptor.getStatSize(); + } finally { + if (parcelFileDescriptor != null) { + parcelFileDescriptor.close(); + } + } + this.fileUri = fileUri; + this.contentType = contentType; + this.callback = context.getProgressCallback(); + this.request = (T) context.getRequest(); + this.context = context; + this.isCheckCRC64 = checkCRC64; + } + + public ProgressTouchableRequestBody(InputStream input, long contentLength, String contentType, ExecutionContext context, boolean checkCRC64) { + this.inputStream = input; + this.contentType = contentType; + this.contentLength = contentLength; + this.callback = context.getProgressCallback(); + this.request = (T) context.getRequest(); + this.isCheckCRC64 = checkCRC64; + } + + @Deprecated public ProgressTouchableRequestBody(InputStream input, long contentLength, String contentType, ExecutionContext context) { this.inputStream = input; this.contentType = contentType; @@ -45,7 +111,25 @@ public long contentLength() throws IOException { @Override public void writeTo(BufferedSink sink) throws IOException { - Source source = Okio.source(this.inputStream); + InputStream inputStream = null; + if (content != null) { + inputStream = new ByteArrayInputStream(content); + } else if (filePath != null) { + File file = new File(filePath); + inputStream = new FileInputStream(file); + } else if (fileUri != null) { + ParcelFileDescriptor parcelFileDescriptor = null; + inputStream = context.getApplicationContext().getContentResolver().openInputStream(fileUri); + } else if (this.inputStream != null) { + inputStream = this.inputStream; + } + if (inputStream == null) { + return; + } + if (isCheckCRC64) { + inputStream = new CheckedInputStream(inputStream, new CRC64()); + } + Source source = Okio.source(inputStream); long total = 0; long read, toRead, remain;