java - PipedInputStream, PipedOutputStream and Process -


i'm learning java pipedinputstream/pipeoutputstream .

i'd read stdin (the 'source' class below) , redirect process ( here 'grep a'), output of grep redirected system.out.

to consumme stdout , stderr after grep, created class copyto redirect inputstream outputstream.

import java.io.*; class test     {     private static class source         implements runnable         {         private pipedoutputstream pipedoutputstream=new pipedoutputstream();         private inputstream in;              source(inputstream in) throws ioexception                 {                 this.in=in;                 }               @override             public void run()                 {                 try                     {                     int c;                     while((c=this.in.read())!=-1)                         {                         pipedoutputstream.write(c);                         }                     pipedoutputstream.flush();                     pipedoutputstream.close();                     }             catch(exception err)                 {                 err.printstacktrace();                 }                 }         }      private static class grep         implements runnable         {          private pipedinputstream pipeinpipedinputstream;          public grep(source src) throws ioexception             {             this.pipeinpipedinputstream=new pipedinputstream(src.pipedoutputstream);             }              @override             public void run()                 {             try {                 process proc=runtime.getruntime().exec(new string[]{                     "/bin/grep",                     "a"});                 outputstream os=proc.getoutputstream();                   thread t1=new thread(new copyto(proc.geterrorstream(),system.err));                 thread t2=new thread(new copyto(proc.getinputstream(),system.out));                 t1.start();                 t2.start();                 int c;                 while((c=this.pipeinpipedinputstream.read())!=-1)                     {                        os.write((char)c);                     }                  t1.join();                 t2.join();                 }              catch (exception e) {                 e.printstacktrace();                 }                 }         }      private static class copyto implements runnable         {         private inputstream in;         private outputstream out;         copyto(inputstream in,outputstream out)             {             this.in=in;             this.out=out;             }         @override         public void run() {             try {                 int c;                 while((c=in.read())!=-1)                     {                     out.write(c);                     }                 }             catch (exception e)                 {                 e.printstacktrace();                 }             }         }       public static void main(string[] args)         {         try             {             source src=new source(system.in);             thread t1=new thread(src);             thread t2=new thread(new grep(src));             t1.start();             t2.start();             }         catch(exception err)             {             err.printstacktrace();             }         }      } 

however, compiling , running program produces no output (and program frozen).

$ javac test.java && echo -e "a\nt\ng\nc" | java test 

where wrong ? thanks.

your need flush , close os stream in method run() of grep class after block of code:

while((c=this.pipeinpipedinputstream.read())!=-1) {        os.write((char)c); } 

add lines:

os.flush(); os.close(); 

and method run() of grep class must like:

@override public void run() {     try {         process proc=runtime.getruntime().exec(new string[]{              "/bin/grep",              "a"});         outputstream os=proc.getoutputstream();          thread t1=new thread(new copyto(proc.geterrorstream(),system.err));         thread t2=new thread(new copyto(proc.getinputstream(),system.out));         t1.start();         t2.start();         int c;         while((c=this.pipeinpipedinputstream.read())!=-1)         {                 os.write((char)c);         }          //missing lines of code         os.flush();         os.close();          t1.join();         t2.join();     }      catch (exception e) {         e.printstacktrace();     } } 

the output of command:

$ javac test.java && echo -e "a\nt\ng\nc" | java test 

will be:

a 

and program terminated.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -