Russel Winder | 1 Dec 2004 10:55
Picon
Gravatar

Is this a buffering problem?

I have a Java class TriangleCalculation which as its first action it
outputs a prompt and then waits for input:

|> java TriangleCalculation
Enter length of first side:

I then tried the Groovy script:

#! /usr/bin/env groovy

javaProcess = "java TriangleCalculation".execute()
javaProcess.in.eachLine { line | println line }

but I do not get any output, it just goes straight to the hanging.  Is
there an extra level of IO buffering going on that I need to circumvent?

--

-- 
Russel.
=============================================
Dr Russel Winder         +44 20 7585 2200
41 Buckmaster Road       +44 7770 465 077
London SW11 1EN, UK      russel@...

Guillaume Laforge | 1 Dec 2004 11:12
Gravatar

Re: Is this a buffering problem?

Hello Russel,

Russel Winder wrote:
> I have a Java class TriangleCalculation which as its first action it
> outputs a prompt and then waits for input:
> 
> |> java TriangleCalculation
> Enter length of first side:
> 
> I then tried the Groovy script:
> 
> #! /usr/bin/env groovy
> 
> javaProcess = "java TriangleCalculation".execute()
> javaProcess.in.eachLine { line | println line }
> 
> but I do not get any output, it just goes straight to the hanging.  Is
> there an extra level of IO buffering going on that I need to circumvent?

You'll have to provide some input to your TriangleCalculation program:
javaProcess.out.println(myLengthOfFirstSide)

--
Guillaume Laforge
http://glaforge.free.fr/weblog

Russel Winder | 1 Dec 2004 11:33
Picon
Gravatar

Re: Is this a buffering problem?

On Wed, 2004-12-01 at 10:12, Guillaume Laforge wrote:
 [ . . . ]
> You'll have to provide some input to your TriangleCalculation program:
> javaProcess.out.println(myLengthOfFirstSide)

I think your proposal implies that when using the eachLine method the
output from the process is buffered until the process finishes?  What I
am looking for is to be able to check that each string output as prompt
is correct before sending the output.

Basically I am trying to rewrite in Groovy a Python script that acts
like a human user of an console-based interactive program -- very
expect-like I know!  The point here is that the sequencing of input and
output has to be as it would be on the console.  I guess this means
manipulating the InputStream explicitly.

The experiment:  do things manually, entering 3, 4, 5 then

|> java TriangleCalculation
Enter length of first side: 3
Enter length of second side: 4
Enter length of third side: 5
Perimeter is: 12.0
Area is: 6.0

the groovy script is:

#! /usr/bin/env groovy

javaProcess = "java TriangleCalculation".execute()
(Continue reading)

Atif Shahab | 1 Dec 2004 11:37
Picon
Favicon

Re: Is this a buffering problem?

javaProcess.out.println(myLengthOfFirstSide)

rather than sending the out stream to the process, is instead printing 
to the screen

-------------- trun.pl -------------
#!/usr/bin/env perl

print "Enter length of first side: ";
$n = <STDIN>;
--------------------------------------

Groovy script is

#!/usr/bin/env groovy
javaProcess = "/full/path/to/trun.pl".execute();
javaProcess.out.println( "2" );
javaProcess.in.eachLine{ line | println line };

output is

2
<hangs here>

Best Regards,

On 01-Dec-04, at PM 06:12, Guillaume Laforge wrote:

> Hello Russel,
>
(Continue reading)

Guillaume Laforge | 1 Dec 2004 11:43
Gravatar

Re: Is this a buffering problem?

Well, perhaps try to flush the streams.
With the flush() method.

--
Guillaume Laforge
http://glaforge.free.fr/weblog

