在本机代码中创建的未命名管道的 Java 输入/输出流?
有没有一种方法可以轻松地为在本机代码中创建的未命名管道创建 Java 输入/输出流?
动机:我需要自己实现 Process 类。本机代码生成了一个新进程,其中子进程的 IO 流重定向到未命名的管道。
问题:这些管道正确末端的文件描述符进入了 Java。此时我陷入困境,因为我无法创建一个可以传递给 FileInput/FileOutput 流的新 FileDescriptor。
我使用反射来解决问题,并与运行的简单保镖子进程进行通信。但是我有一个想法,这不是最干净的方法。你用过这个方法吗?您认为这种方法有什么问题吗? (平台永远不会改变)
在互联网上搜索发现了使用本机代码的类似解决方案。
在我深入测试这种方法之前,任何想法都是非常受欢迎的。 我想在编写自己的 IO 流实现之前先尝试一下现有代码...
谢谢。
Is there a way to easily create Java Input/Output streams for unnamed pipes created in native code?
Motivation: I need my own implementation of the Process class. The native code spawns me a new process with subprocess' IO streams redirected to unnamed pipes.
Problem: The file descriptors for correct ends of those pipes make their way to Java. At this point I get stuck as I cannot create a new FileDescriptor which I could pass to FileInput/FileOutput stream.
I have used reflection to get around the problem and got communication with a simple bouncer sub-process running. However I have a notion that it is not the cleanest way to go. Have you used this approach? Do you see any problems with this approach? (the platform will never change)
Searching around the internets revealed similar solution using native code.
Any thoughts before I dive into heavy testing of this approach are very welcome. I would like to give a shot to existing code before writing my own IO stream implementations...
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我以前也遇到过这个问题。我知道创建 FileDescriptor 对象的唯一方法是使用反射(或从 JNI)来设置 FileDescriptor 类内的私有“int”字段。
但这工作得很好,并且肯定是标准库中其他地方的做法,所以我相信它正如您所期望的那样合法。
I have run into this before as well. The only way I know to create FileDescriptor objects is using reflection (or from JNI) to set the private "int" field inside the FileDescriptor class.
But this works fine and is surely how it's done elsewhere in the standard library, so I believe it's as legitimate as you could expect.