48

I read about ParcelFileDescriptor from below link.

http://developer.android.com/reference/android/os/ParcelFileDescriptor.html

but I haven't got any idea from it. What is it? and What can it do?

Can anyone explain me?

sakura
  • 2,249
  • 2
  • 26
  • 39
user829821
  • 563
  • 1
  • 6
  • 8

3 Answers3

24

What it is?

A file descriptor is an object that a process uses to read or write to an open file and open network sockets.

FileDescriptor objects, representing raw Linux file descriptor identifiers, can be written and ParcelFileDescriptor objects returned to operate on the original file descriptor. The returned file descriptor is a dup of the original file descriptor: the object and fd is different, but operating on the same underlying file stream, with the same position.

and What can it do?

Create pipes, create fds from sockets, open/close files.

Reno
  • 33,594
  • 11
  • 89
  • 102
  • 1
    Can I receive the video from socket using PercelFileDescrptor Cause it look as same as pipe to connect between socket and mediaplayer – user829821 Jul 16 '11 at 09:43
  • will you answer this question please? https://stackoverflow.com/questions/58166061/how-to-write-a-file-using-intent-action-create-document – Nasib Sep 30 '19 at 10:35
7

It implements the Parcable API, which allow put it into Bundles and Intents. It may be even possible to send a FileDescriptor between process. (I have not tested this).

Also the VPNService gives you a ParcelFileDescriptor.

plaisthos
  • 6,255
  • 6
  • 35
  • 63
  • 1
    This is great. But my question is how can I find the path for the file (if it's a file) – Bagusflyer Oct 05 '13 at 10:36
  • Probably not with an official API It is just a wrapped fd or in other words just an integer. I don't know of a UNIX API that allows you to get the path of an fd. – plaisthos Oct 09 '13 at 12:39
  • @plaisthos: On Linux (and thus Android) /proc/$PID/fd/$FD is a symlink to the actual file. I'm not sure if this can be relied upon. – mvds Nov 29 '13 at 00:17
  • Binder indeed allows sending file descriptors across process boundaries. That's how the USB service gives a specific USB device to an app without giving it unrestricted access. – Ilya Jan 07 '15 at 13:42
  • 1
    @plaisthos File descriptors are lower-level entities than paths. It may not always be possible to get path which refers to your file descriptor. For example, if the file is moved from one directory to another, then path is changed, but the file descriptor is not. There may be multiple paths pointing to the same file. Paths to files are like links to web pages. – Dzmitry Lazerka Oct 02 '15 at 03:12
2

Whenever a file is opened, the operating system creates an entry to represent this file and stores its information. Each entry is represented by an integer value and this entry is termed as file descriptor. Basically, Java class FileDescriptor provides a handle to the underlying machine-specific structure representing an open file, an open socket, or another source or sink of bytes.

bizimunda
  • 819
  • 2
  • 9
  • 26