Geen omschrijving

RNFetchBlobFileResp.java 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package com.RNFetchBlob.Response;
  2. import android.util.Log;
  3. import com.RNFetchBlob.RNFetchBlobReq;
  4. import com.facebook.react.bridge.Arguments;
  5. import com.facebook.react.bridge.ReactApplicationContext;
  6. import com.facebook.react.bridge.WritableMap;
  7. import com.facebook.react.modules.core.DeviceEventManagerModule;
  8. import java.io.File;
  9. import java.io.FileOutputStream;
  10. import java.io.IOException;
  11. import okhttp3.MediaType;
  12. import okhttp3.ResponseBody;
  13. import okio.Buffer;
  14. import okio.BufferedSource;
  15. import okio.Okio;
  16. import okio.Source;
  17. import okio.Timeout;
  18. /**
  19. * Created by wkh237 on 2016/7/11.
  20. */
  21. public class RNFetchBlobFileResp extends ResponseBody {
  22. String mTaskId;
  23. ResponseBody originalBody;
  24. String mPath;
  25. long bytesDownloaded = 0;
  26. ReactApplicationContext rctContext;
  27. FileOutputStream ofStream;
  28. public RNFetchBlobFileResp(ReactApplicationContext ctx, String taskId, ResponseBody body, String path) throws IOException {
  29. super();
  30. this.rctContext = ctx;
  31. this.mTaskId = taskId;
  32. this.originalBody = body;
  33. assert path != null;
  34. this.mPath = path;
  35. if (path != null) {
  36. File f = new File(path);
  37. if(f.exists() == false)
  38. f.createNewFile();
  39. ofStream = new FileOutputStream(new File(path));
  40. }
  41. }
  42. @Override
  43. public MediaType contentType() {
  44. return originalBody.contentType();
  45. }
  46. @Override
  47. public long contentLength() {
  48. return originalBody.contentLength();
  49. }
  50. @Override
  51. public BufferedSource source() {
  52. ProgressReportingSource countable = new ProgressReportingSource();
  53. return Okio.buffer(countable);
  54. }
  55. private class ProgressReportingSource implements Source {
  56. @Override
  57. public long read(Buffer sink, long byteCount) throws IOException {
  58. byte [] bytes = new byte[(int) byteCount];
  59. long read = originalBody.byteStream().read(bytes, 0, (int) byteCount);
  60. bytesDownloaded += read > 0 ? read : 0;
  61. Log.i("bytes downloaded", String.valueOf(byteCount) +"/"+ String.valueOf(read) + "=" + String.valueOf(bytesDownloaded));
  62. if(read > 0 ) {
  63. ofStream.write(bytes, 0, (int) read);
  64. }
  65. if(RNFetchBlobReq.isReportProgress(mTaskId)) {
  66. WritableMap args = Arguments.createMap();
  67. args.putString("taskId", mTaskId);
  68. args.putString("written", String.valueOf(bytesDownloaded));
  69. args.putString("total", String.valueOf(contentLength()));
  70. rctContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
  71. .emit("RNFetchBlobProgress", args);
  72. }
  73. return read;
  74. }
  75. @Override
  76. public Timeout timeout() {
  77. return null;
  78. }
  79. @Override
  80. public void close() throws IOException {
  81. ofStream.close();
  82. }
  83. }
  84. }