Няма описание

RNFetchBlobFileResp.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package com.RNFetchBlob.Response;
  2. import androidx.annotation.NonNull;
  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 != null && !parent.exists() && !parent.mkdirs()){
  44. throw new IllegalStateException("Couldn't create dir: " + parent);
  45. }
  46. if(!f.exists())
  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. public boolean isDownloadComplete() {
  60. return bytesDownloaded == contentLength();
  61. }
  62. @Override
  63. public BufferedSource source() {
  64. ProgressReportingSource countable = new ProgressReportingSource();
  65. return Okio.buffer(countable);
  66. }
  67. private class ProgressReportingSource implements Source {
  68. @Override
  69. public long read(@NonNull Buffer sink, long byteCount) throws IOException {
  70. try {
  71. byte[] bytes = new byte[(int) byteCount];
  72. long read = originalBody.byteStream().read(bytes, 0, (int) byteCount);
  73. bytesDownloaded += read > 0 ? read : 0;
  74. if (read > 0) {
  75. ofStream.write(bytes, 0, (int) read);
  76. }
  77. RNFetchBlobProgressConfig reportConfig = RNFetchBlobReq.getReportProgress(mTaskId);
  78. if (reportConfig != null && contentLength() != 0 &&reportConfig.shouldReport(bytesDownloaded / contentLength())) {
  79. WritableMap args = Arguments.createMap();
  80. args.putString("taskId", mTaskId);
  81. args.putString("written", String.valueOf(bytesDownloaded));
  82. args.putString("total", String.valueOf(contentLength()));
  83. rctContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
  84. .emit(RNFetchBlobConst.EVENT_PROGRESS, args);
  85. }
  86. return read;
  87. } catch(Exception ex) {
  88. return -1;
  89. }
  90. }
  91. @Override
  92. public Timeout timeout() {
  93. return null;
  94. }
  95. @Override
  96. public void close() throws IOException {
  97. ofStream.close();
  98. }
  99. }
  100. }