Process.hx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. @:coreApi
  32. class Process {
  33. public var stdout(default,null) : haxe.io.Input;
  34. public var stderr(default,null) : haxe.io.Input;
  35. public var stdin(default, null) : haxe.io.Output;
  36. private var proc:java.lang.Process;
  37. public function new( cmd : String, args : Array<String> ) : Void
  38. {
  39. var pargs = new NativeArray(args.length + 1);
  40. pargs[0] = cmd;
  41. for (i in 0...args.length)
  42. {
  43. pargs[i + 1] = args[i];
  44. }
  45. try
  46. {
  47. proc = new java.lang.ProcessBuilder(pargs).start();
  48. }
  49. catch (e:Dynamic) { throw e; } //wrapping typed exceptions
  50. var p = proc;
  51. stderr = new ProcessInput(p.getErrorStream());
  52. stdout = new ProcessInput(p.getInputStream());
  53. stdin = new java.io.NativeOutput(p.getOutputStream());
  54. }
  55. public function getPid() : Int
  56. {
  57. if (Reflect.hasField(proc, "pid"))
  58. return Reflect.field(proc, "pid");
  59. return -1;
  60. }
  61. public function exitCode() : Int
  62. {
  63. cast(stdout, ProcessInput).bufferContents();
  64. cast(stderr, ProcessInput).bufferContents();
  65. try
  66. {
  67. proc.waitFor();
  68. }
  69. catch (e:Dynamic) { throw e; }
  70. return proc.exitValue();
  71. }
  72. public function close() : Void
  73. {
  74. proc.destroy();
  75. }
  76. public function kill() : Void
  77. {
  78. proc.destroy();
  79. }
  80. }
  81. private class ProcessInput extends java.io.NativeInput
  82. {
  83. private var chained:BytesInput;
  84. public function bufferContents():Void
  85. {
  86. if (chained != null) return;
  87. var b = this.readAll();
  88. chained = new BytesInput(b);
  89. }
  90. override public function readByte():Int
  91. {
  92. if (chained != null)
  93. return chained.readByte();
  94. try
  95. {
  96. return stream.read();
  97. }
  98. catch (e:EOFException) {
  99. throw new Eof();
  100. }
  101. catch (e:IOException) {
  102. throw haxe.io.Error.Custom(e);
  103. }
  104. }
  105. override public function readBytes(s:Bytes, pos:Int, len:Int):Int
  106. {
  107. if (chained != null)
  108. return chained.readBytes(s, pos, len);
  109. var ret = -1;
  110. try
  111. {
  112. ret = stream.read(s.getData(), pos, len);
  113. }
  114. catch (e:EOFException) {
  115. throw new Eof();
  116. }
  117. catch (e:IOException) {
  118. throw haxe.io.Error.Custom(e);
  119. }
  120. if (ret == -1)
  121. throw new Eof();
  122. return ret;
  123. }
  124. override public function close():Void
  125. {
  126. if (chained != null)
  127. chained.close();
  128. try
  129. {
  130. stream.close();
  131. }
  132. catch (e:IOException) {
  133. throw haxe.io.Error.Custom(e);
  134. }
  135. }
  136. }