1. /*
  2. * @(#)ProcessImpl.java 1.30 04/03/25
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.lang;
  8. import java.io.*;
  9. /* This class is for the exclusive use of ProcessBuilder.start() to
  10. * create new processes.
  11. *
  12. * @author Martin Buchholz
  13. * @version 1.30, 04/03/25
  14. * @since 1.5
  15. */
  16. final class ProcessImpl extends Process {
  17. // System-dependent portion of ProcessBuilder.start()
  18. static Process start(String cmdarray[],
  19. java.util.Map<String,String> environment,
  20. String dir,
  21. boolean redirectErrorStream)
  22. throws IOException
  23. {
  24. String envblock = ProcessEnvironment.toEnvironmentBlock(environment);
  25. return new ProcessImpl(cmdarray, envblock, dir, redirectErrorStream);
  26. }
  27. private long handle = 0;
  28. private FileDescriptor stdin_fd;
  29. private FileDescriptor stdout_fd;
  30. private FileDescriptor stderr_fd;
  31. private OutputStream stdin_stream;
  32. private InputStream stdout_stream;
  33. private InputStream stderr_stream;
  34. private ProcessImpl(String cmd[],
  35. String envblock,
  36. String path,
  37. boolean redirectErrorStream)
  38. throws IOException
  39. {
  40. // Win32 CreateProcess requires cmd[0] to be normalized
  41. cmd[0] = new File(cmd[0]).getPath();
  42. StringBuilder cmdbuf = new StringBuilder(80);
  43. for (int i = 0; i < cmd.length; i++) {
  44. if (i > 0) {
  45. cmdbuf.append(' ');
  46. }
  47. String s = cmd[i];
  48. if (s.indexOf(' ') >= 0 || s.indexOf('\t') >= 0) {
  49. if (s.charAt(0) != '"') {
  50. cmdbuf.append('"');
  51. cmdbuf.append(s);
  52. if (s.endsWith("\\")) {
  53. cmdbuf.append("\\");
  54. }
  55. cmdbuf.append('"');
  56. } else if (s.endsWith("\"")) {
  57. /* The argument has already been quoted. */
  58. cmdbuf.append(s);
  59. } else {
  60. /* Unmatched quote for the argument. */
  61. throw new IllegalArgumentException();
  62. }
  63. } else {
  64. cmdbuf.append(s);
  65. }
  66. }
  67. String cmdstr = cmdbuf.toString();
  68. stdin_fd = new FileDescriptor();
  69. stdout_fd = new FileDescriptor();
  70. stderr_fd = new FileDescriptor();
  71. handle = create(cmdstr, envblock, path, redirectErrorStream,
  72. stdin_fd, stdout_fd, stderr_fd);
  73. java.security.AccessController.doPrivileged(
  74. new java.security.PrivilegedAction() {
  75. public Object run() {
  76. stdin_stream =
  77. new BufferedOutputStream(new FileOutputStream(stdin_fd));
  78. stdout_stream =
  79. new BufferedInputStream(new FileInputStream(stdout_fd));
  80. stderr_stream =
  81. new FileInputStream(stderr_fd);
  82. return null;
  83. }
  84. });
  85. }
  86. public OutputStream getOutputStream() {
  87. return stdin_stream;
  88. }
  89. public InputStream getInputStream() {
  90. return stdout_stream;
  91. }
  92. public InputStream getErrorStream() {
  93. return stderr_stream;
  94. }
  95. public void finalize() {
  96. close();
  97. }
  98. public native int exitValue();
  99. public native int waitFor();
  100. public native void destroy();
  101. private native long create(String cmdstr,
  102. String envblock,
  103. String dir,
  104. boolean redirectErrorStream,
  105. FileDescriptor in_fd,
  106. FileDescriptor out_fd,
  107. FileDescriptor err_fd);
  108. private native void close();
  109. }