diff options
| author | 2019-01-24 11:02:17 -0800 | |
|---|---|---|
| committer | 2019-01-24 11:02:17 -0800 | |
| commit | d152e75a7c12449c7dfcec1d312fef67b6ad6da3 (patch) | |
| tree | b6e2a06c958b2c62c1af87c04f8f3d122abf40e2 | |
| parent | d5cc53302fb99669c43a2e7c1e43dc1860615e39 (diff) | |
| parent | 8d7976a37e31071c79945be30f1b9d96638609b8 (diff) | |
Merge "ParcelFileDescriptor: fix various ownership mistakes."
am: 8d7976a37e
Change-Id: I4a6b274fcbb09ab52242f00a85630e91c1d87a5a
| -rw-r--r-- | core/java/android/os/ParcelFileDescriptor.java | 32 |
1 files changed, 24 insertions, 8 deletions
diff --git a/core/java/android/os/ParcelFileDescriptor.java b/core/java/android/os/ParcelFileDescriptor.java index 81fc5c0303d5..a89fc095b403 100644 --- a/core/java/android/os/ParcelFileDescriptor.java +++ b/core/java/android/os/ParcelFileDescriptor.java @@ -55,6 +55,7 @@ import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InterruptedIOException; +import java.io.UncheckedIOException; import java.net.DatagramSocket; import java.net.Socket; import java.nio.ByteOrder; @@ -397,26 +398,41 @@ public class ParcelFileDescriptor implements Parcelable, Closeable { * @param socket The Socket whose FileDescriptor is used to create * a new ParcelFileDescriptor. * - * @return A new ParcelFileDescriptor with the FileDescriptor of the - * specified Socket. + * @return A new ParcelFileDescriptor with a duped copy of the + * FileDescriptor of the specified Socket. + * + * @throws UncheckedIOException if {@link #dup(FileDescriptor)} throws IOException. */ public static ParcelFileDescriptor fromSocket(Socket socket) { FileDescriptor fd = socket.getFileDescriptor$(); - return fd != null ? new ParcelFileDescriptor(fd) : null; + try { + return fd != null ? ParcelFileDescriptor.dup(fd) : null; + } catch (IOException e) { + throw new UncheckedIOException(e); + } } /** - * Create a new ParcelFileDescriptor from the specified DatagramSocket. + * Create a new ParcelFileDescriptor from the specified DatagramSocket. The + * 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. * * @param datagramSocket The DatagramSocket whose FileDescriptor is used * to create a new ParcelFileDescriptor. * - * @return A new ParcelFileDescriptor with the FileDescriptor of the - * specified DatagramSocket. + * @return A new ParcelFileDescriptor with a duped copy of the + * FileDescriptor of the specified Socket. + * + * @throws UncheckedIOException if {@link #dup(FileDescriptor)} throws IOException. */ public static ParcelFileDescriptor fromDatagramSocket(DatagramSocket datagramSocket) { FileDescriptor fd = datagramSocket.getFileDescriptor$(); - return fd != null ? new ParcelFileDescriptor(fd) : null; + try { + return fd != null ? ParcelFileDescriptor.dup(fd) : null; + } catch (IOException e) { + throw new UncheckedIOException(e); + } } /** @@ -546,7 +562,7 @@ public class ParcelFileDescriptor implements Parcelable, Closeable { } file.deactivate(); FileDescriptor fd = file.getFileDescriptor(); - return fd != null ? new ParcelFileDescriptor(fd) : null; + return fd != null ? ParcelFileDescriptor.dup(fd) : null; } /** |