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