Atif Shahab wrote:
> javaProcess.out.println(myLengthOfFirstSide)
> 
> rather than sending the out stream to the process, is instead printing 
> to the screen
> 
> -------------- trun.pl -------------
> #!/usr/bin/env perl
> 
> print "Enter length of first side: ";
> $n = <STDIN>;
> --------------------------------------
> 
> Groovy script is
> 
> #!/usr/bin/env groovy
> javaProcess = "/full/path/to/trun.pl".execute();
> javaProcess.out.println( "2" );
> javaProcess.in.eachLine{ line | println line };
> 
> output is
> 
> 2
(Continue reading)

Russel Winder | 1 Dec 2004 12:59
Picon
Gravatar

Re: Is this a buffering problem?

On Wed, 2004-12-01 at 10:37, Atif Shahab wrote:

> rather than sending the out stream to the process, is instead printing 
> to the screen

It does seem that this is the case doesn't it.  I am not sure how that
can happen though, I would have thought that javaProcess.out was a
stream to the process and not an alias for System.out.

--

-- 
Russel.
=============================================
Dr Russel Winder         +44 20 7585 2200
41 Buckmaster Road       +44 7770 465 077
London SW11 1EN, UK      russel@...

Guillaume Laforge | 1 Dec 2004 13:22
Gravatar

Re: Is this a buffering problem?

Russel Winder wrote:
> It does seem that this is the case doesn't it.  I am not sure how that
> can happen though, I would have thought that javaProcess.out was a
> stream to the process and not an alias for System.out.

It's not an alias to System.out!
javaProcess.out is equivalent to javaProcess.getOutputStream().
That's the stream you can use to write to the process' input stream.

--
Guillaume Laforge
http://glaforge.free.fr/weblog

Daniel Serodio | 1 Dec 2004 13:38
Picon

Re: standard for loop

Just added it to the wiki, under "Looping". It's the first time I use 
Confluence, I hope I got it right.

[]'s
Daniel Serodio

Scott Stirling wrote:

> Awesome! Thanks to all for the help.
>
> Should I add this info to the Wiki somewhere? Just occurred to me . . .
>
> Scott S.
>
> On Nov 30, 2004, at 1:20 AM, Marc Hedlund wrote:
>
>>
>> Scott,
>>
>> In addition to the answers you've already gotten, I would also  
>> recommend
>> using closures where you would have used a for loop.  each() and
>> eachWithIndex() replace most for loops in my scripts.
>>
>> Some examples follow...
>>
>> /**********************/
>>
>> stringList = [ "java", "perl", "python", "ruby", "c#", "cobol",
>>                "groovy", "jython", "smalltalk", "prolog", "m", 
(Continue reading)

Guillaume Laforge | 1 Dec 2004 14:11
Gravatar

Re: standard for loop

Daniel Serodio wrote:
> Just added it to the wiki, under "Looping". It's the first time I use 
> Confluence, I hope I got it right.
> 
> []'s
> Daniel Serodio

Thanks a lot Daniel!

--
Guillaume Laforge
http://glaforge.free.fr/weblog

Russel Winder | 1 Dec 2004 15:13
Picon
Gravatar

Re: Is this a buffering problem?

On Wed, 2004-12-01 at 12:22, Guillaume Laforge wrote:
> Russel Winder wrote:
> > It does seem that this is the case doesn't it.  I am not sure how that
> > can happen though, I would have thought that javaProcess.out was a
> > stream to the process and not an alias for System.out.
> 
> It's not an alias to System.out!
> javaProcess.out is equivalent to javaProcess.getOutputStream().
> That's the stream you can use to write to the process' input stream.

Exactly what I thought.  The problem is though that the line:

	javaProcess.in.println("3")

appears to have resulted in the string "3\n" being sent to the console
and not to the process.  I even tried replacing the Java program with a
standard executable:

#! /usr/bin/env groovy
process = "tee flob".execute()
process.out.println("3")
process.getOut().println("4")
process.getOutputStream().println("5")
System.out.println("6")

which results in a file flob on the file system of size zero when it
should be of size 6 and the characters "3\n4\n5\n6\n" appearing on the
console.  Something somewhere is making all the lines that should be
sending characters to the spawned process behave identically to the last
line which simply send a character to the console.
(Continue reading)


Gmane