简体   繁体   English

在Java / Android中,为什么我可以从ParcelFileDescriptor获取整数文件描述符?

[英]In Java / Android, why am I able to get an integer file descriptor from ParcelFileDescriptor?

I have solved a problem I had while coding on the NDK, but I'm not sure the solution is canonical, or if there is a canonical way to do this. 我已经解决了在NDK上编码时遇到的问题,但是我不确定该解决方案是否规范,或者不确定是否有规范的方法可以做到这一点。 First a description of what I did: 首先说明一下我做了什么:

It appears that I cannot access an integer file descriptor value using Java's File or FileDescriptor objects: http://developer.android.com/reference/java/io/File.html http://developer.android.com/reference/java/io/FileDescriptor.html 看来我无法使用Java的File或FileDescriptor对象访问整数文件描述符值: http : //developer.android.com/reference/java/io/File.html http://developer.android.com/reference/java /io/FileDescriptor.html

But I can using Android's ParcelFileDescriptor object: http://developer.android.com/reference/android/os/ParcelFileDescriptor.html 但是我可以使用Android的ParcelFileDescriptor对象: http : //developer.android.com/reference/android/os/ParcelFileDescriptor.html

So I can get the integer fd to my native code in this way: 因此,我可以通过这种方式将整数fd转换为本机代码:

pfd = ParcelFileDescriptor.open(new File("blah"), MODE_WRITE_ONLY);
myNativeFunction(pfd.getFd());

Why does this work? 为什么这样做? Wouldn't the file descriptor integer field be private in the File object, since I can't access it even when I own the object? 文件描述符整数字段在File对象中是否是私有的,因为即使我拥有该对象也无法访问它? So how does the ParcelFileDescriptor get to access this presumably private field just by being passed the object in one of its public methods? 那么,仅通过在对象的公共方法之一中传递对象,ParcelFileDescriptor如何才能访问此假定的私有字段? Do I even want to know? 我想知道吗?

The Java.io.File and Java.io.FileDescriptor are part of the base Java APIs defined by Sun, which, by design, never expose an integer file descriptor to the. Java.io.File和Java.io.FileDescriptor是Sun定义的基本Java API的一部分,根据设计,它们从不向其公开整数文件描述符。 This is likely done for the sake of abstraction, ie to make Java programs portable to custom operating systems that don't use simple integers to identify opened file descriptors. 这样做可能是出于抽象的目的,即使Java程序可移植到不使用简单整数来标识打开的文件描述符的定制操作系统。

On the other hand, android.os.ParcelFileDescriptor is an Android-specific API, and on Android, which is based on Linux, integers are always used to model file descriptors, so it's ok to expose it. 另一方面,android.os.ParcelFileDescriptor是一个特定于Android的API,在基于Linux的Android上,始终使用整数来对文件描述符进行建模,因此可以公开它。

You probably don't need to know exactly how ParcelFileDescriptor performs its magic though. 但是,您可能不需要确切地了解ParcelFileDescriptor如何执行其魔术。 Just my 2 cents. 只是我的2美分。

how does the ParcelFileDescriptor get to access this presumably private field ParcelFileDescriptor如何访问这个大概的私有字段

The integer file descriptor is not part of java.io.File class at all. 整数文件描述符根本不是java.io.File类的一部分。 This class wraps a file name and can be used for names that don't exist or even cannot exist on the file system. 此类包装文件名,可用于文件系统上不存在甚至不存在的名称。 When you work with Java File object, the Linux file is not opened. 当您使用Java File对象时,不会打开Linux file So, to open the file, we use one of the many classes like FileInputStream , FileReader , etc. 因此,要打开文件,我们使用许多类之一,例如FileInputStreamFileReader等。

FileDescriptor class, defined in the core Java API, can be used for some manipulations with open files; 核心Java API中定义的FileDescriptor类可用于对打开的文件进行某些操作。 it does "know" the actual int file descriptor (you can check its source code ), but Android SDK isolates this number, as @danske correctly explained. 它确实“知道”实际的int文件描述符(您可以检查其源代码 ),但是Android SDK会隔离此数字,如@danske正确解释的那样。

android.os.ParcelFileDescriptor uses system library libnativehelper.so to find the value, you can see the relevant source code , too. android.os.ParcelFileDescriptor使用系统库libnativehelper.so查找值,您也可以看到相关的源代码 You can actually look up the sources of the nativehelper , too. 您实际上也可以查找nativehelper的来源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM