설명 없음

RNFetchBlobFileResp.java 3.3KB

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