Açıklama Yok

RNFetchBlobDefaultResp.java 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package com.RNFetchBlob.Response;
  2. import com.RNFetchBlob.RNFetchBlob;
  3. import com.RNFetchBlob.RNFetchBlobReq;
  4. import com.facebook.react.bridge.Arguments;
  5. import com.facebook.react.bridge.ReactApplicationContext;
  6. import com.facebook.react.bridge.WritableMap;
  7. import com.facebook.react.modules.core.DeviceEventManagerModule;
  8. import java.io.IOException;
  9. import okhttp3.Call;
  10. import okhttp3.Callback;
  11. import okhttp3.MediaType;
  12. import okhttp3.Response;
  13. import okhttp3.ResponseBody;
  14. import okio.Buffer;
  15. import okio.BufferedSource;
  16. import okio.ForwardingSource;
  17. import okio.Okio;
  18. import okio.Source;
  19. import okio.Timeout;
  20. /**
  21. * Created by wkh237 on 2016/7/11.
  22. */
  23. public class RNFetchBlobDefaultResp extends ResponseBody {
  24. String mTaskId;
  25. ReactApplicationContext rctContext;
  26. ResponseBody originalBody;
  27. public RNFetchBlobDefaultResp(ReactApplicationContext ctx, String taskId, ResponseBody body) {
  28. this.rctContext = ctx;
  29. this.mTaskId = taskId;
  30. this.originalBody = body;
  31. }
  32. @Override
  33. public MediaType contentType() {
  34. return originalBody.contentType();
  35. }
  36. @Override
  37. public long contentLength() {
  38. return originalBody.contentLength();
  39. }
  40. @Override
  41. public BufferedSource source() {
  42. return Okio.buffer(new ProgressReportingSource(originalBody.source()));
  43. }
  44. private class ProgressReportingSource implements Source {
  45. BufferedSource mOriginalSource;
  46. long bytesRead = 0;
  47. ProgressReportingSource(BufferedSource originalSource) {
  48. mOriginalSource = originalSource;
  49. }
  50. @Override
  51. public long read(Buffer sink, long byteCount) throws IOException {
  52. long read = mOriginalSource.read(sink, byteCount);
  53. bytesRead += read > 0 ? read : 0;
  54. if(RNFetchBlobReq.isReportProgress(mTaskId)) {
  55. WritableMap args = Arguments.createMap();
  56. args.putString("taskId", mTaskId);
  57. args.putString("written", String.valueOf(bytesRead));
  58. args.putString("total", String.valueOf(contentLength()));
  59. rctContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
  60. .emit("RNFetchBlobProgress", args);
  61. }
  62. return read;
  63. }
  64. @Override
  65. public Timeout timeout() {
  66. return null;
  67. }
  68. @Override
  69. public void close() throws IOException {
  70. }
  71. }
  72. }