ParcelFileDescriptor.FromSocket(Socket) Method

Definition

Create a new ParcelFileDescriptor from the specified Socket.

[Android.Runtime.Register("fromSocket", "(Ljava/net/Socket;)Landroid/os/ParcelFileDescriptor;", "")]
public static Android.OS.ParcelFileDescriptor? FromSocket (Java.Net.Socket? socket);
[<Android.Runtime.Register("fromSocket", "(Ljava/net/Socket;)Landroid/os/ParcelFileDescriptor;", "")>]
static member FromSocket : Java.Net.Socket -> Android.OS.ParcelFileDescriptor

Parameters

socket
Socket

The Socket whose FileDescriptor is used to create a new ParcelFileDescriptor.

Returns

A new ParcelFileDescriptor with a duped copy of the FileDescriptor of the specified Socket.

Attributes

Remarks

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.

<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:

{@code
               ParcelFileDescriptor pfd = ParcelFileDescriptor.fromSocket(socket).dup();
            }

Java documentation for android.os.ParcelFileDescriptor.fromSocket(java.net.Socket).

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to