jdk/src/java.base/unix/classes/java/lang/ProcessEnvironment.java
author martin
Tue, 15 Sep 2015 21:56:04 -0700
changeset 32649 2ee9017c7597
parent 29986 97167d851fc4
permissions -rw-r--r--
8136583: Core libraries should use blessed modifier order Summary: Run blessed-modifier-order script (see bug) Reviewed-by: psandoz, chegar, alanb, plevart
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 11275
diff changeset
     2
 * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/* We use APIs that access the standard Unix environ array, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * is defined by UNIX98 to look like:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *    char **environ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * These are unsorted, case-sensitive, null-terminated arrays of bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * of the form FOO=BAR\000 which are usually encoded in the user's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * default encoding (file.encoding is an excellent choice for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * encoding/decoding these).  However, even though the user cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * directly access the underlying byte representation, we take pains
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * to pass on the child the exact byte representation we inherit from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * the parent process for any environment name or value not created by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * Javaland.  So we keep track of all the byte representations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * Internally, we define the types Variable and Value that exhibit
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * String/byteArray duality.  The internal representation of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * environment then looks like a Map<Variable,Value>.  But we don't
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * expose this to the user -- we only provide a Map<String,String>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * view, although we could also provide a Map<byte[],byte[]> view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * The non-private methods in this class are not for general use even
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * within this package.  Instead, they are the system-dependent parts
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * of the system-independent method of the same name.  Don't even
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * think of using this class unless your method's name appears below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @author  Martin Buchholz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * @since   1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
package java.lang;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
final class ProcessEnvironment
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private static final HashMap<Variable,Value> theEnvironment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private static final Map<String,String> theUnmodifiableEnvironment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    static final int MIN_NAME_LENGTH = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        // We cache the C environment.  This means that subsequent calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        // to putenv/setenv from C will not be visible from Java code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        byte[][] environ = environ();
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
    71
        theEnvironment = new HashMap<>(environ.length/2 + 3);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        // Read environment variables back to front,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        // so that earlier variables override later ones.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        for (int i = environ.length-1; i > 0; i-=2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            theEnvironment.put(Variable.valueOf(environ[i-1]),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                               Value.valueOf(environ[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        theUnmodifiableEnvironment
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            = Collections.unmodifiableMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            (new StringEnvironment(theEnvironment));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /* Only for use by System.getenv(String) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    static String getenv(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        return theUnmodifiableEnvironment.get(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /* Only for use by System.getenv() */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    static Map<String,String> getenv() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        return theUnmodifiableEnvironment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /* Only for use by ProcessBuilder.environment() */
11275
7cb0861d512f 7117612: Miscellaneous warnings in java.lang
omajid
parents: 9035
diff changeset
    94
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    static Map<String,String> environment() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        return new StringEnvironment
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            ((Map<Variable,Value>)(theEnvironment.clone()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /* Only for use by Runtime.exec(...String[]envp...) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    static Map<String,String> emptyEnvironment(int capacity) {
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 25859
diff changeset
   102
        return new StringEnvironment(new HashMap<>(capacity));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private static native byte[][] environ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    // This class is not instantiable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private ProcessEnvironment() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    // Check that name is suitable for insertion into Environment map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    private static void validateVariable(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (name.indexOf('=')      != -1 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            name.indexOf('\u0000') != -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                ("Invalid environment variable name: \"" + name + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    // Check that value is suitable for insertion into Environment map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    private static void validateValue(String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        if (value.indexOf('\u0000') != -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                ("Invalid environment variable value: \"" + value + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    // A class hiding the byteArray-String duality of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    // text data on Unixoid operating systems.
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 29986
diff changeset
   127
    private abstract static class ExternalData {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        protected final String str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        protected final byte[] bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        protected ExternalData(String str, byte[] bytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            this.str = str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            this.bytes = bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        public byte[] getBytes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            return bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            return str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            return o instanceof ExternalData
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                && arrayEquals(getBytes(), ((ExternalData) o).getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            return arrayHash(getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    private static class Variable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        extends ExternalData implements Comparable<Variable>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        protected Variable(String str, byte[] bytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            super(str, bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        public static Variable valueOfQueryOnly(Object str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            return valueOfQueryOnly((String) str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        public static Variable valueOfQueryOnly(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            return new Variable(str, str.getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        public static Variable valueOf(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            validateVariable(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            return valueOfQueryOnly(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        public static Variable valueOf(byte[] bytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            return new Variable(new String(bytes), bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        public int compareTo(Variable variable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            return arrayCompare(getBytes(), variable.getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            return o instanceof Variable && super.equals(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    private static class Value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        extends ExternalData implements Comparable<Value>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        protected Value(String str, byte[] bytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            super(str, bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        public static Value valueOfQueryOnly(Object str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            return valueOfQueryOnly((String) str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        public static Value valueOfQueryOnly(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            return new Value(str, str.getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        public static Value valueOf(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            validateValue(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            return valueOfQueryOnly(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        public static Value valueOf(byte[] bytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            return new Value(new String(bytes), bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        public int compareTo(Value value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            return arrayCompare(getBytes(), value.getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            return o instanceof Value && super.equals(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    // This implements the String map view the user sees.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    private static class StringEnvironment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        extends AbstractMap<String,String>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        private Map<Variable,Value> m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        private static String toString(Value v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            return v == null ? null : v.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        public StringEnvironment(Map<Variable,Value> m) {this.m = m;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        public int size()        {return m.size();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        public boolean isEmpty() {return m.isEmpty();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        public void clear()      {       m.clear();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        public boolean containsKey(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            return m.containsKey(Variable.valueOfQueryOnly(key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        public boolean containsValue(Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            return m.containsValue(Value.valueOfQueryOnly(value));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        public String get(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            return toString(m.get(Variable.valueOfQueryOnly(key)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        public String put(String key, String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            return toString(m.put(Variable.valueOf(key),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                                  Value.valueOf(value)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        public String remove(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            return toString(m.remove(Variable.valueOfQueryOnly(key)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        public Set<String> keySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            return new StringKeySet(m.keySet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        public Set<Map.Entry<String,String>> entrySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            return new StringEntrySet(m.entrySet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        public Collection<String> values() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            return new StringValues(m.values());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        // It is technically feasible to provide a byte-oriented view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        // as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        //      public Map<byte[],byte[]> asByteArrayMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        //          return new ByteArrayEnvironment(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        //      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        // Convert to Unix style environ as a monolithic byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        // inspired by the Windows Environment Block, except we work
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        // exclusively with bytes instead of chars, and we need only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        // one trailing NUL on Unix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        // This keeps the JNI as simple and efficient as possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        public byte[] toEnvironmentBlock(int[]envc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            int count = m.size() * 2; // For added '=' and NUL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            for (Map.Entry<Variable,Value> entry : m.entrySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                count += entry.getKey().getBytes().length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                count += entry.getValue().getBytes().length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            byte[] block = new byte[count];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            for (Map.Entry<Variable,Value> entry : m.entrySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                byte[] key   = entry.getKey  ().getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                byte[] value = entry.getValue().getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                System.arraycopy(key, 0, block, i, key.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                i+=key.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                block[i++] = (byte) '=';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                System.arraycopy(value, 0, block, i, value.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                i+=value.length + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                // No need to write NUL byte explicitly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                //block[i++] = (byte) '\u0000';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            envc[0] = m.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            return block;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    static byte[] toEnvironmentBlock(Map<String,String> map, int[]envc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        return map == null ? null :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            ((StringEnvironment)map).toEnvironmentBlock(envc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    private static class StringEntry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        implements Map.Entry<String,String>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        private final Map.Entry<Variable,Value> e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        public StringEntry(Map.Entry<Variable,Value> e) {this.e = e;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        public String getKey()   {return e.getKey().toString();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        public String getValue() {return e.getValue().toString();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        public String setValue(String newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            return e.setValue(Value.valueOf(newValue)).toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        public String toString() {return getKey() + "=" + getValue();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            return o instanceof StringEntry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                && e.equals(((StringEntry)o).e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        public int hashCode()    {return e.hashCode();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    private static class StringEntrySet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        extends AbstractSet<Map.Entry<String,String>>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        private final Set<Map.Entry<Variable,Value>> s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        public StringEntrySet(Set<Map.Entry<Variable,Value>> s) {this.s = s;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        public int size()        {return s.size();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        public boolean isEmpty() {return s.isEmpty();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        public void clear()      {       s.clear();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        public Iterator<Map.Entry<String,String>> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            return new Iterator<Map.Entry<String,String>>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                Iterator<Map.Entry<Variable,Value>> i = s.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                public boolean hasNext() {return i.hasNext();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                public Map.Entry<String,String> next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                    return new StringEntry(i.next());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                public void remove() {i.remove();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        private static Map.Entry<Variable,Value> vvEntry(final Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            if (o instanceof StringEntry)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                return ((StringEntry)o).e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            return new Map.Entry<Variable,Value>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                public Variable getKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                    return Variable.valueOfQueryOnly(((Map.Entry)o).getKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                public Value getValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                    return Value.valueOfQueryOnly(((Map.Entry)o).getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                public Value setValue(Value value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                    throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        public boolean contains(Object o) { return s.contains(vvEntry(o)); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        public boolean remove(Object o)   { return s.remove(vvEntry(o)); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            return o instanceof StringEntrySet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                && s.equals(((StringEntrySet) o).s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        public int hashCode() {return s.hashCode();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    private static class StringValues
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
          extends AbstractCollection<String>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        private final Collection<Value> c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        public StringValues(Collection<Value> c) {this.c = c;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        public int size()        {return c.size();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        public boolean isEmpty() {return c.isEmpty();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        public void clear()      {       c.clear();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        public Iterator<String> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            return new Iterator<String>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                Iterator<Value> i = c.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                public boolean hasNext() {return i.hasNext();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                public String next()     {return i.next().toString();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                public void remove()     {i.remove();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            return c.contains(Value.valueOfQueryOnly(o));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            return c.remove(Value.valueOfQueryOnly(o));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            return o instanceof StringValues
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                && c.equals(((StringValues)o).c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        public int hashCode() {return c.hashCode();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    private static class StringKeySet extends AbstractSet<String> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        private final Set<Variable> s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        public StringKeySet(Set<Variable> s) {this.s = s;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        public int size()        {return s.size();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        public boolean isEmpty() {return s.isEmpty();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        public void clear()      {       s.clear();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        public Iterator<String> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            return new Iterator<String>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                Iterator<Variable> i = s.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                public boolean hasNext() {return i.hasNext();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                public String next()     {return i.next().toString();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                public void remove()     {       i.remove();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            return s.contains(Variable.valueOfQueryOnly(o));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            return s.remove(Variable.valueOfQueryOnly(o));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    // Replace with general purpose method someday
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    private static int arrayCompare(byte[]x, byte[] y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        int min = x.length < y.length ? x.length : y.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        for (int i = 0; i < min; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            if (x[i] != y[i])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                return x[i] - y[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        return x.length - y.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    // Replace with general purpose method someday
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    private static boolean arrayEquals(byte[] x, byte[] y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        if (x.length != y.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        for (int i = 0; i < x.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            if (x[i] != y[i])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    // Replace with general purpose method someday
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    private static int arrayHash(byte[] x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        int hash = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        for (int i = 0; i < x.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            hash = 31 * hash + x[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        return hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
}