Java 中是否有与 unputc 等效的程序?

发布于 2024-10-10 17:26:53 字数 3009 浏览 3 评论 0原文

我有一个 BufferedWriter,用来将字符写入 PipedInputStream。基于键盘事件。但我正在实现一个退格处理程序,但如果没有 unputc,我看不出有什么办法可以做到这一点。

我几乎又回到使用字符串来缓冲当前行。

也许,我可以使用 Canvas 做得更好。 (天哪,我多么讨厌java!)

public class Console extends JTextArea {
    /**
     * 
     */
    private static final long serialVersionUID = 6315506875930497268L;
    private PipedInputStream stdin;
    private PipedOutputStream stdout;
    private PipedOutputStream stderr;
    private boolean bounceKey = true;

    private class Dispatcher implements Runnable {
        InputStream in;

        public Dispatcher(InputStream in) {
            this.in = in;
        }

        @Override
        public void run() {
            Reader input = new BufferedReader(new InputStreamReader(in));
            int c;
            try {
                try {
                    while ((c = input.read()) >= 0) {
                        append(String.valueOf((char) c));
                    }
                } finally {
                    input.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public Console() throws IOException {
        stdin = new PipedInputStream();
        stdout = new PipedOutputStream();
        stderr = new PipedOutputStream();
        final Writer kbd = new BufferedWriter(new OutputStreamWriter(
                new PipedOutputStream(stdin)));

        new Thread(new Dispatcher(new PipedInputStream(stdout))).start();
        new Thread(new Dispatcher(new PipedInputStream(stderr))).start();

        addKeyListener(new KeyListener() {

            @Override
            public void keyTyped(KeyEvent e) {
                try {
                    char ch = e.getKeyChar();
                    kbd.write(ch);
                    append(String.valueOf(ch));
                    if(ch == '\n') kbd.flush();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }

            @Override
            public void keyPressed(KeyEvent e) {
                int keycode = e.getKeyCode();
                switch(keycode) {
                case KeyEvent.VK_BACK_SPACE:
                    // Erase the last char from buffer and the screen
                }
            }

            @Override
            public void keyReleased(KeyEvent e) {
            } // DONOTHING
        });

        setEditable(false);
        setFocusable(true);
        setBackground(Color.black);
        setForeground(Color.lightGray);
        setFont(new Font("Monospace", Font.BOLD, 12));
        setLineWrap(true);
    }

    public OutputStream getStderr() {
        return stderr;
    }

    public OutputStream getStdout() {
        return stdout;
    }

    public InputStream getStdin() {
        return stdin;
    }

    public boolean isBounceKey() {
        return bounceKey;
    }

    public void setBounceKey(boolean bounceKey) {
        this.bounceKey = bounceKey;
    }
}

I have a BufferedWriter to which I use to write characters into a PipedInputStream. Based on keyboard events. But I am implementing a backspace handler, but I can see no way of doing so without unputc.

I am almost falling back into using a string to buffer the current line.

Perhaps, I could do a better job using Canvas instead. (God how I hate java!)

public class Console extends JTextArea {
    /**
     * 
     */
    private static final long serialVersionUID = 6315506875930497268L;
    private PipedInputStream stdin;
    private PipedOutputStream stdout;
    private PipedOutputStream stderr;
    private boolean bounceKey = true;

    private class Dispatcher implements Runnable {
        InputStream in;

        public Dispatcher(InputStream in) {
            this.in = in;
        }

        @Override
        public void run() {
            Reader input = new BufferedReader(new InputStreamReader(in));
            int c;
            try {
                try {
                    while ((c = input.read()) >= 0) {
                        append(String.valueOf((char) c));
                    }
                } finally {
                    input.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public Console() throws IOException {
        stdin = new PipedInputStream();
        stdout = new PipedOutputStream();
        stderr = new PipedOutputStream();
        final Writer kbd = new BufferedWriter(new OutputStreamWriter(
                new PipedOutputStream(stdin)));

        new Thread(new Dispatcher(new PipedInputStream(stdout))).start();
        new Thread(new Dispatcher(new PipedInputStream(stderr))).start();

        addKeyListener(new KeyListener() {

            @Override
            public void keyTyped(KeyEvent e) {
                try {
                    char ch = e.getKeyChar();
                    kbd.write(ch);
                    append(String.valueOf(ch));
                    if(ch == '\n') kbd.flush();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }

            @Override
            public void keyPressed(KeyEvent e) {
                int keycode = e.getKeyCode();
                switch(keycode) {
                case KeyEvent.VK_BACK_SPACE:
                    // Erase the last char from buffer and the screen
                }
            }

            @Override
            public void keyReleased(KeyEvent e) {
            } // DONOTHING
        });

        setEditable(false);
        setFocusable(true);
        setBackground(Color.black);
        setForeground(Color.lightGray);
        setFont(new Font("Monospace", Font.BOLD, 12));
        setLineWrap(true);
    }

    public OutputStream getStderr() {
        return stderr;
    }

    public OutputStream getStdout() {
        return stdout;
    }

    public InputStream getStdin() {
        return stdin;
    }

    public boolean isBounceKey() {
        return bounceKey;
    }

    public void setBounceKey(boolean bounceKey) {
        this.bounceKey = bounceKey;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

满意归宿 2024-10-17 17:26:53

我几乎又开始使用字符串来缓冲当前行。

这是正确的方法:保存整行,并且在用户按“enter”之前不要对其执行任何操作。他们可能会删除整个内容 - 如果您正在通信的进程已经采取了行动怎么办?

也就是说,你应该做的事情正是你出于某种原因试图避免做的事情! :-)

I am almost falling back into using a string to buffer the current line.

That is the correct way to do it: save up the whole line and don't do anything with it it until the user presses "enter". They might delete the whole thing - what if the process you are communicating with has already taken action?

I.e. what you should be doing is the thing you are for some reason trying to avoid doing! :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文