Path: blob/master/src/java.base/share/classes/sun/nio/fs/Util.java
41159 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 sun.security.action.GetPropertyAction;3132/**33* Utility methods34*/3536class Util {37private Util() { }3839private static final Charset jnuEncoding = Charset.forName(40GetPropertyAction.privilegedGetProperty("sun.jnu.encoding"));4142/**43* Returns {@code Charset} corresponding to the sun.jnu.encoding property44*/45static Charset jnuEncoding() {46return jnuEncoding;47}4849/**50* Encodes the given String into a sequence of bytes using the {@code Charset}51* specified by the sun.jnu.encoding property.52*/53static byte[] toBytes(String s) {54return s.getBytes(jnuEncoding);55}5657/**58* Constructs a new String by decoding the specified array of bytes using the59* {@code Charset} specified by the sun.jnu.encoding property.60*/61static String toString(byte[] bytes) {62return new String(bytes, jnuEncoding);63}646566/**67* Splits a string around the given character. The array returned by this68* method contains each substring that is terminated by the character. Use69* for simple string spilting cases when needing to avoid loading regex.70*/71static String[] split(String s, char c) {72int count = 0;73for (int i=0; i<s.length(); i++) {74if (s.charAt(i) == c)75count++;76}77String[] result = new String[count+1];78int n = 0;79int last = 0;80for (int i=0; i<s.length(); i++) {81if (s.charAt(i) == c) {82result[n++] = s.substring(last, i);83last = i + 1;84}85}86result[n] = s.substring(last, s.length());87return result;88}8990/**91* Returns a Set containing the given elements.92*/93@SafeVarargs94static <E> Set<E> newSet(E... elements) {95HashSet<E> set = new HashSet<>();96for (E e: elements) {97set.add(e);98}99return set;100}101102/**103* Returns a Set containing all the elements of the given Set plus104* the given elements.105*/106@SafeVarargs107static <E> Set<E> newSet(Set<E> other, E... elements) {108HashSet<E> set = new HashSet<>(other);109for (E e: elements) {110set.add(e);111}112return set;113}114115/**116* Returns {@code true} if symbolic links should be followed117*/118static boolean followLinks(LinkOption... options) {119boolean followLinks = true;120for (LinkOption option: options) {121if (option == LinkOption.NOFOLLOW_LINKS) {122followLinks = false;123} else if (option == null) {124throw new NullPointerException();125} else {126throw new AssertionError("Should not get here");127}128}129return followLinks;130}131}132133134