diff options
author | Dianne Hackborn <hackbod@google.com> | 2011-05-20 10:37:34 -0700 |
---|---|---|
committer | Dianne Hackborn <hackbod@google.com> | 2011-05-20 12:48:22 -0700 |
commit | ea2117bdc03316a9292e2344c6fd157c85c13167 (patch) | |
tree | f1bc6b8428311c333b94d08e58c17ede1b2e54d5 /core/java | |
parent | 69cb87576ba163b61bb0e6477a3b7c57a9b11d40 (diff) | |
download | frameworks_base-ea2117bdc03316a9292e2344c6fd157c85c13167.zip frameworks_base-ea2117bdc03316a9292e2344c6fd157c85c13167.tar.gz frameworks_base-ea2117bdc03316a9292e2344c6fd157c85c13167.tar.bz2 |
Add ParcelFileDescriptor.fromFd() and .adoptFd().
Change-Id: I2fe0429188dc80abaa0c8977f2e43a010e0f4da2
Diffstat (limited to 'core/java')
-rw-r--r-- | core/java/android/os/ParcelFileDescriptor.java | 56 |
1 files changed, 46 insertions, 10 deletions
diff --git a/core/java/android/os/ParcelFileDescriptor.java b/core/java/android/os/ParcelFileDescriptor.java index aa959b4..0f1354b 100644 --- a/core/java/android/os/ParcelFileDescriptor.java +++ b/core/java/android/os/ParcelFileDescriptor.java @@ -128,7 +128,46 @@ public class ParcelFileDescriptor implements Parcelable { } /** - * Create a new ParcelFileDescriptor from the specified Socket. + * Create a new ParcelFileDescriptor from a raw native fd. The new + * ParcelFileDescriptor holds a dup of the original fd passed in here, + * so you must still close that fd as well as the new ParcelFileDescriptor. + * + * @param fd The native fd that the ParcelFileDescriptor should dup. + * + * @return Returns a new ParcelFileDescriptor holding a FileDescriptor + * for a dup of the given fd. + */ + public static ParcelFileDescriptor fromFd(int fd) throws IOException { + FileDescriptor fdesc = getFileDescriptorFromFd(fd); + return new ParcelFileDescriptor(fdesc); + } + + // Extracts the file descriptor from the specified socket and returns it untouched + private static native FileDescriptor getFileDescriptorFromFd(int fd) throws IOException; + + /** + * Take ownership of a raw native fd in to a new ParcelFileDescriptor. + * The returned ParcelFileDescriptor now owns the given fd, and will be + * responsible for closing it. You must not close the fd yourself. + * + * @param fd The native fd that the ParcelFileDescriptor should adopt. + * + * @return Returns a new ParcelFileDescriptor holding a FileDescriptor + * for the given fd. + */ + public static ParcelFileDescriptor adoptFd(int fd) { + FileDescriptor fdesc = getFileDescriptorFromFdNoDup(fd); + return new ParcelFileDescriptor(fdesc); + } + + // Extracts the file descriptor from the specified socket and returns it untouched + private static native FileDescriptor getFileDescriptorFromFdNoDup(int fd); + + /** + * Create a new ParcelFileDescriptor from the specified Socket. The new + * ParcelFileDescriptor holds a dup of the original FileDescriptor in + * the Socket, so you must still close the Socket as well as the new + * ParcelFileDescriptor. * * @param socket The Socket whose FileDescriptor is used to create * a new ParcelFileDescriptor. @@ -151,17 +190,14 @@ public class ParcelFileDescriptor implements Parcelable { */ public static ParcelFileDescriptor[] createPipe() throws IOException { FileDescriptor[] fds = new FileDescriptor[2]; - int res = createPipeNative(fds); - if (res == 0) { - ParcelFileDescriptor[] pfds = new ParcelFileDescriptor[2]; - pfds[0] = new ParcelFileDescriptor(fds[0]); - pfds[1] = new ParcelFileDescriptor(fds[1]); - return pfds; - } - throw new IOException("Unable to create pipe: errno=" + -res); + createPipeNative(fds); + ParcelFileDescriptor[] pfds = new ParcelFileDescriptor[2]; + pfds[0] = new ParcelFileDescriptor(fds[0]); + pfds[1] = new ParcelFileDescriptor(fds[1]); + return pfds; } - private static native int createPipeNative(FileDescriptor[] outFds); + private static native void createPipeNative(FileDescriptor[] outFds) throws IOException; /** * @hide Please use createPipe() or ContentProvider.openPipeHelper(). |