0

在 CI 上,将简单地创建几个管道并使用 dup2 覆盖 std 文件描述符,而在另一端,我将利用阻塞 IO 在无限循环上为每个输出管道(sdtout、sdterr)创建一个线程管道更新适合控制台建议的 textArea/canvas。至于标准输入,我会监听此类组件上的关键事件,并将其写入管道。

但是我怎样才能在 Java 上用 swing 来执行呢?

我不能将本机代码混合为项目指令。到目前为止,我已经违反了许多项目指令,所以我不能继续这样做......

此外,提供某种级别的终端仿真(例如 VT100)也很酷,但是如何在 unix 上通知 java 应用程序这种功能,我将设置 TERM envvar。

在 CI 上将执行以下操作:

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>

static pthread_t workers[2];

static void *_worker(void *file)
{
    int c;
    if(!file) pthread_exit(NULL);

    while((c=fgetc(file))!=EOF) {
        // Sync and put C on the screen
    }

    pthread_exit(NULL);
}

int initConsole()
{
    int stdin_pipe[2], stdout_pipe[2], stderr_pipe[2];

    if(!(pipe(stdin_pipe)||pipe(stdout_pipe)||pipe(stderr_pipe))) {
        if(dup2(stdin_pipe[0], STDIN_FILENO)<0) return -1;
        if(dup2(stdout_pipe[1], STDOUT_FILENO)<0) return -1;
        if(dup2(stderr_pipe[1], STDERR_FILENO)<0) return -1;

        pthread_create(&workers[0], NULL, _worker, fdopen(stdout_pipe[0], "r"));
        pthread_create(&workers[1], NULL, _worker, fdopen(stderr_pipe[0], "r"));

        // Register a handler within the toolkit to push chars into the stdin_pipe

        return 0;
    }

    return -1;
}
4

2 回答 2

1

System.err/out 和 in 应该在 java 中做同样的事情。您还继承了频道 [System.inheritedChanne()]

我第一次误解了你的问题,所以如果你只需要在线程中阅读 System.in 并使用 EventQueue.invokeLater() 附加到文本区域。

于 2011-01-05T15:25:58.270 回答
1

您可以通过继承 PrintStream 并让您的类简单地写入 JTextArea 来将 System.out 定向到 JTextArea。然后简单地创建你的类的一个实例并调用System.setOut(yourInstance)

read()您可以通过子类化 InputStream 并通过从 JTextArea 返回数据来实现该方法,对 System.in 执行非常相似的操作。

于 2011-01-05T15:31:51.127 回答