Skip to content

Commit b3e4ef3

Browse files
committed
COMMENT ONLY change to add some warnings about ParcelFileDescriptor
behavior with Parcel.writeValue(). Change-Id: If55fcce29559379855735ff5297cf4f46d5d6be6 Bug: 2847738
1 parent bfcbeff commit b3e4ef3

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

core/java/android/os/Parcel.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,12 @@ public final void writeStrongInterface(IInterface val) {
437437
/**
438438
* Write a FileDescriptor into the parcel at the current dataPosition(),
439439
* growing dataCapacity() if needed.
440+
*
441+
* <p class="caution">The file descriptor will not be closed, which may
442+
* result in file descriptor leaks when objects are returned from Binder
443+
* calls. Use {@link ParcelFileDescriptor#writeToParcel} instead, which
444+
* accepts contextual flags and will close the original file descriptor
445+
* if {@link Parcelable#PARCELABLE_WRITE_RETURN_VALUE} is set.</p>
440446
*/
441447
public final native void writeFileDescriptor(FileDescriptor val);
442448

@@ -1000,7 +1006,7 @@ public final <T extends Parcelable> void writeTypedArray(T[] val,
10001006
/**
10011007
* Flatten a generic object in to a parcel. The given Object value may
10021008
* currently be one of the following types:
1003-
*
1009+
*
10041010
* <ul>
10051011
* <li> null
10061012
* <li> String
@@ -1023,7 +1029,7 @@ public final <T extends Parcelable> void writeTypedArray(T[] val,
10231029
* <li> Parcelable[]
10241030
* <li> CharSequence (as supported by {@link TextUtils#writeToParcel}).
10251031
* <li> List (as supported by {@link #writeList}).
1026-
* <li> {@link SparseArray} (as supported by {@link #writeSparseArray}).
1032+
* <li> {@link SparseArray} (as supported by {@link #writeSparseArray(SparseArray)}).
10271033
* <li> {@link IBinder}
10281034
* <li> Any object that implements Serializable (but see
10291035
* {@link #writeSerializable} for caveats). Note that all of the
@@ -1032,6 +1038,13 @@ public final <T extends Parcelable> void writeTypedArray(T[] val,
10321038
* approach is much less efficient and should be avoided whenever
10331039
* possible.
10341040
* </ul>
1041+
*
1042+
* <p class="caution">{@link Parcelable} objects are written with
1043+
* {@link Parcelable#writeToParcel} using contextual flags of 0. When
1044+
* serializing objects containing {@link ParcelFileDescriptor}s,
1045+
* this may result in file descriptor leaks when they are returned from
1046+
* Binder calls (where {@link Parcelable#PARCELABLE_WRITE_RETURN_VALUE}
1047+
* should be used).</p>
10351048
*/
10361049
public final void writeValue(Object v) {
10371050
if (v == null) {
@@ -1120,7 +1133,7 @@ public final void writeValue(Object v) {
11201133
/**
11211134
* Flatten the name of the class of the Parcelable and its contents
11221135
* into the parcel.
1123-
*
1136+
*
11241137
* @param p The Parcelable object to be written.
11251138
* @param parcelableFlags Contextual flags as per
11261139
* {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}.

core/java/android/os/ParcelFileDescriptor.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,11 @@ public int describeContents() {
250250
return Parcelable.CONTENTS_FILE_DESCRIPTOR;
251251
}
252252

253+
/**
254+
* {@inheritDoc}
255+
* If {@link Parcelable#PARCELABLE_WRITE_RETURN_VALUE} is set in flags,
256+
* the file descriptor will be closed after a copy is written to the Parcel.
257+
*/
253258
public void writeToParcel(Parcel out, int flags) {
254259
out.writeFileDescriptor(mFileDescriptor);
255260
if ((flags&PARCELABLE_WRITE_RETURN_VALUE) != 0 && !mClosed) {

0 commit comments

Comments
 (0)