jdk/src/java.base/share/classes/sun/nio/fs/Util.java
author redestad
Tue, 03 May 2016 15:50:54 +0200
changeset 37781 71ed5645f17c
parent 37593 824750ada3d6
permissions -rw-r--r--
8155775: Re-examine naming of privileged methods to access System properties Reviewed-by: mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 21290
diff changeset
     2
 * Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved.
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
     4
 *
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
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: 3065
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3065
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    10
 *
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    15
 * accompanied this code).
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    16
 *
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3065
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3065
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3065
diff changeset
    23
 * questions.
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    24
 */
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    25
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    26
package sun.nio.fs;
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    27
8808
fc799c458da8 7017446: (fs) Updates to file system API (3/2011)
alanb
parents: 5506
diff changeset
    28
import java.util.*;
9025
a72fc1fc4b71 7029979: (fs) Path.toRealPath(boolean) should be toRealPath(LinkOption...)
alanb
parents: 8808
diff changeset
    29
import java.nio.file.*;
21290
db5e0cad1102 7050570: (fs) FileSysteProvider fails to initializes if run with file.encoding set to Cp037
alanb
parents: 10137
diff changeset
    30
import java.nio.charset.Charset;
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 25859
diff changeset
    31
import sun.security.action.GetPropertyAction;
8808
fc799c458da8 7017446: (fs) Updates to file system API (3/2011)
alanb
parents: 5506
diff changeset
    32
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    33
/**
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    34
 * Utility methods
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    35
 */
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    36
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    37
class Util {
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    38
    private Util() { }
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    39
21290
db5e0cad1102 7050570: (fs) FileSysteProvider fails to initializes if run with file.encoding set to Cp037
alanb
parents: 10137
diff changeset
    40
    private static final Charset jnuEncoding = Charset.forName(
37781
71ed5645f17c 8155775: Re-examine naming of privileged methods to access System properties
redestad
parents: 37593
diff changeset
    41
        GetPropertyAction.privilegedGetProperty("sun.jnu.encoding"));
21290
db5e0cad1102 7050570: (fs) FileSysteProvider fails to initializes if run with file.encoding set to Cp037
alanb
parents: 10137
diff changeset
    42
db5e0cad1102 7050570: (fs) FileSysteProvider fails to initializes if run with file.encoding set to Cp037
alanb
parents: 10137
diff changeset
    43
    /**
db5e0cad1102 7050570: (fs) FileSysteProvider fails to initializes if run with file.encoding set to Cp037
alanb
parents: 10137
diff changeset
    44
     * Returns {@code Charset} corresponding to the sun.jnu.encoding property
db5e0cad1102 7050570: (fs) FileSysteProvider fails to initializes if run with file.encoding set to Cp037
alanb
parents: 10137
diff changeset
    45
     */
db5e0cad1102 7050570: (fs) FileSysteProvider fails to initializes if run with file.encoding set to Cp037
alanb
parents: 10137
diff changeset
    46
    static Charset jnuEncoding() {
db5e0cad1102 7050570: (fs) FileSysteProvider fails to initializes if run with file.encoding set to Cp037
alanb
parents: 10137
diff changeset
    47
        return jnuEncoding;
db5e0cad1102 7050570: (fs) FileSysteProvider fails to initializes if run with file.encoding set to Cp037
alanb
parents: 10137
diff changeset
    48
    }
db5e0cad1102 7050570: (fs) FileSysteProvider fails to initializes if run with file.encoding set to Cp037
alanb
parents: 10137
diff changeset
    49
db5e0cad1102 7050570: (fs) FileSysteProvider fails to initializes if run with file.encoding set to Cp037
alanb
parents: 10137
diff changeset
    50
    /**
db5e0cad1102 7050570: (fs) FileSysteProvider fails to initializes if run with file.encoding set to Cp037
alanb
parents: 10137
diff changeset
    51
     * Encodes the given String into a sequence of bytes using the {@code Charset}
db5e0cad1102 7050570: (fs) FileSysteProvider fails to initializes if run with file.encoding set to Cp037
alanb
parents: 10137
diff changeset
    52
     * specified by the sun.jnu.encoding property.
db5e0cad1102 7050570: (fs) FileSysteProvider fails to initializes if run with file.encoding set to Cp037
alanb
parents: 10137
diff changeset
    53
     */
db5e0cad1102 7050570: (fs) FileSysteProvider fails to initializes if run with file.encoding set to Cp037
alanb
parents: 10137
diff changeset
    54
    static byte[] toBytes(String s) {
db5e0cad1102 7050570: (fs) FileSysteProvider fails to initializes if run with file.encoding set to Cp037
alanb
parents: 10137
diff changeset
    55
        return s.getBytes(jnuEncoding);
db5e0cad1102 7050570: (fs) FileSysteProvider fails to initializes if run with file.encoding set to Cp037
alanb
parents: 10137
diff changeset
    56
    }
db5e0cad1102 7050570: (fs) FileSysteProvider fails to initializes if run with file.encoding set to Cp037
alanb
parents: 10137
diff changeset
    57
db5e0cad1102 7050570: (fs) FileSysteProvider fails to initializes if run with file.encoding set to Cp037
alanb
parents: 10137
diff changeset
    58
    /**
db5e0cad1102 7050570: (fs) FileSysteProvider fails to initializes if run with file.encoding set to Cp037
alanb
parents: 10137
diff changeset
    59
     * Constructs a new String by decoding the specified array of bytes using the
db5e0cad1102 7050570: (fs) FileSysteProvider fails to initializes if run with file.encoding set to Cp037
alanb
parents: 10137
diff changeset
    60
     * {@code Charset} specified by the sun.jnu.encoding property.
db5e0cad1102 7050570: (fs) FileSysteProvider fails to initializes if run with file.encoding set to Cp037
alanb
parents: 10137
diff changeset
    61
     */
db5e0cad1102 7050570: (fs) FileSysteProvider fails to initializes if run with file.encoding set to Cp037
alanb
parents: 10137
diff changeset
    62
    static String toString(byte[] bytes) {
db5e0cad1102 7050570: (fs) FileSysteProvider fails to initializes if run with file.encoding set to Cp037
alanb
parents: 10137
diff changeset
    63
        return new String(bytes, jnuEncoding);
db5e0cad1102 7050570: (fs) FileSysteProvider fails to initializes if run with file.encoding set to Cp037
alanb
parents: 10137
diff changeset
    64
    }
db5e0cad1102 7050570: (fs) FileSysteProvider fails to initializes if run with file.encoding set to Cp037
alanb
parents: 10137
diff changeset
    65
db5e0cad1102 7050570: (fs) FileSysteProvider fails to initializes if run with file.encoding set to Cp037
alanb
parents: 10137
diff changeset
    66
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    67
    /**
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    68
     * Splits a string around the given character. The array returned by this
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    69
     * method contains each substring that is terminated by the character. Use
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    70
     * for simple string spilting cases when needing to avoid loading regex.
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    71
     */
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    72
    static String[] split(String s, char c) {
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    73
        int count = 0;
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    74
        for (int i=0; i<s.length(); i++) {
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    75
            if (s.charAt(i) == c)
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    76
                count++;
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    77
        }
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    78
        String[] result = new String[count+1];
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    79
        int n = 0;
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    80
        int last = 0;
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    81
        for (int i=0; i<s.length(); i++) {
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    82
            if (s.charAt(i) == c) {
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    83
                result[n++] = s.substring(last, i);
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    84
                last = i + 1;
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    85
            }
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    86
        }
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    87
        result[n] = s.substring(last, s.length());
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    88
        return result;
8808
fc799c458da8 7017446: (fs) Updates to file system API (3/2011)
alanb
parents: 5506
diff changeset
    89
    }
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
    90
8808
fc799c458da8 7017446: (fs) Updates to file system API (3/2011)
alanb
parents: 5506
diff changeset
    91
    /**
fc799c458da8 7017446: (fs) Updates to file system API (3/2011)
alanb
parents: 5506
diff changeset
    92
     * Returns a Set containing the given elements.
fc799c458da8 7017446: (fs) Updates to file system API (3/2011)
alanb
parents: 5506
diff changeset
    93
     */
10137
d92637d3d673 7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9050
diff changeset
    94
    @SafeVarargs
8808
fc799c458da8 7017446: (fs) Updates to file system API (3/2011)
alanb
parents: 5506
diff changeset
    95
    static <E> Set<E> newSet(E... elements) {
fc799c458da8 7017446: (fs) Updates to file system API (3/2011)
alanb
parents: 5506
diff changeset
    96
        HashSet<E> set = new HashSet<>();
fc799c458da8 7017446: (fs) Updates to file system API (3/2011)
alanb
parents: 5506
diff changeset
    97
        for (E e: elements) {
fc799c458da8 7017446: (fs) Updates to file system API (3/2011)
alanb
parents: 5506
diff changeset
    98
            set.add(e);
fc799c458da8 7017446: (fs) Updates to file system API (3/2011)
alanb
parents: 5506
diff changeset
    99
        }
fc799c458da8 7017446: (fs) Updates to file system API (3/2011)
alanb
parents: 5506
diff changeset
   100
        return set;
fc799c458da8 7017446: (fs) Updates to file system API (3/2011)
alanb
parents: 5506
diff changeset
   101
    }
fc799c458da8 7017446: (fs) Updates to file system API (3/2011)
alanb
parents: 5506
diff changeset
   102
fc799c458da8 7017446: (fs) Updates to file system API (3/2011)
alanb
parents: 5506
diff changeset
   103
    /**
fc799c458da8 7017446: (fs) Updates to file system API (3/2011)
alanb
parents: 5506
diff changeset
   104
     * Returns a Set containing all the elements of the given Set plus
fc799c458da8 7017446: (fs) Updates to file system API (3/2011)
alanb
parents: 5506
diff changeset
   105
     * the given elements.
fc799c458da8 7017446: (fs) Updates to file system API (3/2011)
alanb
parents: 5506
diff changeset
   106
     */
10137
d92637d3d673 7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9050
diff changeset
   107
    @SafeVarargs
8808
fc799c458da8 7017446: (fs) Updates to file system API (3/2011)
alanb
parents: 5506
diff changeset
   108
    static <E> Set<E> newSet(Set<E> other, E... elements) {
fc799c458da8 7017446: (fs) Updates to file system API (3/2011)
alanb
parents: 5506
diff changeset
   109
        HashSet<E> set = new HashSet<>(other);
fc799c458da8 7017446: (fs) Updates to file system API (3/2011)
alanb
parents: 5506
diff changeset
   110
        for (E e: elements) {
fc799c458da8 7017446: (fs) Updates to file system API (3/2011)
alanb
parents: 5506
diff changeset
   111
            set.add(e);
fc799c458da8 7017446: (fs) Updates to file system API (3/2011)
alanb
parents: 5506
diff changeset
   112
        }
fc799c458da8 7017446: (fs) Updates to file system API (3/2011)
alanb
parents: 5506
diff changeset
   113
        return set;
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   114
    }
9025
a72fc1fc4b71 7029979: (fs) Path.toRealPath(boolean) should be toRealPath(LinkOption...)
alanb
parents: 8808
diff changeset
   115
a72fc1fc4b71 7029979: (fs) Path.toRealPath(boolean) should be toRealPath(LinkOption...)
alanb
parents: 8808
diff changeset
   116
    /**
a72fc1fc4b71 7029979: (fs) Path.toRealPath(boolean) should be toRealPath(LinkOption...)
alanb
parents: 8808
diff changeset
   117
     * Returns {@code true} if symbolic links should be followed
a72fc1fc4b71 7029979: (fs) Path.toRealPath(boolean) should be toRealPath(LinkOption...)
alanb
parents: 8808
diff changeset
   118
     */
a72fc1fc4b71 7029979: (fs) Path.toRealPath(boolean) should be toRealPath(LinkOption...)
alanb
parents: 8808
diff changeset
   119
    static boolean followLinks(LinkOption... options) {
a72fc1fc4b71 7029979: (fs) Path.toRealPath(boolean) should be toRealPath(LinkOption...)
alanb
parents: 8808
diff changeset
   120
        boolean followLinks = true;
a72fc1fc4b71 7029979: (fs) Path.toRealPath(boolean) should be toRealPath(LinkOption...)
alanb
parents: 8808
diff changeset
   121
        for (LinkOption option: options) {
a72fc1fc4b71 7029979: (fs) Path.toRealPath(boolean) should be toRealPath(LinkOption...)
alanb
parents: 8808
diff changeset
   122
            if (option == LinkOption.NOFOLLOW_LINKS) {
a72fc1fc4b71 7029979: (fs) Path.toRealPath(boolean) should be toRealPath(LinkOption...)
alanb
parents: 8808
diff changeset
   123
                followLinks = false;
a72fc1fc4b71 7029979: (fs) Path.toRealPath(boolean) should be toRealPath(LinkOption...)
alanb
parents: 8808
diff changeset
   124
            } else if (option == null) {
a72fc1fc4b71 7029979: (fs) Path.toRealPath(boolean) should be toRealPath(LinkOption...)
alanb
parents: 8808
diff changeset
   125
                throw new NullPointerException();
a72fc1fc4b71 7029979: (fs) Path.toRealPath(boolean) should be toRealPath(LinkOption...)
alanb
parents: 8808
diff changeset
   126
            } else {
a72fc1fc4b71 7029979: (fs) Path.toRealPath(boolean) should be toRealPath(LinkOption...)
alanb
parents: 8808
diff changeset
   127
                throw new AssertionError("Should not get here");
a72fc1fc4b71 7029979: (fs) Path.toRealPath(boolean) should be toRealPath(LinkOption...)
alanb
parents: 8808
diff changeset
   128
            }
a72fc1fc4b71 7029979: (fs) Path.toRealPath(boolean) should be toRealPath(LinkOption...)
alanb
parents: 8808
diff changeset
   129
        }
a72fc1fc4b71 7029979: (fs) Path.toRealPath(boolean) should be toRealPath(LinkOption...)
alanb
parents: 8808
diff changeset
   130
        return followLinks;
a72fc1fc4b71 7029979: (fs) Path.toRealPath(boolean) should be toRealPath(LinkOption...)
alanb
parents: 8808
diff changeset
   131
    }
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff changeset
   132
}