2

I need to process some text output from a long running command on windows. To get the results of this process as early as possible i am using the Scala Stream and ProcessBuilder class.

Everything runs flawless, but i run into a character encoding problem.

Here is my striped down source code (the powershell command is just a substitution for the real executable).

import scala.sys.process._

object CP850TEST extends Application{
  val cmd = Seq("powershell", "-command", "echo 1a; Start-Sleep -s 1; echo 2äüîß; Start-Sleep -s 1 ; echo 3end")
  val lines:Stream[String] = cmd.lines
  lines.foreach(println)
}

The Output schould looks like:

1a
2äüîß
3end

but displays only:

1a
2����
3end

To solve this Problem in Java, I would declare the charset (Cp850) of the InputStream, but I can't find any solution in Scala:

public static void main(String[] args) throws IOException {
    ProcessBuilder pb = new ProcessBuilder("powershell", "-command", "echo 1a; Start-Sleep -s 1; echo 2äüîß; Start-Sleep -s 1 ; echo 3end");
    Process process = pb.start();
    Scanner scanner = new Scanner(process.getInputStream(), "Cp850");
    while ( scanner.hasNextLine() ) {
        String s = scanner.nextLine();
        System.out.println( s );
    }
    scanner.close();
}
Sascha
  • 937
  • 6
  • 14
  • check out this post, hopefully you can use the ISO-8859-1 encoder: http://stackoverflow.com/a/13625541/3248346 –  Mar 30 '14 at 21:30
  • @I.K.: It won't work, Sascha's stream was mangled in an opposite way. – Karol S Mar 31 '14 at 01:21

1 Answers1

4

I think the only way is to use ProcessIO.

So instead of:

cmd.lines

You'll have to do:

cmd.run(new ProcessIO(
  i => i.close, 
  o => 
    Source.fromInputStream(o, "Cp850").getLines.foreach { line =>
      // your callback here
    },
  e => e.close))
Karol S
  • 9,028
  • 2
  • 32
  • 45