Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c9119f50 authored by Dianne Hackborn's avatar Dianne Hackborn
Browse files

Add ParcelFileDescriptor APIs to get raw fd.

Change-Id: I66ba72ffffd27237e60c9411453eef950ae62705
parent 6c2193a7
Loading
Loading
Loading
Loading
+22 −0
Original line number Diff line number Diff line
@@ -149691,6 +149691,17 @@
 visibility="public"
>
</method>
<method name="detachFd"
 return="int"
 abstract="false"
 native="false"
 synchronized="false"
 static="false"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
</method>
<method name="fromSocket"
 return="android.os.ParcelFileDescriptor"
 abstract="false"
@@ -149704,6 +149715,17 @@
<parameter name="socket" type="java.net.Socket">
</parameter>
</method>
<method name="getFd"
 return="int"
 abstract="false"
 native="false"
 synchronized="false"
 static="false"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
</method>
<method name="getFileDescriptor"
 return="java.io.FileDescriptor"
 abstract="false"
+1 −0
Original line number Diff line number Diff line
@@ -1385,6 +1385,7 @@ public final class Parcel {
            int mode) throws FileNotFoundException;
    /*package*/ static native void closeFileDescriptor(FileDescriptor desc)
            throws IOException;
    /*package*/ static native void clearFileDescriptor(FileDescriptor desc);

    /**
     * Read a byte value from the parcel at the current dataPosition().
+34 −0
Original line number Diff line number Diff line
@@ -196,6 +196,40 @@ public class ParcelFileDescriptor implements Parcelable {
     */
    public native long seekTo(long pos);
    
    /**
     * Return the native fd int for this ParcelFileDescriptor.  The
     * ParcelFileDescriptor still owns the fd, and it still must be closed
     * through this API.
     */
    public int getFd() {
        if (mClosed) {
            throw new IllegalStateException("Already closed");
        }
        return getFdNative();
    }
    
    private native int getFdNative();
    
    /**
     * Return the native fd int for this ParcelFileDescriptor and detach it
     * from the object here.  You are now responsible for closing the fd in
     * native code.
     */
    public int detachFd() {
        if (mClosed) {
            throw new IllegalStateException("Already closed");
        }
        if (mParcelDescriptor != null) {
            int fd = mParcelDescriptor.detachFd();
            mClosed = true;
            return fd;
        }
        int fd = getFd();
        mClosed = true;
        Parcel.clearFileDescriptor(mFileDescriptor);
        return fd;
    }
    
    /**
     * Close the ParcelFileDescriptor. This implementation closes the underlying
     * OS resources allocated to represent this stream.
+14 −1
Original line number Diff line number Diff line
@@ -126,6 +126,17 @@ static jlong android_os_ParcelFileDescriptor_seekTo(JNIEnv* env,
    return lseek(fd, pos, SEEK_SET);
}

static jlong android_os_ParcelFileDescriptor_getFdNative(JNIEnv* env, jobject clazz)
{
    jint fd = getFd(env, clazz);
    if (fd < 0) {
        jniThrowException(env, "java/lang/IllegalArgumentException", "bad file descriptor");
        return -1;
    }

    return fd;
}

static const JNINativeMethod gParcelFileDescriptorMethods[] = {
    {"getFileDescriptorFromSocket", "(Ljava/net/Socket;)Ljava/io/FileDescriptor;",
        (void*)android_os_ParcelFileDescriptor_getFileDescriptorFromSocket},
@@ -134,7 +145,9 @@ static const JNINativeMethod gParcelFileDescriptorMethods[] = {
    {"getStatSize", "()J",
        (void*)android_os_ParcelFileDescriptor_getStatSize},
    {"seekTo", "(J)J",
        (void*)android_os_ParcelFileDescriptor_seekTo}
        (void*)android_os_ParcelFileDescriptor_seekTo},
    {"getFdNative", "()I",
        (void*)android_os_ParcelFileDescriptor_getFdNative}
};

const char* const kParcelFileDescriptorPathName = "android/os/ParcelFileDescriptor";
+9 −0
Original line number Diff line number Diff line
@@ -1520,6 +1520,14 @@ static void android_os_Parcel_closeFileDescriptor(JNIEnv* env, jobject clazz, jo
    }
}

static void android_os_Parcel_clearFileDescriptor(JNIEnv* env, jobject clazz, jobject object)
{
    int fd = env->GetIntField(object, gFileDescriptorOffsets.mDescriptor);
    if (fd >= 0) {
        env->SetIntField(object, gFileDescriptorOffsets.mDescriptor, -1);
    }
}

static void android_os_Parcel_freeBuffer(JNIEnv* env, jobject clazz)
{
    int32_t own = env->GetIntField(clazz, gParcelOffsets.mOwnObject);
@@ -1719,6 +1727,7 @@ static const JNINativeMethod gParcelMethods[] = {
    {"internalReadFileDescriptor",  "()Ljava/io/FileDescriptor;", (void*)android_os_Parcel_readFileDescriptor},
    {"openFileDescriptor",  "(Ljava/lang/String;I)Ljava/io/FileDescriptor;", (void*)android_os_Parcel_openFileDescriptor},
    {"closeFileDescriptor", "(Ljava/io/FileDescriptor;)V", (void*)android_os_Parcel_closeFileDescriptor},
    {"clearFileDescriptor", "(Ljava/io/FileDescriptor;)V", (void*)android_os_Parcel_clearFileDescriptor},
    {"freeBuffer",          "()V", (void*)android_os_Parcel_freeBuffer},
    {"init",                "(I)V", (void*)android_os_Parcel_init},
    {"destroy",             "()V", (void*)android_os_Parcel_destroy},