Process.hx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright (C)2005-2015 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package sys.io;
  23. import haxe.io.Bytes;
  24. import haxe.io.BytesInput;
  25. import haxe.io.Eof;
  26. import java.io.IOException;
  27. import java.io.EOFException;
  28. import java.NativeArray;
  29. @:coreApi
  30. class Process {
  31. public var stdout(default,null) : haxe.io.Input;
  32. public var stderr(default,null) : haxe.io.Input;
  33. public var stdin(default, null) : haxe.io.Output;
  34. private var proc:java.lang.Process;
  35. public function new( cmd : String, args : Array<String> ) : Void
  36. {
  37. var pargs = new NativeArray(args.length + 1);
  38. pargs[0] = cmd;
  39. for (i in 0...args.length)
  40. {
  41. pargs[i + 1] = args[i];
  42. }
  43. try
  44. {
  45. proc = new java.lang.ProcessBuilder(pargs).start();
  46. }
  47. catch (e:Dynamic) { throw e; } //wrapping typed exceptions
  48. var p = proc;
  49. stderr = new ProcessInput(p.getErrorStream());
  50. stdout = new ProcessInput(p.getInputStream());
  51. stdin = new java.io.NativeOutput(p.getOutputStream());
  52. }
  53. public function getPid() : Int
  54. {
  55. if (Reflect.hasField(proc, "pid"))
  56. return Reflect.field(proc, "pid");
  57. return -1;
  58. }
  59. public function exitCode() : Int
  60. {
  61. cast(stdout, ProcessInput).bufferContents();
  62. cast(stderr, ProcessInput).bufferContents();
  63. try
  64. {
  65. proc.waitFor();
  66. }
  67. catch (e:Dynamic) { throw e; }
  68. return proc.exitValue();
  69. }
  70. public function close() : Void
  71. {
  72. proc.destroy();
  73. }
  74. public function kill() : Void
  75. {
  76. proc.destroy();
  77. }
  78. }
  79. private class ProcessInput extends java.io.NativeInput
  80. {
  81. private var chained:BytesInput;
  82. public function bufferContents():Void
  83. {
  84. if (chained != null) return;
  85. var b = this.readAll();
  86. chained = new BytesInput(b);
  87. }
  88. override public function readByte():Int
  89. {
  90. if (chained != null)
  91. return chained.readByte();
  92. var ret = 0;
  93. try
  94. {
  95. ret = stream.read();
  96. }
  97. catch (e:IOException) {
  98. throw haxe.io.Error.Custom(e);
  99. }
  100. if ( ret == -1 )
  101. throw new Eof();
  102. return ret;
  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. }