Return-Path: Delivered-To: apmail-commons-user-archive@www.apache.org Received: (qmail 77250 invoked from network); 23 Sep 2009 23:06:54 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 23 Sep 2009 23:06:54 -0000 Received: (qmail 83900 invoked by uid 500); 23 Sep 2009 23:06:52 -0000 Delivered-To: apmail-commons-user-archive@commons.apache.org Received: (qmail 83779 invoked by uid 500); 23 Sep 2009 23:06:52 -0000 Mailing-List: contact user-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "Commons Users List" Delivered-To: mailing list user@commons.apache.org Received: (qmail 83769 invoked by uid 99); 23 Sep 2009 23:06:52 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 23 Sep 2009 23:06:52 +0000 X-ASF-Spam-Status: No, hits=-0.6 required=10.0 tests=HTML_MESSAGE,RCVD_IN_DNSWL_MED,SPF_NEUTRAL X-Spam-Check-By: apache.org Received-SPF: neutral (nike.apache.org: local policy) Received: from [208.65.145.71] (HELO p01c12o148.mxlogic.net) (208.65.145.71) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 23 Sep 2009 23:06:41 +0000 Received: from unknown [216.166.12.178] by p01c12o148.mxlogic.net (mxl_mta-6.3.0-4) with SMTP id ae9aaba4.0.463338.00-012.987791.p01c12o148.mxlogic.net (envelope-from ); Wed, 23 Sep 2009 17:06:20 -0600 (MDT) X-MXL-Hash: 4abaa9ec6ae01c8d-065a384e817e95365b391688c564a629a8f8ff53 Received: from AUSP01VMBX08.collaborationhost.net ([10.2.8.97]) by AUSP01MHUB04.collaborationhost.net ([10.2.0.189]) with mapi; Wed, 23 Sep 2009 18:04:02 -0500 From: Arun Ramakrishnan To: "user@commons.apache.org" Date: Wed, 23 Sep 2009 18:06:06 -0500 Subject: [exec] Piping to Input Stream using PipedInputStream Thread-Topic: [exec] Piping to Input Stream using PipedInputStream Thread-Index: Aco8om4Nmu8zoYPrRTiKXWjg/ZFSlA== Message-ID: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: acceptlanguage: en-US Content-Type: multipart/alternative; boundary="_000_C3AD6464AC81DC4AB14FFEA31391866A62D33FEAC5AUSP01VMBX08c_" MIME-Version: 1.0 X-Spam: [F=0.2000000000; CM=0.500; S=0.200(2009092101)] X-MAIL-FROM: X-SOURCE-IP: [(unknown)] X-AnalysisOut: [v=1.0 c=1 a=Cki5b4d1iLUA:10 a=N0rcUYRaQg3G+2kUXxmxxA==:17 ] X-AnalysisOut: [a=I7HXOmfAB-e2QUt6tHIA:9 a=ngcQZj4IP7WzVSCiYqd5Vv5IeBQA:4 ] X-AnalysisOut: [a=J3nmpts1vhDpS6tP:21 a=SKAg9qMJEzlxT2SD:21 a=yMhMjlubAAAA] X-AnalysisOut: [:8 a=SSmOFEACAAAA:8 a=hybvrfIzYA1HrKGRFHoA:9 a=BacblTpSG1a] X-AnalysisOut: [oRFJOeLY97yJSgScA:4] X-Virus-Checked: Checked by ClamAV on apache.org --_000_C3AD6464AC81DC4AB14FFEA31391866A62D33FEAC5AUSP01VMBX08c_ Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable I am trying to build java wrapper around a binary. The idea is that I can s= tream in and out of this binary that has heavy startup time. 1) I used to be able to achieve this using ProcessBuilder ( refer to c= ode snippet 1 ) 2) I trying to use Apache Commons Exec instead to start the binaries (= it see from the code that it has separate threads to deal with the various= streams so that the binary dosent block due to full output or error buffer= s ). But, it seems extremely complicated to open a stream/pipe to the inpu= t of the binary. I am not sure if Code snippert 2 is the right approach to = it using Piped Streams classes. 3) One of the problems I am having is that I don't understand why the = pipe seems to close prematurely after only one write to it. Please refer to= code snippet 3 for a concise code that demonstrates the problem I am havin= g. Any suggestions would be appreciated. ######################## Code Snippet 1 ############################## Process p =3D "tool.exe".execute() // uses processBuilder BufferedOutputStream pout =3D new BufferedOutputStream( p.getOutputStream()= ) // then use pout to send input to the tool.exe's input stream ######################## Code Snippet 3 ############################## PipedOutputStream ps =3D new PipedOutputStream(); PipedInputStream is =3D new PipedInputStream(ps); BufferedWriter os =3D new BufferedWriter(new OutputStreamWriter(ps)); String s =3D "hi there \n hi \n"; [1..1000].each{ println it os.write(s); } os.close(); println is.getText(); ######################## Code Snippet 2 ################################ import org.apache.commons.exec.*; String line =3D "F:\\Suggester\\bin\\langid.bat" ; String infile =3D "F:\\Suggester\\bin\\in.txt" ; CommandLine commandLine =3D CommandLine.parse(line); commandLine.addArgument("engita") DefaultExecutor executor =3D new DefaultExecutor(); FileInputStream fis =3D new FileInputStream( infile ) PipedOutputStream ps =3D new PipedOutputStream(); PipedInputStream is =3D new PipedInputStream(ps); PrintStream os =3D new PrintStream(ps); String s =3D "hi there \n--#--\nwhatup"; [1..1000].each{ println it os.append(s); os.flush(); } os.close(); println is.getText() PumpStreamHandler ioh =3D new PumpStreamHandler(System.out,System.err,is); //ioh.setProcessInputStream(System.out) his will not work ioh.start() executor.setStreamHandler(ioh) //pis.write( s.getBytes() ); int exitValue =3D executor.execute(commandLine); println " execution started" println exitValue; ####################### End ########################################## --_000_C3AD6464AC81DC4AB14FFEA31391866A62D33FEAC5AUSP01VMBX08c_--