problem writing into stdin with PushbackInputStream

Hi all,

I want to write into System.in but I am running into some problems.

I wrote the following code:

PushbackInputStream pushIn = new PushbackInputStream(System.in,20);

String newLine="data";

pushIn.unread(newLine.getBytes());

BufferedReader bufferIn = new BufferedReader(new InputStreamReader(pushIn));

char c;

while (!bufferIn.ready()){

System.out.println("ready="+bufferIn.ready());

}

System.out.println("ready="+bufferIn.ready());

if(bufferIn.ready()){

c=(char)bufferIn.read();

System.out.println("str -=*- " + c+ "-");

}

The output displays "ready=true" and then gets stuck in the"c=(char)bufferIn.read();".

Do you have any idea why the buffer is ready to read and gets stuck?

Thanks!

[844 byte] By [dimebaga] at [2007-11-27 4:49:52]
# 1
read returns an int. you should usebufferln.read(c);
Leviaa at 2007-7-12 10:03:01 > top of Java-index,Java Essentials,Java Programming...
# 2
It sitll does not work.I try to read with either a BufferedReader or a PushbackInputStream but it still gets stuck.
dimebaga at 2007-7-12 10:03:01 > top of Java-index,Java Essentials,Java Programming...
# 3
after running program, type something then click enter
java_2006a at 2007-7-12 10:03:01 > top of Java-index,Java Essentials,Java Programming...
# 4

Is this what you are looking for ?

import java.io.PushbackInputStream;

public class PushbackInputStreamDemo {

public static void main(String[] args) {

try {

PushbackInputStream pushIn = new PushbackInputStream(System.in, 20);

String newLine = "data";

pushIn.unread(newLine.getBytes());

int i=-1;

while((i=pushIn.read())>=0){

System.out.print((char)i);

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

Hope That Helps

java_2006a at 2007-7-12 10:03:01 > top of Java-index,Java Essentials,Java Programming...
# 5
You still pushin', dimebag?
Hippolytea at 2007-7-12 10:03:02 > top of Java-index,Java Essentials,Java Programming...
# 6

> Is this what you are looking for ?

> import java.io.PushbackInputStream;

>

> public class PushbackInputStreamDemo {

>

> public static void main(String[] args) {

> try {

>

> PushbackInputStream pushIn = new

> new PushbackInputStream(System.in, 20);

> String newLine = "data";

> pushIn.unread(newLine.getBytes());

>

> int i=-1;

> while((i=pushIn.read())>=0){

> System.out.print((char)i);

> }

> } catch (Exception e) {

> e.printStackTrace();

> }

>

> }

> }

>

>

> Hope That Helps

Thanks a lot for your help!!

That is almost what I want. :)

The only difference is that I have a process p1 who reads pushIn

pushIn.read(). He is waiting for data in pushIn, that's why I have a second process p2 who will unread "data" sothat p1 can read them.

Though if p1 tries to read "data" before p2 unreads it, I need to type "enter" to be able to read...and I would need to do that automatically (like in your example that works because the same process unreads and reads in sequence).

Does anyone have an idea?

I hope I made myself clear.

Again, thanks for your help!

dimebaga at 2007-7-12 10:03:02 > top of Java-index,Java Essentials,Java Programming...
# 7
any idea?Thanks! :)
dimebaga at 2007-7-12 10:03:02 > top of Java-index,Java Essentials,Java Programming...
# 8
I would review the whole requirement. Why do you want to write to stdin?
ejpa at 2007-7-12 10:03:02 > top of Java-index,Java Essentials,Java Programming...