Geen omschrijving

RNFetchBlobFileResp.java 3.0KB

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