summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Josh Gao <jmgao@google.com> 2019-03-15 19:52:34 -0700
committer android-build-merger <android-build-merger@google.com> 2019-03-15 19:52:34 -0700
commitbc7222d46df3f1283dfb6cf8965702b4d90a73e7 (patch)
treeb1903ac170111dd451ac8b60d2d70ef1c4776bb8
parent545bace60f490a2f45b05dd881f4e467eb127fea (diff)
parentc4e2b24d87c55bbe83f576af53e38ac7e4a63e3b (diff)
Merge "ParcelFileDescriptor: improve javadoc."
am: c4e2b24d87 Change-Id: Ic01db5d3f9c7527f2458aa4cf10bd2c43d685bed
-rw-r--r--core/java/android/os/ParcelFileDescriptor.java29
1 files changed, 28 insertions, 1 deletions
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.
+ * <p>
+ * <strong>WARNING:</strong> 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.
+ * <p>
+ * <strong>WARNING:</strong> 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:
+ * <pre>{@code
+ * ParcelFileDescriptor pfd = ParcelFileDescriptor.fromSocket(socket).dup();
+ * }</pre>
*
* @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.
+ * <p>
+ * <strong>WARNING:</strong> 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:
+ * <pre>{@code
+ * ParcelFileDescriptor pfd = ParcelFileDescriptor.fromDatagramSocket(socket).dup();
+ * }</pre>
*
* @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.
+ * <p>
+ * <strong>WARNING:</strong> 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) {