No Description

RNFetchBlobFileResp.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package com.RNFetchBlob.Response;
  2. import android.support.annotation.NonNull;
  3. import android.util.Log;
  4. import com.RNFetchBlob.RNFetchBlobConst;
  5. import com.RNFetchBlob.RNFetchBlobProgressConfig;
  6. import com.RNFetchBlob.RNFetchBlobReq;
  7. import com.facebook.react.bridge.Arguments;
  8. import com.facebook.react.bridge.ReactApplicationContext;
  9. import com.facebook.react.bridge.WritableMap;
  10. import com.facebook.react.modules.core.DeviceEventManagerModule;
  11. import java.io.File;
  12. import java.io.FileOutputStream;
  13. import java.io.IOException;
  14. import okhttp3.MediaType;
  15. import okhttp3.ResponseBody;
  16. import okio.Buffer;
  17. import okio.BufferedSource;
  18. import okio.Okio;
  19. import okio.Source;
  20. import okio.Timeout;
  21. /**
  22. * Created by wkh237 on 2016/7/11.
  23. */
  24. public class RNFetchBlobFileResp extends ResponseBody {
  25. String mTaskId;
  26. ResponseBody originalBody;
  27. String mPath;
  28. long bytesDownloaded = 0;
  29. ReactApplicationContext rctContext;
  30. FileOutputStream ofStream;
  31. public RNFetchBlobFileResp(ReactApplicationContext ctx, String taskId, ResponseBody body, String path, boolean overwrite) throws IOException {
  32. super();
  33. this.rctContext = ctx;
  34. this.mTaskId = taskId;
  35. this.originalBody = body;
  36. assert path != null;
  37. this.mPath = path;
  38. if (path != null) {
  39. boolean appendToExistingFile = !overwrite;
  40. path = path.replace("?append=true", "");
  41. mPath = path;
  42. File f = new File(path);
  43. File parent = f.getParentFile();
  44. if(parent != null && !parent.exists() && !parent.mkdirs()){
  45. throw new IllegalStateException("Couldn't create dir: " + parent);
  46. }
  47. if(!f.exists())
  48. f.createNewFile();
  49. ofStream = new FileOutputStream(new File(path), appendToExistingFile);
  50. }
  51. }
  52. @Override
  53. public MediaType contentType() {
  54. return originalBody.contentType();
  55. }
  56. @Override
  57. public long contentLength() {
  58. return originalBody.contentLength();
  59. }
  60. @Override
  61. public BufferedSource source() {
  62. ProgressReportingSource countable = new ProgressReportingSource();
  63. return Okio.buffer(countable);
  64. }
  65. private class ProgressReportingSource implements Source {
  66. @Override
  67. public long read(@NonNull Buffer sink, long byteCount) throws IOException {
  68. try {
  69. byte[] bytes = new byte[(int) byteCount];
  70. long read = originalBody.byteStream().read(bytes, 0, (int) byteCount);
  71. bytesDownloaded += read > 0 ? read : 0;
  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. }