下面列出了android.os.ParcelFileDescriptor#detachFd ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private MemoryIntArray(Parcel parcel) throws IOException {
mIsOwner = false;
ParcelFileDescriptor pfd = parcel.readParcelable(null);
if (pfd == null) {
throw new IOException("No backing file descriptor");
}
mFd = pfd.detachFd();
mMemoryAddr = nativeOpen(mFd, mIsOwner);
mCloseGuard.open("close");
}
@Override
public void writeToParcel(Parcel parcel, int flags) {
ParcelFileDescriptor pfd = ParcelFileDescriptor.adoptFd(mFd);
try {
// Don't let writing to a parcel to close our fd - plz
parcel.writeParcelable(pfd, flags & ~Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
} finally {
pfd.detachFd();
}
}
public LibInfo(Parcel in) {
mLoadAddress = in.readLong();
mLoadSize = in.readLong();
mRelroStart = in.readLong();
mRelroSize = in.readLong();
ParcelFileDescriptor fd = ParcelFileDescriptor.CREATOR.createFromParcel(in);
// If CreateSharedRelro fails, the OS file descriptor will be -1 and |fd| will be null.
mRelroFd = (fd == null) ? -1 : fd.detachFd();
}
public LibInfo(Parcel in) {
mLoadAddress = in.readLong();
mLoadSize = in.readLong();
mRelroStart = in.readLong();
mRelroSize = in.readLong();
ParcelFileDescriptor fd = in.readFileDescriptor();
mRelroFd = fd.detachFd();
}
public LibInfo(Parcel in) {
mLoadAddress = in.readLong();
mLoadSize = in.readLong();
mRelroStart = in.readLong();
mRelroSize = in.readLong();
ParcelFileDescriptor fd = in.readFileDescriptor();
mRelroFd = fd.detachFd();
}
/**
* Tag socket referenced by {@code ifd} with {@code tag} for UID {@code uid}.
*
* Assumes thread UID tag isn't set upon entry, and ensures thread UID tag isn't set upon exit.
* Unfortunately there is no TrafficStatis.getThreadStatsUid().
*/
@CalledByNative
private static void tagSocket(int ifd, int uid, int tag) throws IOException {
// Set thread tags.
int oldTag = TrafficStats.getThreadStatsTag();
if (tag != oldTag) {
TrafficStats.setThreadStatsTag(tag);
}
if (uid != TrafficStatsUid.UNSET) {
ThreadStatsUid.set(uid);
}
// Apply thread tags to socket.
// First, convert integer file descriptor (ifd) to FileDescriptor.
final ParcelFileDescriptor pfd;
final FileDescriptor fd;
// The only supported way to generate a FileDescriptor from an integer file
// descriptor is via ParcelFileDescriptor.adoptFd(). Unfortunately prior to Android
// Marshmallow ParcelFileDescriptor.detachFd() didn't actually detach from the
// FileDescriptor, so use reflection to set {@code fd} into the FileDescriptor for
// versions prior to Marshmallow. Here's the fix that went into Marshmallow:
// https://android.googlesource.com/platform/frameworks/base/+/b30ad6f
if (Build.VERSION.SDK_INT < VERSION_CODES.M) {
pfd = null;
fd = SetFileDescriptor.createWithFd(ifd);
} else {
pfd = ParcelFileDescriptor.adoptFd(ifd);
fd = pfd.getFileDescriptor();
}
// Second, convert FileDescriptor to Socket.
Socket s = new SocketFd(fd);
// Third, tag the Socket.
TrafficStats.tagSocket(s);
s.close(); // No-op but always good to close() Closeables.
// Have ParcelFileDescriptor relinquish ownership of the file descriptor.
if (pfd != null) {
pfd.detachFd();
}
// Restore prior thread tags.
if (tag != oldTag) {
TrafficStats.setThreadStatsTag(oldTag);
}
if (uid != TrafficStatsUid.UNSET) {
ThreadStatsUid.clear();
}
}