|
3 | 3 | import org.jetbrains.annotations.NotNull; |
4 | 4 | import org.json.JSONException; |
5 | 5 | import org.json.JSONObject; |
6 | | -import org.monora.coolsocket.core.response.SizeOverflowException; |
7 | 6 | import org.monora.coolsocket.core.session.ActiveConnection; |
8 | 7 | import org.monora.coolsocket.core.session.CancelledException; |
9 | 8 | import org.monora.uprotocol.core.CommunicationBridge; |
@@ -165,7 +164,7 @@ public static void receive(@NotNull CommunicationBridge bridge, @NotNull Transfe |
165 | 164 | /** |
166 | 165 | * Handle the sending process. You can invoke this method via {@link TransportSeat#beginFileTransfer} method when |
167 | 166 | * the type is {@link TransferItem.Type#Outgoing}. |
168 | | - * |
| 167 | + * <p> |
169 | 168 | * This can also be invoked when using {@link CommunicationBridge#requestFileTransferStart}. |
170 | 169 | * |
171 | 170 | * @param bridge The bridge that speaks on behalf of you when making requests. A connection wrapper. |
@@ -218,20 +217,32 @@ public static void send(@NotNull CommunicationBridge bridge, @NotNull TransferOp |
218 | 217 | byte[] bytes = new byte[8096]; |
219 | 218 | int len; |
220 | 219 |
|
221 | | - try { |
222 | | - while ((len = inputStream.read(bytes)) != -1) { |
223 | | - operation.publishProgress(); |
| 220 | + // For avoiding Android MediaStore bug where the reported size is different than actual |
| 221 | + // data size. |
| 222 | + boolean exceedingClose = false; |
| 223 | + long available; |
224 | 224 |
|
225 | | - if (len > 0) { |
226 | | - operation.setBytesOngoing(operation.getBytesOngoing() + len, len); |
227 | | - activeConnection.write(description, bytes, 0, len); |
| 225 | + while ((len = inputStream.read(bytes)) != -1) { |
| 226 | + operation.publishProgress(); |
| 227 | + |
| 228 | + if (len > 0) { |
| 229 | + available = description.available(); |
| 230 | + if (len > available) { |
| 231 | + len = (int) available; |
| 232 | + exceedingClose = true; |
228 | 233 | } |
229 | | - } |
230 | 234 |
|
231 | | - activeConnection.writeEnd(description); |
232 | | - } catch (SizeOverflowException ignored) { |
| 235 | + operation.setBytesOngoing(operation.getBytesOngoing() + len, len); |
| 236 | + activeConnection.write(description, bytes, 0, len); |
| 237 | + |
| 238 | + if (exceedingClose) { |
| 239 | + break; |
| 240 | + } |
| 241 | + } |
233 | 242 | } |
234 | 243 |
|
| 244 | + activeConnection.writeEnd(description); |
| 245 | + |
235 | 246 | operation.setBytesTotal(operation.getBytesTotal() + operation.getBytesOngoing()); |
236 | 247 | operation.setCount(operation.getCount() + 1); |
237 | 248 | operation.clearBytesOngoing(); |
|
0 commit comments