Skip to content
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

Modify the logic of opening a file stream #406

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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]));
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -25,6 +34,63 @@ public class ProgressTouchableRequestBody<T extends OSSRequest> 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;
Expand All @@ -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;

Expand Down