Skip to content

Commit

Permalink
Merge pull request #954 from PlayerData/upload-channel
Browse files Browse the repository at this point in the history
Switch to using channels for byte copying.
  • Loading branch information
itinance authored Feb 28, 2021
2 parents 58e50a2 + d8c51cc commit 5cc8afd
Showing 1 changed file with 23 additions and 13 deletions.
36 changes: 23 additions & 13 deletions android/src/main/java/com/rnfs/Uploader.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.WritableByteChannel;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
Expand Down Expand Up @@ -126,39 +129,46 @@ private void upload(UploadParams params, UploadResult result) throws Exception {
connection.connect();

request = new DataOutputStream(connection.getOutputStream());
WritableByteChannel requestChannel = Channels.newChannel(request);

if (!binaryStreamOnly) {
request.writeBytes(metaData);
}

byteSentTotal = 0;
Runtime run = Runtime.getRuntime();

for (ReadableMap map : params.files) {
if (!binaryStreamOnly) {
request.writeBytes(fileHeader[fileCount]);
}

File file = new File(map.getString("filepath"));
int fileLength = (int) file.length();
int bytes_read = 0;
BufferedInputStream bufInput = new BufferedInputStream(new FileInputStream(file));
int buffer_size =(int) Math.ceil(fileLength / 100.f);
if(buffer_size > run.freeMemory() / 10.f) {
buffer_size = (int) Math.ceil(run.freeMemory() / 10.f);
}
byte[] buffer = new byte[buffer_size];
while ((bytes_read = bufInput.read(buffer)) != -1) {
request.write(buffer, 0, bytes_read);

long fileLength = file.length();
long bufferSize = (long) Math.ceil(fileLength / 100.f);
long bytesRead = 0;

FileInputStream fileStream = new FileInputStream(file);
FileChannel fileChannel = fileStream.getChannel();

while (bytesRead < fileLength) {
long transferredBytes = fileChannel.transferTo(bytesRead, bufferSize, requestChannel);
bytesRead += transferredBytes;

if (mParams.onUploadProgress != null) {
byteSentTotal += bytes_read;
byteSentTotal += transferredBytes;
mParams.onUploadProgress.onUploadProgress((int) totalFileLength, byteSentTotal);
}
}

if (!binaryStreamOnly) {
request.writeBytes(crlf);
}

fileCount++;
bufInput.close();
fileStream.close();
}

if (!binaryStreamOnly) {
request.writeBytes(tail);
}
Expand Down

0 comments on commit 5cc8afd

Please sign in to comment.