1 7 8 package java.lang; 9 10 import java.io.*; 11 12 19 20 final class ProcessImpl extends Process { 21 22 static Process start(String cmdarray[], 24 java.util.Map <String ,String > environment, 25 String dir, 26 boolean redirectErrorStream) 27 throws IOException 28 { 29 String envblock = ProcessEnvironment.toEnvironmentBlock(environment); 30 return new ProcessImpl (cmdarray, envblock, dir, redirectErrorStream); 31 } 32 33 private long handle = 0; 34 private FileDescriptor stdin_fd; 35 private FileDescriptor stdout_fd; 36 private FileDescriptor stderr_fd; 37 private OutputStream stdin_stream; 38 private InputStream stdout_stream; 39 private InputStream stderr_stream; 40 41 private ProcessImpl(String cmd[], 42 String envblock, 43 String path, 44 boolean redirectErrorStream) 45 throws IOException 46 { 47 cmd[0] = new File(cmd[0]).getPath(); 49 50 StringBuilder cmdbuf = new StringBuilder (80); 51 for (int i = 0; i < cmd.length; i++) { 52 if (i > 0) { 53 cmdbuf.append(' '); 54 } 55 String s = cmd[i]; 56 if (s.indexOf(' ') >= 0 || s.indexOf('\t') >= 0) { 57 if (s.charAt(0) != '"') { 58 cmdbuf.append('"'); 59 cmdbuf.append(s); 60 if (s.endsWith("\\")) { 61 cmdbuf.append("\\"); 62 } 63 cmdbuf.append('"'); 64 } else if (s.endsWith("\"")) { 65 66 cmdbuf.append(s); 67 } else { 68 69 throw new IllegalArgumentException (); 70 } 71 } else { 72 cmdbuf.append(s); 73 } 74 } 75 String cmdstr = cmdbuf.toString(); 76 77 stdin_fd = new FileDescriptor(); 78 stdout_fd = new FileDescriptor(); 79 stderr_fd = new FileDescriptor(); 80 81 handle = create(cmdstr, envblock, path, redirectErrorStream, 82 stdin_fd, stdout_fd, stderr_fd); 83 84 java.security.AccessController.doPrivileged( 85 new java.security.PrivilegedAction () { 86 public Object run() { 87 stdin_stream = 88 new BufferedOutputStream(new FileOutputStream(stdin_fd)); 89 stdout_stream = 90 new BufferedInputStream(new FileInputStream(stdout_fd)); 91 stderr_stream = 92 new FileInputStream(stderr_fd); 93 return null; 94 } 95 }); 96 } 97 98 public OutputStream getOutputStream() { 99 return stdin_stream; 100 } 101 102 public InputStream getInputStream() { 103 return stdout_stream; 104 } 105 106 public InputStream getErrorStream() { 107 return stderr_stream; 108 } 109 110 public void finalize() { 111 close(); 112 } 113 114 public native int exitValue(); 115 public native int waitFor(); 116 public native void destroy(); 117 118 private native long create(String cmdstr, 119 String envblock, 120 String dir, 121 boolean redirectErrorStream, 122 FileDescriptor in_fd, 123 FileDescriptor out_fd, 124 FileDescriptor err_fd); 125 private native void close(); 126 } 127 | Popular Tags |