/** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.alibaba.jstorm.utils; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; import com.google.common.base.Joiner; import org.apache.commons.io.FileUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * @author yannian */ public class PathUtils { static Logger LOG = LoggerFactory.getLogger(PathUtils.class); public static final String SEPARATOR = "/"; /** * split path as list */ public static List<String> tokenize_path(String path) { String[] tokens = path.split(SEPARATOR); java.util.ArrayList<String> rtn = new ArrayList<>(); for (String str : tokens) { if (!str.isEmpty()) { rtn.add(str); } } return rtn; } public static String toks_to_path(List<String> tokens) { StringBuilder buff = new StringBuilder(); buff.append(SEPARATOR); int size = tokens.size(); for (int i = 0; i < size; i++) { buff.append(tokens.get(i)); if (i < (size - 1)) { buff.append(SEPARATOR); } } return buff.toString(); } public static String normalize_path(String path) { return toks_to_path(tokenize_path(path)); } public static String parent_path(String path) { List<String> tokens = tokenize_path(path); int size = tokens.size(); if (size > 0) { tokens.remove(size - 1); } return toks_to_path(tokens); } public static String full_path(String parent, String name) { return normalize_path(parent + SEPARATOR + name); } public static boolean exists_file(String path) { return (new File(path)).exists(); } public static void rmr(String path) throws IOException { LOG.debug("Rmr path " + path); if (exists_file(path)) { FileUtils.forceDelete(new File(path)); } } public static void local_mkdirs(String path) throws IOException { LOG.debug("Making dirs at" + path); FileUtils.forceMkdir(new File(path)); } public static void rmpath(String path) { LOG.debug("Removing path " + path); boolean succ = (new File(path)).delete(); if (!succ) { throw new RuntimeException("Failed to delete " + path); } } public static void touch(String path) throws IOException { LOG.debug("Touching file at " + path); boolean success = (new File(path)).createNewFile(); if (!success) { throw new RuntimeException("Failed to touch " + path); } } public static List<String> read_dir_contents(String dir) { ArrayList<String> rtn = new ArrayList<>(); if (exists_file(dir)) { File[] list = (new File(dir)).listFiles(); if (list == null) { return rtn; } for (File f : list) { rtn.add(f.getName()); } } return rtn; } public static String getCanonicalPath(String fileName) { String ret = null; File file = new File(fileName); if (file.exists()) { try { ret = file.getCanonicalPath(); } catch (IOException e) { LOG.error("", e); } } else { LOG.warn(fileName + " doesn't exist "); } return ret; } public static String join(String... pathList) { return Joiner.on(File.separator).join(pathList); } public static void mv(String src, String dest) { try { FileUtils.moveFile(new File(src), new File(dest)); } catch (IOException ignored) { } } }