• Post Reply Bookmark Topic Watch Topic
  • New Topic

BufferdReader/Writer problem

 
Chris Dancy
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I posted this problem over in java Intermediate and have gotten no reposnse, so as a last effort I'm posting it here which is where it seems I should have posted in the first place. Sorry if this seems like cross posting however I did not receive an answer from anyone over there and maybe this problem is too advanced for them...

Here is the issue. My java program starts and launches an external program. I get the inputstream and outputstream of that process to play with. I can pipe all the inputstream information to a textarea just fine... however... its when i want to give input to the outputstream that the problem occurs. I believe I'm just thinking of this the wrong way and am coming here for help.
Also if anyone looking at this code can knows how I can differentiate from a process that needs input to one that does not that would be great as well.

here is the sample code and the following ruby script I used for the external program...




public static void test3() throws IOException{
String line;
Scanner scan = new Scanner(System.in);

Process process = Runtime.getRuntime ().exec ("/home/cdancy/Desktop/start.rb");
OutputStream stdin = process.getOutputStream ();
InputStream stderr = process.getErrorStream ();
InputStream stdout = process.getInputStream ();

BufferedReader reader = new BufferedReader (new InputStreamReader(stdout));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stdin));

String temp = "\n";
writer.write(temp);
writer.close();
System.out.println("before loop");
while ((line = reader.readLine ()) != null) {

System.out.println ("Stdout: " + line);
String input = scan.nextLine();
input += "\n";
writer = new BufferedWriter(new OutputStreamWriter(stdin));
writer.write(input);
writer.close();
}
reader.close();
}



here is the simple ruby script i am using...


#! /usr/bin/ruby
puts "Enter a number: "
line = gets
puts "Number is " + line
puts "Enter another number: "
line = gets
puts "Second number is " + line
puts "Script is done"


again any help is appreciated.

Thanks in advance,
 
Chris Blades
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you may have stdin and stdout backwards. I looked up Process in the API and it is quite vague, so I can't say for sure. From my understanding, Process.getOutputStream() returns stdout for the process and .getInputStream() returns stdin for the process.

-Chris

[ December 24, 2008: Message edited by: Chris Blades ]

edit: close() flushes automatically.
[ December 24, 2008: Message edited by: Chris Blades ]
 
Chris Dancy
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
writer.close() will flush the bufferedwriter and close it.... however that brings up the next problem. If i dont close the bufferedwriter before i enter the while loop the program just hangs indefinetly
 
Rob Spoor
Sheriff
Posts: 20820
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Chris Blades:
I think you may have stdin and stdout backwards. I looked up Process in the API and it is quite vague, so I can't say for sure. From my understanding, Process.getOutputStream() returns stdout for the process and .getInputStream() returns stdin for the process.

-Chris

Well no, the other Chris has it right.

getOutputStream() returns an output stream that you can use to write to the stdin of the process.
getInputStream() returns an input stream that you can use to read from the stdout of the process.
The names of these methods do not reflect the stream from which they read, but the type of stream they return.

The names are definitely confusing, and perhaps they should have been named getStdinStream(), getStdoutStream() and getStderrStream() to prevent this confusion.
 
Chris Dancy
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a piece of code that does work. It takes the input first and then spits out the output. However this is bad for me. I want to pipe in the input to the stream in real time...Maybe this will help



public static void test3() throws IOException{
String line;
Scanner scan = new Scanner(System.in);

Process process = Runtime.getRuntime ().exec ("/home/cdancy/Desktop/start.rb");
OutputStream stdin = process.getOutputStream ();
InputStream stderr = process.getErrorStream ();
InputStream stdout = process.getInputStream ();

BufferedReader reader = new BufferedReader (new InputStreamReader(stdout));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stdin));

String input = scan.nextLine();
input += "\n";
writer.write(input);
writer.flush();

input = scan.nextLine();
input += "\n";
writer.write(input);
writer.close();

while ((line = reader.readLine ()) != null) {
System.out.println ("Stdout: " + line);
}
reader.close();
}
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!