如何检测Jsch中的输入结束?

时间:2017-11-17 02:46:18

标签: swing inputstream jsch

我在我的程序中使用Jsch。
我想显示用户当前正在处理的目录 因此,我想显示一个目录,例如,“[/ home / user / abc]< user command input>”,而不是显示uid。

我的想法是在用户的每个命令之后发送pwd并解析输出。
但问题是我不知道如何从inputstream解析它。
如何确定pwd和用户命令之间的输入边界?

public class TextFieldStreamer extends InputStream implements ActionListener
{
    private JTextField         tf;
    private String             str    = null;
    private int                pos    = 0;
    private boolean            finish = false;

    public TextFieldStreamer(JTextField jtf)
    {
        tf = jtf;
    }

    public void close()
    {
        finish = true;
        try
        {
            super.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    // gets triggered everytime that "Enter" is pressed on the textfield
    public void actionPerformed(ActionEvent e)
    {
        enterAction();
    }

    public void enterAction()
    {
        str = tf.getText();
        str += "\n";
        pos = 0;
    }

    @Override
    public int read()
    {
        // test if the available input has reached its end
        // and the EOS should be returned
        if (str != null && pos == str.length())
        {
            str = null;
            // this is supposed to return -1 on "end of stream"
            // but I'm having a hard time locating the constant
            return java.io.StreamTokenizer.TT_EOF;
        }

        while (!finish && (str == null || pos >= str.length()))
        {
            try
            {
                Thread.sleep(100);
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
        }
        return str.charAt(pos++);
    }

}

public class TextAreaOutputStream extends OutputStream
{
    JTextArea textArea;

    public TextAreaOutputStream(JTextArea textArea)
    {
        this.textArea = textArea;
    }

    public void write(int b) throws IOException
    {
        write(new byte[] { (byte) b }, 0, 1);
    }

    public void write(byte[] b, int off, int len) throws IOException
    {
        final String text = new String(b, off, len, "UTF-8");
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                textArea.append(text);
            }
        });
    }
}

0 个答案:

没有答案