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]

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
> 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!