|
16 | 16 | import java.io.FileInputStream;
|
17 | 17 | import java.io.InputStreamReader;
|
18 | 18 | import java.net.HttpURLConnection;
|
| 19 | +import java.nio.channels.Channels; |
| 20 | +import java.nio.channels.FileChannel; |
| 21 | +import java.nio.channels.WritableByteChannel; |
19 | 22 | import java.util.List;
|
20 | 23 | import java.util.Map;
|
21 | 24 | import java.util.concurrent.atomic.AtomicBoolean;
|
@@ -126,39 +129,46 @@ private void upload(UploadParams params, UploadResult result) throws Exception {
|
126 | 129 | connection.connect();
|
127 | 130 |
|
128 | 131 | request = new DataOutputStream(connection.getOutputStream());
|
| 132 | + WritableByteChannel requestChannel = Channels.newChannel(request); |
| 133 | + |
129 | 134 | if (!binaryStreamOnly) {
|
130 | 135 | request.writeBytes(metaData);
|
131 | 136 | }
|
132 | 137 |
|
133 | 138 | byteSentTotal = 0;
|
134 |
| - Runtime run = Runtime.getRuntime(); |
135 | 139 |
|
136 | 140 | for (ReadableMap map : params.files) {
|
137 | 141 | if (!binaryStreamOnly) {
|
138 | 142 | request.writeBytes(fileHeader[fileCount]);
|
139 | 143 | }
|
| 144 | + |
140 | 145 | File file = new File(map.getString("filepath"));
|
141 |
| - int fileLength = (int) file.length(); |
142 |
| - int bytes_read = 0; |
143 |
| - BufferedInputStream bufInput = new BufferedInputStream(new FileInputStream(file)); |
144 |
| - int buffer_size =(int) Math.ceil(fileLength / 100.f); |
145 |
| - if(buffer_size > run.freeMemory() / 10.f) { |
146 |
| - buffer_size = (int) Math.ceil(run.freeMemory() / 10.f); |
147 |
| - } |
148 |
| - byte[] buffer = new byte[buffer_size]; |
149 |
| - while ((bytes_read = bufInput.read(buffer)) != -1) { |
150 |
| - request.write(buffer, 0, bytes_read); |
| 146 | + |
| 147 | + long fileLength = file.length(); |
| 148 | + long bufferSize = (long) Math.ceil(fileLength / 100.f); |
| 149 | + long bytesRead = 0; |
| 150 | + |
| 151 | + FileInputStream fileStream = new FileInputStream(file); |
| 152 | + FileChannel fileChannel = fileStream.getChannel(); |
| 153 | + |
| 154 | + while (bytesRead < fileLength) { |
| 155 | + long transferredBytes = fileChannel.transferTo(bytesRead, bufferSize, requestChannel); |
| 156 | + bytesRead += transferredBytes; |
| 157 | + |
151 | 158 | if (mParams.onUploadProgress != null) {
|
152 |
| - byteSentTotal += bytes_read; |
| 159 | + byteSentTotal += transferredBytes; |
153 | 160 | mParams.onUploadProgress.onUploadProgress((int) totalFileLength, byteSentTotal);
|
154 | 161 | }
|
155 | 162 | }
|
| 163 | + |
156 | 164 | if (!binaryStreamOnly) {
|
157 | 165 | request.writeBytes(crlf);
|
158 | 166 | }
|
| 167 | + |
159 | 168 | fileCount++;
|
160 |
| - bufInput.close(); |
| 169 | + fileStream.close(); |
161 | 170 | }
|
| 171 | + |
162 | 172 | if (!binaryStreamOnly) {
|
163 | 173 | request.writeBytes(tail);
|
164 | 174 | }
|
|
0 commit comments