J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2003-2007 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * 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 | * @since 1.5 |
| 37 | */ |
| 38 | final class ProcessImpl { |
| 39 | private ProcessImpl() {} // Not instantiable |
| 40 | |
| 41 | private static byte[] toCString(String s) { |
| 42 | if (s == null) |
| 43 | return null; |
| 44 | byte[] bytes = s.getBytes(); |
| 45 | byte[] result = new byte[bytes.length + 1]; |
| 46 | System.arraycopy(bytes, 0, |
| 47 | result, 0, |
| 48 | 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, |
| 56 | String dir, |
| 57 | boolean redirectErrorStream) |
| 58 | throws IOException |
| 59 | { |
| 60 | assert cmdarray != null && cmdarray.length > 0; |
| 61 | |
| 62 | // Convert arguments to a contiguous block; it's easier to do |
| 63 | // memory management in Java than in C. |
| 64 | byte[][] args = new byte[cmdarray.length-1][]; |
| 65 | int size = args.length; // For added NUL bytes |
| 66 | for (int i = 0; i < args.length; i++) { |
| 67 | args[i] = cmdarray[i+1].getBytes(); |
| 68 | size += args[i].length; |
| 69 | } |
| 70 | byte[] argBlock = new byte[size]; |
| 71 | int i = 0; |
| 72 | for (byte[] arg : args) { |
| 73 | System.arraycopy(arg, 0, argBlock, i, arg.length); |
| 74 | i += arg.length + 1; |
| 75 | // No need to write NUL bytes explicitly |
| 76 | } |
| 77 | |
| 78 | int[] envc = new int[1]; |
| 79 | byte[] envBlock = ProcessEnvironment.toEnvironmentBlock(environment, envc); |
| 80 | |
| 81 | return new UNIXProcess |
| 82 | (toCString(cmdarray[0]), |
| 83 | argBlock, args.length, |
| 84 | envBlock, envc[0], |
| 85 | toCString(dir), |
| 86 | redirectErrorStream); |
| 87 | } |
| 88 | } |