|
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 { |
|
39 private Util() { } |
|
40 |
|
41 private static final Charset jnuEncoding = Charset.forName( |
|
42 AccessController.doPrivileged(new GetPropertyAction("sun.jnu.encoding"))); |
|
43 |
|
44 /** |
|
45 * Returns {@code Charset} corresponding to the sun.jnu.encoding property |
|
46 */ |
|
47 static Charset jnuEncoding() { |
|
48 return jnuEncoding; |
|
49 } |
|
50 |
|
51 /** |
|
52 * Encodes the given String into a sequence of bytes using the {@code Charset} |
|
53 * specified by the sun.jnu.encoding property. |
|
54 */ |
|
55 static byte[] toBytes(String s) { |
|
56 return s.getBytes(jnuEncoding); |
|
57 } |
|
58 |
|
59 /** |
|
60 * Constructs a new String by decoding the specified array of bytes using the |
|
61 * {@code Charset} specified by the sun.jnu.encoding property. |
|
62 */ |
|
63 static String toString(byte[] bytes) { |
|
64 return new String(bytes, jnuEncoding); |
|
65 } |
|
66 |
|
67 |
|
68 /** |
|
69 * Splits a string around the given character. The array returned by this |
|
70 * method contains each substring that is terminated by the character. Use |
|
71 * for simple string spilting cases when needing to avoid loading regex. |
|
72 */ |
|
73 static String[] split(String s, char c) { |
|
74 int count = 0; |
|
75 for (int i=0; i<s.length(); i++) { |
|
76 if (s.charAt(i) == c) |
|
77 count++; |
|
78 } |
|
79 String[] result = new String[count+1]; |
|
80 int n = 0; |
|
81 int last = 0; |
|
82 for (int i=0; i<s.length(); i++) { |
|
83 if (s.charAt(i) == c) { |
|
84 result[n++] = s.substring(last, i); |
|
85 last = i + 1; |
|
86 } |
|
87 } |
|
88 result[n] = s.substring(last, s.length()); |
|
89 return result; |
|
90 } |
|
91 |
|
92 /** |
|
93 * Returns a Set containing the given elements. |
|
94 */ |
|
95 @SafeVarargs |
|
96 static <E> Set<E> newSet(E... elements) { |
|
97 HashSet<E> set = new HashSet<>(); |
|
98 for (E e: elements) { |
|
99 set.add(e); |
|
100 } |
|
101 return set; |
|
102 } |
|
103 |
|
104 /** |
|
105 * Returns a Set containing all the elements of the given Set plus |
|
106 * the given elements. |
|
107 */ |
|
108 @SafeVarargs |
|
109 static <E> Set<E> newSet(Set<E> other, E... elements) { |
|
110 HashSet<E> set = new HashSet<>(other); |
|
111 for (E e: elements) { |
|
112 set.add(e); |
|
113 } |
|
114 return set; |
|
115 } |
|
116 |
|
117 /** |
|
118 * Returns {@code true} if symbolic links should be followed |
|
119 */ |
|
120 static boolean followLinks(LinkOption... options) { |
|
121 boolean followLinks = true; |
|
122 for (LinkOption option: options) { |
|
123 if (option == LinkOption.NOFOLLOW_LINKS) { |
|
124 followLinks = false; |
|
125 } else if (option == null) { |
|
126 throw new NullPointerException(); |
|
127 } else { |
|
128 throw new AssertionError("Should not get here"); |
|
129 } |
|
130 } |
|
131 return followLinks; |
|
132 } |
|
133 } |