You can have the code much shorter (and faster):

    // Only for use by ProcessImpl.start()
    String toEnvironmentBlock() {
        // Sort Unicode-case-insensitively by name
        Map.Entry<String,String>[] list = entrySet().toArray(new 
Map.Entry<>[size()]);
        Arrays.sort(list, entryComparator);

        StringBuilder sb = new StringBuilder(list.length*30);
        for (int i = 0, cmp = -1; ; i++) {
            Map.Entry<String,String> e = i < list.length ? list[i] : null;
            final String ROOT = "SystemRoot";
            if (cmp < 0 && (e == null || (cmp = nameComparator.compare(e.getKey(), 
ROOT)) > 0)) {
                String value = getenv(ROOT);
                if (value != null)
                    addEnv(sb, ROOT, value);
                // Ensure double NUL termination, even if environment is empty.
                else if (list.isEmpty())
                    sb.append('\u0000');
            }
            if (e == null)  break;
            addEnv(sb, e.getKey(), e.getValue());
        }
        sb.append('\u0000');
        return sb.toString();
    }

    // add the environment variable to the child
    private static void addEnv(StringBuilder sb, String key, String value) {
        sb.append(key+"="+value+'\u0000'); // under the hood, it compiles to 4 
sb.append()
    }

Do you like it?

Note: I think, if we use NameComparator, we can use "SystemRoot".

-Ulf


Am 14.04.2011 16:06, schrieb Michael McMahon:
An updated webrev is available for this fix. I'll probably go ahead
with the CCC request for the spec. change anyway.

http://cr.openjdk.java.net/~michaelm/7034570/webrev.2/


Thanks,
Michael.

Reply via email to