1

I would like to use a BufferedReader with a kind of readLine() (or similar) that can return an echo for every keystroke pressed.

It's for a remote terminal. Other way to ask it is how is implemented a console in java.

This is what came to mind but is too ugly. Is there any known library that implement something like this?

while(condition) {

    nByteRead = in.read(buffer);

    if (nByteRead != -1) {

        //  ECHO
        out.write(buffer, 0, bytes_read);                      

        //  read bytes till NEW_LINE...                            
        //  etc...!                            
    }                           
}                    

Of course I could encapsulate this behaviour in some thread and go on with a library for this, I just wonder if there is some wheel already invented.

Thanks for any hint!

aioobe
  • 399,198
  • 105
  • 792
  • 807
Hernán Eche
  • 5,957
  • 11
  • 49
  • 73

2 Answers2

1

Most terminals, including the default terminals in Ubuntu and Windows (I believe) won't pass on the characters to the JVM until the user hits return. (I.e., it is buffered on a full-line basis on a lower level in the system.)

If you need to read one character at a time from the terminal, you'll have to go with a lower level system library.

Related question:

(disclamer, I'm not completely sure I understood your question correctly.)

Community
  • 1
  • 1
aioobe
  • 399,198
  • 105
  • 792
  • 807
1

There's:

Bart Kiers
  • 161,100
  • 35
  • 287
  • 281
  • I'll try JLine but I see it uses some dll, and work differently for windows and linux. I see there is no Java standard way to do this, so perhaps I'll code it by hand, thanks – Hernán Eche Sep 01 '11 at 12:18