From 4a64663c0fa426a01e3053d1820e0177be215fda Mon Sep 17 00:00:00 2001 From: Josh Gao Date: Fri, 8 Mar 2019 11:22:21 -0800 Subject: ParcelFileDescriptor: improve javadoc. Add some warnings to adoptFd, getFd, and from[Datagram]Socket. Bug: http://b/111790184 Test: none Change-Id: I1963e5c28ad6201660c37ee5f3cfe6c8bf0ff392 --- core/java/android/os/ParcelFileDescriptor.java | 29 +++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/core/java/android/os/ParcelFileDescriptor.java b/core/java/android/os/ParcelFileDescriptor.java index a89fc095b403..36b99d28f2a5 100644 --- a/core/java/android/os/ParcelFileDescriptor.java +++ b/core/java/android/os/ParcelFileDescriptor.java @@ -375,7 +375,11 @@ public class ParcelFileDescriptor implements Parcelable, Closeable { /** * 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. + * responsible for closing it. + *

+ * WARNING: You must not close the fd yourself after + * this call, and ownership of the file descriptor must have been + * released prior to the call to this function. * * @param fd The native fd that the ParcelFileDescriptor should adopt. * @@ -394,6 +398,16 @@ public class ParcelFileDescriptor implements Parcelable, Closeable { * ParcelFileDescriptor holds a dup of the original FileDescriptor in * the Socket, so you must still close the Socket as well as the new * ParcelFileDescriptor. + *

+ * WARNING: Prior to API level 29, this function would not + * actually dup the Socket's FileDescriptor, and would take a + * reference to the its internal FileDescriptor instead. If the Socket + * gets garbage collected before the ParcelFileDescriptor, this may + * lead to the ParcelFileDescriptor being unexpectedly closed. To avoid + * this, the following pattern can be used: + *

{@code
+     *    ParcelFileDescriptor pfd = ParcelFileDescriptor.fromSocket(socket).dup();
+     * }
* * @param socket The Socket whose FileDescriptor is used to create * a new ParcelFileDescriptor. @@ -417,6 +431,16 @@ public class ParcelFileDescriptor implements Parcelable, Closeable { * new ParcelFileDescriptor holds a dup of the original FileDescriptor in * the DatagramSocket, so you must still close the DatagramSocket as well * as the new ParcelFileDescriptor. + *

+ * WARNING: Prior to API level 29, this function would not + * actually dup the DatagramSocket's FileDescriptor, and would take a + * reference to the its internal FileDescriptor instead. If the DatagramSocket + * gets garbage collected before the ParcelFileDescriptor, this may + * lead to the ParcelFileDescriptor being unexpectedly closed. To avoid + * this, the following pattern can be used: + *

{@code
+     *    ParcelFileDescriptor pfd = ParcelFileDescriptor.fromDatagramSocket(socket).dup();
+     * }
* * @param datagramSocket The DatagramSocket whose FileDescriptor is used * to create a new ParcelFileDescriptor. @@ -674,6 +698,9 @@ public class ParcelFileDescriptor implements Parcelable, Closeable { * Return the native fd int for this ParcelFileDescriptor. The * ParcelFileDescriptor still owns the fd, and it still must be closed * through this API. + *

+ * WARNING: Do not call close on the return value of this + * function or pass it to a function that assumes ownership of the fd. */ public int getFd() { if (mWrapped != null) { -- cgit v1.2.3-59-g8ed1b