1 /*
2  * Copyright (c) 2009, 2013, Oracle and/or its affiliates. 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.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package sun.nio.fs;
27 
28 import java.util.*;
29 import java.nio.file.*;
30 import java.nio.charset.Charset;
31 import java.security.*;
32 import sun.security.action.*;
33 
34 /**
35  * Utility methods
36  */
37 
38 class Util {
Util()39     private Util() { }
40 
41     // private static final Charset jnuEncoding = Charset.forName(
42     //    AccessController.doPrivileged(new GetPropertyAction("sun.jnu.encoding")));
43     private static final Charset jnuEncoding = Charset.forName("UTF-8");
44 
45     /**
46      * Returns {@code Charset} corresponding to the sun.jnu.encoding property
47      */
jnuEncoding()48     static Charset jnuEncoding() {
49         return jnuEncoding;
50     }
51 
52     /**
53      * Encodes the given String into a sequence of bytes using the {@code Charset}
54      * specified by the sun.jnu.encoding property.
55      */
toBytes(String s)56     static byte[] toBytes(String s) {
57         return s.getBytes(jnuEncoding);
58     }
59 
60     /**
61      * Constructs a new String by decoding the specified array of bytes using the
62      * {@code Charset} specified by the sun.jnu.encoding property.
63      */
toString(byte[] bytes)64     static String toString(byte[] bytes) {
65         return new String(bytes, jnuEncoding);
66     }
67 
68 
69     /**
70      * Splits a string around the given character. The array returned by this
71      * method contains each substring that is terminated by the character. Use
72      * for simple string spilting cases when needing to avoid loading regex.
73      */
split(String s, char c)74     static String[] split(String s, char c) {
75         int count = 0;
76         for (int i=0; i<s.length(); i++) {
77             if (s.charAt(i) == c)
78                 count++;
79         }
80         String[] result = new String[count+1];
81         int n = 0;
82         int last = 0;
83         for (int i=0; i<s.length(); i++) {
84             if (s.charAt(i) == c) {
85                 result[n++] = s.substring(last, i);
86                 last = i + 1;
87             }
88         }
89         result[n] = s.substring(last, s.length());
90         return result;
91     }
92 
93     /**
94      * Returns a Set containing the given elements.
95      */
96     @SafeVarargs
newSet(E... elements)97     static <E> Set<E> newSet(E... elements) {
98         HashSet<E> set = new HashSet<>();
99         for (E e: elements) {
100             set.add(e);
101         }
102         return set;
103     }
104 
105     /**
106      * Returns a Set containing all the elements of the given Set plus
107      * the given elements.
108      */
109     @SafeVarargs
newSet(Set<E> other, E... elements)110     static <E> Set<E> newSet(Set<E> other, E... elements) {
111         HashSet<E> set = new HashSet<>(other);
112         for (E e: elements) {
113             set.add(e);
114         }
115         return set;
116     }
117 
118     /**
119      * Returns {@code true} if symbolic links should be followed
120      */
followLinks(LinkOption... options)121     static boolean followLinks(LinkOption... options) {
122         boolean followLinks = true;
123         for (LinkOption option: options) {
124             if (option == LinkOption.NOFOLLOW_LINKS) {
125                 followLinks = false;
126             } else if (option == null) {
127                 throw new NullPointerException();
128             } else {
129                 throw new AssertionError("Should not get here");
130             }
131         }
132         return followLinks;
133     }
134 }
135