使用Java中编号的文件描述符 [英] Using a numbered file descriptor from Java

查看:466
本文介绍了使用Java中编号的文件描述符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从Java访问编号的文件描述符 - 除了0,1或2以外。



这怎么办?我查看了 FileDescriptor 类,但没有找到任何方法用给定的文件描述符号来初始化它。



作为一个具体的例子,让我们假设Java被另一种编程语言称为子进程。文件描述符3和4由其他语言提供,用于输入和输出。

我在Java中需要的是 InputStream OutputStream 连接到这些文件描述符的对象,就像System.in,System.out和System.error连接到文件desorptors 0,1和2。 / p>

我使用的是Java 1.6,它应该在Unix系统上运行。 :

使用文件描述符特殊文件系统条目的答案确实指出了以下可行的解决方案:




  • 找出您的Unix系统是否有和在哪里有一个包含所有文件描述符的命名条目的特殊文件系统。

  • 我使用FreeBSD,其中 fdescfs(5)是一个文件系统,这个。在Linux下,它将是procfs。

  • 确保这个文件系统已被挂载
    $ b $ FreeBSD:在 / etc / fstab中放入 fdescfs / dev / fd fdescfs rw 0 0 在shell提示符下运行



    或运行 mount -t fdescfs null / dev / fd (可能与sudo)


  • 使用新的 FileInputStream(/ dev / fd / 3)新的FileOutputStream(/ dev / fd / 4)来获取连接到filedescriptors的流(路径是FreeBSD,取而代之的是你的操作系统路径)

  • 确定这不能用纯Java完成 - 你可能必须使用本地代码来绑定文件描述符到FileDescriptor对象或FileInputStream或FileOutputStream对象。

    <编辑

    如果您正在使用Linux,* BSD或macOS,则可以使用伪文件/ dev / fd / nnn来访问fil e-descriptor nnn。

    I need to access numbered file descriptors from Java -- other than 0, 1 or 2.

    How can this be done? I looked at the FileDescriptor class but did'nt find any way to initialize it with a given file descriptor number.

    As a concrete example lets suppose Java gets called as a child process from another programing language. File descriptors 3 and 4 are provided by the other language for input and output.

    What I need in Java are InputStream and OutputStream objects connected to these file-descriptors, just like System.in, System.out and System.error are connected to file-desctiptors 0, 1 and 2.

    I'm using Java 1.6 and this should run on Unix alike systems.

    Tested working solution:

    The answer with the file descriptor special filesystem entries did point me to the following workable solution:

    1. find out if and where your Unix alike system has a special filesystem that contains named entries for all file descriptors.

      • I'm using FreeBSD where fdescfs(5) is a filesystem that does just this. Under Linux it would be procfs.
    2. make sure this filesystem is mounted

      • FreeBSD: put fdescfs /dev/fd fdescfs rw 0 0 in /etc/fstab

        or run mount -t fdescfs null /dev/fd on a shell prompt (probably with sudo)

    3. Use new FileInputStream("/dev/fd/3") and new FileOutputStream("/dev/fd/4") to get the streams connected to the filedescriptors (the paths are for FreeBSD, replace with your operating systems paths)

    解决方案

    I'm pretty sure this can't be done using pure Java -- you'll probably have to use native code to bind a file descriptor to a FileDescriptor object or a FileInputStream or FileOutputStream object.

    EDIT
    If you're using Linux, *BSD or macOS, you can use the pseudo files /dev/fd/nnn to access file-descriptor nnn.

    这篇关于使用Java中编号的文件描述符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