No Description

RNFetchBlobFileResp.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package com.RNFetchBlob.Response;
  2. import android.util.Log;
  3. import com.RNFetchBlob.RNFetchBlobConst;
  4. import com.RNFetchBlob.RNFetchBlobProgressConfig;
  5. import com.RNFetchBlob.RNFetchBlobReq;
  6. import com.facebook.react.bridge.Arguments;
  7. import com.facebook.react.bridge.ReactApplicationContext;
  8. import com.facebook.react.bridge.WritableMap;
  9. import com.facebook.react.modules.core.DeviceEventManagerModule;
  10. import java.io.File;
  11. import java.io.FileOutputStream;
  12. import java.io.IOException;
  13. import okhttp3.MediaType;
  14. import okhttp3.ResponseBody;
  15. import okio.Buffer;
  16. import okio.BufferedSource;
  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 RNFetchBlobFileResp extends ResponseBody {
  24. String mTaskId;
  25. ResponseBody originalBody;
  26. String mPath;
  27. long bytesDownloaded = 0;
  28. ReactApplicationContext rctContext;
  29. FileOutputStream ofStream;
  30. public RNFetchBlobFileResp(ReactApplicationContext ctx, String taskId, ResponseBody body, String path, boolean overwrite) throws IOException {
  31. super();
  32. this.rctContext = ctx;
  33. this.mTaskId = taskId;
  34. this.originalBody = body;
  35. assert path != null;
  36. this.mPath = path;
  37. if (path != null) {
  38. boolean appendToExistingFile = !overwrite;
  39. path = path.replace("?append=true", "");
  40. mPath = path;
  41. File f = new File(path);
  42. File parent = f.getParentFile();
  43. if(!parent.exists() && !parent.mkdirs()){
  44. throw new IllegalStateException("Couldn't create dir: " + parent);
  45. }
  46. if(f.exists() == false)
  47. f.createNewFile();
  48. ofStream = new FileOutputStream(new File(path), appendToExistingFile);
  49. }
  50. }
  51. @Override
  52. public MediaType contentType() {
  53. return originalBody.contentType();
  54. }
  55. @Override
  56. public long contentLength() {
  57. return originalBody.contentLength();
  58. }
  59. @Override
  60. public BufferedSource source() {
  61. ProgressReportingSource countable = new ProgressReportingSource();
  62. return Okio.buffer(countable);
  63. }
  64. private class ProgressReportingSource implements Source {
  65. @Override
  66. public long read(Buffer sink, long byteCount) throws IOException {
  67. try {
  68. byte[] bytes = new byte[(int) byteCount];
  69. long read = originalBody.byteStream().read(bytes, 0, (int) byteCount);
  70. bytesDownloaded += read > 0 ? read : 0;
  71. Log.i("bytes downloaded", String.valueOf(byteCount) + "/" + String.valueOf(read) + "=" + String.valueOf(bytesDownloaded));
  72. if (read > 0) {
  73. ofStream.write(bytes, 0, (int) read);
  74. }
  75. RNFetchBlobProgressConfig reportConfig = RNFetchBlobReq.getReportProgress(mTaskId);
  76. if (reportConfig != null && contentLength() != 0 &&reportConfig.shouldReport(bytesDownloaded / contentLength())) {
  77. WritableMap args = Arguments.createMap();
  78. args.putString("taskId", mTaskId);
  79. args.putString("written", String.valueOf(bytesDownloaded));
  80. args.putString("total", String.valueOf(contentLength()));
  81. rctContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
  82. .emit(RNFetchBlobConst.EVENT_PROGRESS, args);
  83. }
  84. return read;
  85. } catch(Exception ex) {
  86. return -1;
  87. }
  88. }
  89. @Override
  90. public Timeout timeout() {
  91. return null;
  92. }
  93. @Override
  94. public void close() throws IOException {
  95. ofStream.close();
  96. }
  97. }
  98. }