Process.hx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (c) 2005-2007, The haXe Project Contributors
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * - Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * - Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
  17. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  23. * DAMAGE.
  24. */
  25. package sys.io;
  26. import haxe.io.Bytes;
  27. import haxe.io.BytesInput;
  28. import haxe.io.Eof;
  29. import java.io.Exceptions;
  30. import java.NativeArray;
  31. class Process {
  32. public var stdout(default,null) : haxe.io.Input;
  33. public var stderr(default,null) : haxe.io.Input;
  34. public var stdin(default, null) : haxe.io.Output;
  35. private var proc:java.lang.Process;
  36. public function new( cmd : String, args : Array<String> ) : Void
  37. {
  38. var pargs = new NativeArray(args.length + 1);
  39. pargs[0] = cmd;
  40. for (i in 0...args.length)
  41. {
  42. pargs[i + 1] = args[i];
  43. }
  44. try
  45. {
  46. proc = new java.lang.ProcessBuilder(pargs).start();
  47. }
  48. catch (e:Dynamic) { throw e; } //wrapping typed exceptions
  49. var p = proc;
  50. stderr = new ProcessInput(p.getErrorStream());
  51. stdout = new ProcessInput(p.getInputStream());
  52. stdin = new java.io.NativeOutput(p.getOutputStream());
  53. }
  54. public function getPid() : Int
  55. {
  56. if (Reflect.hasField(proc, "pid"))
  57. return Reflect.field(proc, "pid");
  58. return -1;
  59. }
  60. public function exitCode() : Int
  61. {
  62. cast(stdout, ProcessInput).bufferContents();
  63. cast(stderr, ProcessInput).bufferContents();
  64. try
  65. {
  66. proc.waitFor();
  67. }
  68. catch (e:Dynamic) { throw e; }
  69. return proc.exitValue();
  70. }
  71. public function close() : Void
  72. {
  73. proc.destroy();
  74. }
  75. public function kill() : Void
  76. {
  77. proc.destroy();
  78. }
  79. }
  80. private class ProcessInput extends java.io.NativeInput
  81. {
  82. private var chained:BytesInput;
  83. public function bufferContents():Void
  84. {
  85. if (chained != null) return;
  86. var b = this.readAll();
  87. chained = new BytesInput(b);
  88. }
  89. override public function readByte():Int
  90. {
  91. if (chained != null)
  92. return chained.readByte();
  93. try
  94. {
  95. return stream.read();
  96. }
  97. catch (e:EOFException) {
  98. throw new Eof();
  99. }
  100. catch (e:IOException) {
  101. throw haxe.io.Error.Custom(e);
  102. }
  103. }
  104. override public function readBytes(s:Bytes, pos:Int, len:Int):Int
  105. {
  106. if (chained != null)
  107. return chained.readBytes(s, pos, len);
  108. var ret = -1;
  109. try
  110. {
  111. ret = stream.read(s.getData(), pos, len);
  112. }
  113. catch (e:EOFException) {
  114. throw new Eof();
  115. }
  116. catch (e:IOException) {
  117. throw haxe.io.Error.Custom(e);
  118. }
  119. if (ret == -1)
  120. throw new Eof();
  121. return ret;
  122. }
  123. override public function close():Void
  124. {
  125. if (chained != null)
  126. chained.close();
  127. try
  128. {
  129. stream.close();
  130. }
  131. catch (e:IOException) {
  132. throw haxe.io.Error.Custom(e);
  133. }
  134. }
  135. }