01: /*
02: * Copyright 2003-2007 Sun Microsystems, Inc. All Rights Reserved.
03: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
04: *
05: * This code is free software; you can redistribute it and/or modify it
06: * under the terms of the GNU General Public License version 2 only, as
07: * published by the Free Software Foundation. Sun designates this
08: * particular file as subject to the "Classpath" exception as provided
09: * by Sun in the LICENSE file that accompanied this code.
10: *
11: * This code is distributed in the hope that it will be useful, but WITHOUT
12: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14: * version 2 for more details (a copy is included in the LICENSE file that
15: * accompanied this code).
16: *
17: * You should have received a copy of the GNU General Public License version
18: * 2 along with this work; if not, write to the Free Software Foundation,
19: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20: *
21: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22: * CA 95054 USA or visit www.sun.com if you need additional information or
23: * have any questions.
24: */
25:
26: package java.lang;
27:
28: import java.io.IOException;
29: import java.lang.Process;
30:
31: /**
32: * This class is for the exclusive use of ProcessBuilder.start() to
33: * create new processes.
34: *
35: * @author Martin Buchholz
36: * @version 1.10, 07/07/08
37: * @since 1.5
38: */
39: final class ProcessImpl {
40: private ProcessImpl() {
41: } // Not instantiable
42:
43: private static byte[] toCString(String s) {
44: if (s == null)
45: return null;
46: byte[] bytes = s.getBytes();
47: byte[] result = new byte[bytes.length + 1];
48: System.arraycopy(bytes, 0, result, 0, bytes.length);
49: result[result.length - 1] = (byte) 0;
50: return result;
51: }
52:
53: // Only for use by ProcessBuilder.start()
54: static Process start(String[] cmdarray,
55: java.util.Map<String, String> environment, String dir,
56: boolean redirectErrorStream) throws IOException {
57: assert cmdarray != null && cmdarray.length > 0;
58:
59: // Convert arguments to a contiguous block; it's easier to do
60: // memory management in Java than in C.
61: byte[][] args = new byte[cmdarray.length - 1][];
62: int size = args.length; // For added NUL bytes
63: for (int i = 0; i < args.length; i++) {
64: args[i] = cmdarray[i + 1].getBytes();
65: size += args[i].length;
66: }
67: byte[] argBlock = new byte[size];
68: int i = 0;
69: for (byte[] arg : args) {
70: System.arraycopy(arg, 0, argBlock, i, arg.length);
71: i += arg.length + 1;
72: // No need to write NUL bytes explicitly
73: }
74:
75: int[] envc = new int[1];
76: byte[] envBlock = ProcessEnvironment.toEnvironmentBlock(
77: environment, envc);
78:
79: return new UNIXProcess(toCString(cmdarray[0]), argBlock,
80: args.length, envBlock, envc[0], toCString(dir),
81: redirectErrorStream);
82: }
83: }
|