Nenhuma descrição

RNFetchBlobFileResp.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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) 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 = path.contains("?append=true");
  39. path = path.replace("?append=true", "");
  40. mPath = path;
  41. File f = new File(path);
  42. if(f.exists() == false)
  43. f.createNewFile();
  44. ofStream = new FileOutputStream(new File(path), appendToExistingFile);
  45. }
  46. }
  47. @Override
  48. public MediaType contentType() {
  49. return originalBody.contentType();
  50. }
  51. @Override
  52. public long contentLength() {
  53. return originalBody.contentLength();
  54. }
  55. @Override
  56. public BufferedSource source() {
  57. ProgressReportingSource countable = new ProgressReportingSource();
  58. return Okio.buffer(countable);
  59. }
  60. private class ProgressReportingSource implements Source {
  61. @Override
  62. public long read(Buffer sink, long byteCount) throws IOException {
  63. byte [] bytes = new byte[(int) byteCount];
  64. long read = originalBody.byteStream().read(bytes, 0, (int) byteCount);
  65. bytesDownloaded += read > 0 ? read : 0;
  66. Log.i("bytes downloaded", String.valueOf(byteCount) +"/"+ String.valueOf(read) + "=" + String.valueOf(bytesDownloaded));
  67. if(read > 0 ) {
  68. ofStream.write(bytes, 0, (int) read);
  69. }
  70. RNFetchBlobProgressConfig reportConfig = RNFetchBlobReq.getReportProgress(mTaskId);
  71. if(reportConfig != null && reportConfig.shouldReport()) {
  72. reportConfig.tick(bytesDownloaded/contentLength());
  73. WritableMap args = Arguments.createMap();
  74. args.putString("taskId", mTaskId);
  75. args.putString("written", String.valueOf(bytesDownloaded));
  76. args.putString("total", String.valueOf(contentLength()));
  77. rctContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
  78. .emit(RNFetchBlobConst.EVENT_PROGRESS, args);
  79. }
  80. return read;
  81. }
  82. @Override
  83. public Timeout timeout() {
  84. return null;
  85. }
  86. @Override
  87. public void close() throws IOException {
  88. ofStream.close();
  89. }
  90. }
  91. }