[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: InputStream



Chris,

I think this is more a fault of the terminal io as opposed to java.  For
instance, this in C:

char c;

while (1==1)
{
    c = getchar();
    printf("[%c]", c);
}

has the same behavior as this in Java:

while(1==1)
{
    char c = 0;
    try { c = (char) System.in.read();}
    catch(Exception e){e.printStackTrace();}
    System.out.print("[" + c + "]");
}

They both wait until the new line char is entered before processing.  I
think the terminal uses the readine() function for terminal input, and
it's line buffered.  If you were to create your own inputstream and
provide a stream of data through it to test (perhaps read a file?), rather
that command line input, I suspect behavior might be different.

John

cfowler said:
>   public void run() {
>     int i;
>     try {
>       while(true) {
>         in.mark(1);
>         i = in.read();
>         System.out.print("[" + (char)i + "]");
>         if(Main.sock.isConnected() == false) {
>           System.err.println("Remote host closed connection");
>           System.exit(0);
>         }
>         out.write((byte)i);
>         out.flush();
>       }
>     } catch (Exception e) {
>
>
> If I type Hello, I does not display [H] after the first H, it waits till
> I press return to.  The tty is passed to Java in cooked mode and must be
> converted by java in raw mode.  This is the key.  Otherwise, I'l write
> this in C :(
>
>
>
> On Tue, 2003-01-28 at 13:45, John Wells wrote:
>> Chris,
>>
>> Hmm...afaik System.in.read() should only read the next byte from the
>> stream.
>>
>> Have you attempted to print the char/byte out to see exactly what
>> you're getting?
>>
>> When you say it's behaving in line mode, what is leading you to
>> believe this?  Can you provide a sample of what you're trying to read
>> in, and what you're actually getting?
>>
>> If you're inputing from the command line, it's buffered input using
>> readline(), I believe, so System.in.read() in your Java program won't
>> get any characters until your shell sees a newline and submits.
>>
>> John
>>
>> cfowler said:
>> > Is there a way to convert an InputStream like System.in. from what
>> appears to be line-mode to char mode?
>> >
>> > I'm doing this:
>> >
>> > int i = System.in.read();
>> > sockOutStream.write(i);
>> >
>> > It behaves as if I'm in line mode.
>>
>>
>>