Java Code Examples for android.os.ParcelFileDescriptor#detachFd()

The following examples show how to use android.os.ParcelFileDescriptor#detachFd() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MemoryIntArray.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
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");
}
 
Example 2
Source File: MemoryIntArray.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@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();
    }
}
 
Example 3
Source File: Linker.java    From 365browser with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 4
Source File: Linker.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public LibInfo(Parcel in) {
    mLoadAddress = in.readLong();
    mLoadSize = in.readLong();
    mRelroStart = in.readLong();
    mRelroSize = in.readLong();
    ParcelFileDescriptor fd = in.readFileDescriptor();
    mRelroFd = fd.detachFd();
}
 
Example 5
Source File: Linker.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public LibInfo(Parcel in) {
    mLoadAddress = in.readLong();
    mLoadSize = in.readLong();
    mRelroStart = in.readLong();
    mRelroSize = in.readLong();
    ParcelFileDescriptor fd = in.readFileDescriptor();
    mRelroFd = fd.detachFd();
}
 
Example 6
Source File: AndroidNetworkLibrary.java    From cronet with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * 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();
    }
}