Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/nio/fs/Util.java
38918 views
/*1* Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.nio.fs;2627import java.util.*;28import java.nio.file.*;29import java.nio.charset.Charset;30import java.security.*;31import sun.security.action.*;3233/**34* Utility methods35*/3637class Util {38private Util() { }3940private static final Charset jnuEncoding = Charset.forName(41AccessController.doPrivileged(new GetPropertyAction("sun.jnu.encoding")));4243/**44* Returns {@code Charset} corresponding to the sun.jnu.encoding property45*/46static Charset jnuEncoding() {47return jnuEncoding;48}4950/**51* Encodes the given String into a sequence of bytes using the {@code Charset}52* specified by the sun.jnu.encoding property.53*/54static byte[] toBytes(String s) {55return s.getBytes(jnuEncoding);56}5758/**59* Constructs a new String by decoding the specified array of bytes using the60* {@code Charset} specified by the sun.jnu.encoding property.61*/62static String toString(byte[] bytes) {63return new String(bytes, jnuEncoding);64}656667/**68* Splits a string around the given character. The array returned by this69* method contains each substring that is terminated by the character. Use70* for simple string spilting cases when needing to avoid loading regex.71*/72static String[] split(String s, char c) {73int count = 0;74for (int i=0; i<s.length(); i++) {75if (s.charAt(i) == c)76count++;77}78String[] result = new String[count+1];79int n = 0;80int last = 0;81for (int i=0; i<s.length(); i++) {82if (s.charAt(i) == c) {83result[n++] = s.substring(last, i);84last = i + 1;85}86}87result[n] = s.substring(last, s.length());88return result;89}9091/**92* Returns a Set containing the given elements.93*/94@SafeVarargs95static <E> Set<E> newSet(E... elements) {96HashSet<E> set = new HashSet<>();97for (E e: elements) {98set.add(e);99}100return set;101}102103/**104* Returns a Set containing all the elements of the given Set plus105* the given elements.106*/107@SafeVarargs108static <E> Set<E> newSet(Set<E> other, E... elements) {109HashSet<E> set = new HashSet<>(other);110for (E e: elements) {111set.add(e);112}113return set;114}115116/**117* Returns {@code true} if symbolic links should be followed118*/119static boolean followLinks(LinkOption... options) {120boolean followLinks = true;121for (LinkOption option: options) {122if (option == LinkOption.NOFOLLOW_LINKS) {123followLinks = false;124} else if (option == null) {125throw new NullPointerException();126} else {127throw new AssertionError("Should not get here");128}129}130return followLinks;131}132}133134135