3

When using a file descriptor in native code, which has been obtained from a ParcelFileDescriptor, should the native side use fclose after an fdopen?

The documentation states that the ParcelaleFileDescriptor instance owns the file descriptor, and it should be closed through it. I don't know the impact of closing the native FILE pointer before closing the file descriptor.

Pseudocode:

1) JAVA:
    // Obtain the file descriptor and pass it to the native method
    ParcelFileDescriptor descriptor  = contentResolver.openFileDescriptor(uri, "w");
    nativeMethod(descriptor.getFD());

2) C++:
    void nativeMethod(const int fd)
    {
        FILE* fp = fdopen(fd, "wb");    

        ..... // do something

        fclose(fp); // Close the file pointer <-- Is this valid?
    }

3) JAVA:
    // Back from the native method, close the file descriptor
    descriptor.close();
PerracoLabs
  • 16,449
  • 15
  • 74
  • 127
  • *I don't know the impact of closing the native FILE pointer before closing the file descriptor.* Test it and find out? Or just use `write()`/`read()` on the file descriptor directly. – Andrew Henle Aug 26 '18 at 12:03
  • was the write ok, i am just getting -1 for write in native side ? – Amruth A Jan 25 '23 at 09:33
  • https://stackoverflow.com/questions/2423628/whats-the-difference-between-a-file-descriptor-and-file-pointer – Agent_L Jan 25 '23 at 10:23
  • how will this work, pid of java app is differnt from native app , how will native app access java fd ? – Amruth A Feb 01 '23 at 10:10

0 Answers0