简体   繁体   中英

Java, reading and writing to input and output stream of Autoit script

I'm trying to write to the output stream and read the input stream of a simple Autoit script. If I do not use the newLine() character, I get the expected output: a line is sent to auto it, a line is sent to java, and that is repeated. If I add the newLine() character, it seems every cycle an extra line is sent to autoit. Why would this be?

Autoit:

Local $line

While (True)

    $line = ConsoleRead()

    ConsoleWrite( $line & "to java" & @LF )

    Sleep(25)

WEnd

Java:

p = Runtime.getRuntime().exec("Test");

in = new BufferedReader( new InputStreamReader(p.getInputStream()));
out = new BufferedWriter( new OutputStreamWriter(p.getOutputStream()));

int i=0;

out.write("(" + i++ + ") to autoit");
out.newLine();
out.flush();

while ((line = in.readLine()) != null) {

    System.out.println(line);

    out.write("(" + i + ") to autoit");
    out.newLine();
    out.flush();

    if(i++ > 9)
        p.destroy();
}

Output:

(0) to autoit
to java
(1) to autoit
(2) to autoit
to java
(3) to autoit
(4) to autoit
(5) to autoit
to java
(6) to autoit
(7) to autoit
(8) to autoit
(9) to autoit
to java

Output I Expected:

(0) to autoit
to java
(1) to autoit
to java
(2) to autoit
to java
(3) to autoit
to java
(4) to autoit
to java
(5) to autoit
to java
(6) to autoit
to java
(7) to autoit
to java
(8) to autoit
to java
(9) to autoit
to java

I'm no expert at this, not by any means, but consider these changes:

  • A problem with Autoit's ConsoleRead() is that it is not blocking and I believe doesn't recognize new lines. In other words, it's does not behave in any way similar to Java's Scanner.nextLine().
  • It may in fact read in a whole bunch of lines all at once, and I'm not sure if this can be predicted.
  • Consider using AutoIt's StringSplit(...) function to split out the lines using @CRLF as the delimiter, and then pushing each String in the resulting array to the standard out using ConsoleWrite(...)
  • Consider having AutoIt use the StringInStr(...) function to test for a token that tells it to exit.
  • On the Java side, I think that you will need to read the text from standard in on a separate thread from that which you write so as not to be blocking.
  • I have used Scanner to parse the standard in, and like using PrintStream for ease in outputting to the standard out.

For instance:

EchoCaller2.java

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Scanner;

public class EchoCaller2 {
   private static final String AUTO_IT_ECHOER = "Echoer.exe"; // AutoIt program
   private static Scanner scan = null;
   private static PrintStream out = null;

   public static void main(String[] args) throws IOException,
         InterruptedException {
      ProcessBuilder pb2 = new ProcessBuilder(AUTO_IT_ECHOER);
      pb2.redirectErrorStream();
      Process p = pb2.start();
      scan = new Scanner(p.getInputStream());
      out = new PrintStream(new BufferedOutputStream(p.getOutputStream()));

      new Thread(new Runnable() {
         public void run() {
            while (scan.hasNextLine()) {
               System.out.println(scan.nextLine());
            }
            scan.close();
         }
      }).start();

      for (int i = 0; i < 10; i++) {
         out.println("(" + i + ") to autoit ");
         out.flush();
      }

      out.println("exit ");
      out.flush();
      out.close();
   }
}

Echoer.au3

Local $line

While (True)

    $line &= ConsoleRead()

    $strArray = StringSplit($line, @CRLF)

    If $strArray[0] > 0 Then
        For $r = 1 to $strArray[0]
            If StringLen($strArray[$r]) > 0 Then
                ConsoleWrite($strArray[$r] & "to java" & @CRLF)
            EndIf
        Next
    EndIf

    If StringInStr($line, "exit") Then
        Exit
    EndIf

    $line = ""

    Sleep(25)

WEnd

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM