ProcessEnvironment.java revision 0
0N/A * Copyright 2003-2004 Sun Microsystems, Inc. All Rights Reserved. 0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/A * This code is free software; you can redistribute it and/or modify it 0N/A * under the terms of the GNU General Public License version 2 only, as 0N/A * published by the Free Software Foundation. Sun designates this 0N/A * particular file as subject to the "Classpath" exception as provided 0N/A * by Sun in the LICENSE file that accompanied this code. 0N/A * This code is distributed in the hope that it will be useful, but WITHOUT 0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/A * version 2 for more details (a copy is included in the LICENSE file that 0N/A * accompanied this code). 0N/A * You should have received a copy of the GNU General Public License version 0N/A * 2 along with this work; if not, write to the Free Software Foundation, 0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 553N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 553N/A * CA 95054 USA or visit www.sun.com if you need additional information or 0N/A/* We use APIs that access the standard Unix environ array, which 0N/A * is defined by UNIX98 to look like: 0N/A * These are unsorted, case-sensitive, null-terminated arrays of bytes 0N/A * of the form FOO=BAR\000 which are usually encoded in the user's 0N/A * default encoding (file.encoding is an excellent choice for 0N/A * directly access the underlying byte representation, we take pains 0N/A * to pass on the child the exact byte representation we inherit from 0N/A * the parent process for any environment name or value not created by 0N/A * Javaland. So we keep track of all the byte representations. 0N/A * Internally, we define the types Variable and Value that exhibit 0N/A * environment then looks like a Map<Variable,Value>. But we don't 0N/A * expose this to the user -- we only provide a Map<String,String> 0N/A * view, although we could also provide a Map<byte[],byte[]> view. * The non-private methods in this class are not for general use even * within this package. Instead, they are the system-dependent parts * of the system-independent method of the same name. Don't even * think of using this class unless your method's name appears below. * @author Martin Buchholz // We cache the C environment. This means that subsequent calls // to putenv/setenv from C will not be visible from Java code. // Read environment variables back to front, // so that earlier variables override later ones. /* Only for use by System.getenv(String) */ /* Only for use by System.getenv() */ /* Only for use by ProcessBuilder.environment() */ /* Only for use by Runtime.exec(...String[]envp...) */ private static native byte[][]
environ();
// This class is not instantiable. // Check that name is suitable for insertion into Environment map (
"Invalid environment variable name: \"" +
name +
"\"");
// Check that value is suitable for insertion into Environment map (
"Invalid environment variable value: \"" +
value +
"\"");
// A class hiding the byteArray-String duality of // text data on Unixoid operating systems. protected final byte[]
bytes;
private static class Value // This implements the String map view the user sees. // It is technically feasible to provide a byte-oriented view // public Map<byte[],byte[]> asByteArrayMap() { // return new ByteArrayEnvironment(m); // Convert to Unix style environ as a monolithic byte array // inspired by the Windows Environment Block, except we work // exclusively with bytes instead of chars, and we need only // one trailing NUL on Unix. // This keeps the JNI as simple and efficient as possible. int count = m.
size() *
2;
// For added '=' and NUL // No need to write NUL byte explicitly //block[i++] = (byte) '\u0000'; // Replace with general purpose method someday for (
int i =
0; i <
min; i++)
// Replace with general purpose method someday private static boolean arrayEquals(
byte[] x,
byte[] y) {
for (
int i =
0; i < x.
length; i++)
// Replace with general purpose method someday for (
int i =
0; i < x.
length; i++)