itinance / react-native-fs

Native filesystem access for react-native
MIT License
4.89k stars 954 forks source link

Reading ByteArray (raw bytes) directly from file #1194

Open santo4ul opened 10 months ago

santo4ul commented 10 months ago

Hi, I'm uploading media files (images, wav files) from my phone to S3 bucket. To upload, I'll need the Byte array. Currently I'm reading in base64, and then decode it back to byteArray and then finally uploading it.

Currently, I'm doing the following,

let base64Data = await RNFS.readFile(fPath, 'base64');
const arrayBuffer = AWS.util.base64.decode(base64Data) 

I'm trying to find a way to avoid the base64.decode() operation.

The Documentation says,

encoding can be one of utf8 (default), ascii, base64. Use base64 for reading binary files.

Question: Is there a way to directly read raw bytes from the file?

Thank you.

foxnes commented 9 months ago

I find that using react-native-quick-base64 to decode base64 is fast enough. I am using the code below to convert buffer into String or WritableArray in native and then get the String or Array in javascript. But it's slower than using base64.

  @ReactMethod
  public void readRaw(String filepath, int length, int position, Promise promise) {
    try {
      InputStream inputStream = getInputStream(filepath);
      byte[] buffer = new byte[length];
      inputStream.skip(position);
      int bytesRead = inputStream.read(buffer, 0, length);

//      // toString
//      StringBuilder resultBuilder = new StringBuilder(buffer.length);
//      for (int i = 0; i < bytesRead; i++) {
//        resultBuilder.append((char) buffer[i]);
//      }
//      promise.resolve(resultBuilder.toString());

      // to Array
      WritableArray resultArr = Arguments.createArray();
      for (int i = 0; i < bytesRead; i++) {
        resultArr.pushInt((int) buffer[i]);
      }
      promise.resolve(resultArr);

    } catch (Exception ex) {
      ex.printStackTrace();
      reject(promise, filepath, ex);
    }
  }
ainnotate commented 9 months ago

Thanks @foxnes for your inputs. We are uploading files over 500MB. I believe totally avoiding base64 encode/encode for 500MB is the right approach.

dante-cervantes-rocketlab commented 5 months ago

Thanks @foxnes for your inputs. We are uploading files over 500MB. I believe totally avoiding base64 encode/encode for 500MB is the right approach.

Hi, have you found a solution?