com.feilong.lib.io.FilenameUtils Maven / Gradle / Ivy
Show all versions of feilong Show documentation
/*
* 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.feilong.lib.io;
/**
* General filename and filepath manipulation utilities.
*
* When dealing with filenames you can hit problems when moving from a Windows
* based development machine to a Unix based production machine.
* This class aims to help avoid those problems.
*
* NOTE: You may be able to avoid using this class entirely simply by
* using JDK {@link java.io.File File} objects and the two argument constructor
* {@link java.io.File#File(java.io.File, java.lang.String) File(File,String)}.
*
* Most methods on this class are designed to work the same on both Unix and Windows.
* Those that don't include 'System', 'Unix' or 'Windows' in their name.
*
* Most methods recognise both separators (forward and back), and both
* sets of prefixes. See the javadoc of each method for details.
*
* This class defines six components within a filename
* (example C:\dev\project\file.txt):
*
* - the prefix - C:\
* - the path - dev\project\
* - the full path - C:\dev\project\
* - the name - file.txt
* - the base name - file
* - the extension - txt
*
* Note that this class works best if directory filenames end with a separator.
* If you omit the last separator, it is impossible to determine if the filename
* corresponds to a file or a directory. As a result, we have chosen to say
* it corresponds to a file.
*
* This class only supports Unix and Windows style names.
* Prefixes are matched as follows:
*
*
* Windows:
* a\b\c.txt --> "" --> relative
* \a\b\c.txt --> "\" --> current drive absolute
* C:a\b\c.txt --> "C:" --> drive relative
* C:\a\b\c.txt --> "C:\" --> absolute
* \\server\a\b\c.txt --> "\\server\" --> UNC
*
* Unix:
* a/b/c.txt --> "" --> relative
* /a/b/c.txt --> "/" --> absolute
* ~/a/b/c.txt --> "~/" --> current user
* ~ --> "~/" --> current user (slash added)
* ~user/a/b/c.txt --> "~user/" --> named user
* ~user --> "~user/" --> named user (slash added)
*
*
* Both prefix styles are matched always, irrespective of the machine that you are
* currently running on.
*
* Origin of code: Excalibur, Alexandria, Tomcat, Commons-Utils.
*
* @since 1.1
*/
public class FilenameUtils{
private static final int NOT_FOUND = -1;
/**
* The extension separator character.
*
* @since 1.4
*/
public static final char EXTENSION_SEPARATOR = '.';
/**
* The Unix separator character.
*/
private static final char UNIX_SEPARATOR = '/';
/**
* The Windows separator character.
*/
private static final char WINDOWS_SEPARATOR = '\\';
//---------------------------------------------------------------
/**
* Returns the index of the last directory separator character.
*
* This method will handle a file in either Unix or Windows format.
* The position of the last forward or backslash is returned.
*
* The output will be the same irrespective of the machine that the code is running on.
*
* @param filename
* the filename to find the last path separator in, null returns -1
* @return the index of the last separator character, or -1 if there
* is no such character
*/
public static int indexOfLastSeparator(final String filename){
if (filename == null){
return NOT_FOUND;
}
final int lastUnixPos = filename.lastIndexOf(UNIX_SEPARATOR);
final int lastWindowsPos = filename.lastIndexOf(WINDOWS_SEPARATOR);
return Math.max(lastUnixPos, lastWindowsPos);
}
/**
* Returns the index of the last extension separator character, which is a dot.
*
* This method also checks that there is no directory separator after the last dot. To do this it uses
* {@link #indexOfLastSeparator(String)} which will handle a file in either Unix or Windows format.
*
*
* The output will be the same irrespective of the machine that the code is running on.
*
*
* @param filename
* the filename to find the last extension separator in, null returns -1
* @return the index of the last extension separator character, or -1 if there is no such character
*/
public static int indexOfExtension(final String filename){
if (filename == null){
return NOT_FOUND;
}
final int extensionPos = filename.lastIndexOf(EXTENSION_SEPARATOR);
final int lastSeparator = indexOfLastSeparator(filename);
return lastSeparator > extensionPos ? NOT_FOUND : extensionPos;
}
/**
* Gets the name minus the path from a full filename.
*
* This method will handle a file in either Unix or Windows format.
* The text after the last forward or backslash is returned.
*
*
* a/b/c.txt --> c.txt
* a.txt --> a.txt
* a/b/c --> c
* a/b/c/ --> ""
*
*
* The output will be the same irrespective of the machine that the code is running on.
*
* @param filename
* the filename to query, null returns null
* @return the name of the file without the path, or an empty string if none exists.
* Null bytes inside string will be removed
*/
public static String getName(final String filename){
if (filename == null){
return null;
}
failIfNullBytePresent(filename);
final int index = indexOfLastSeparator(filename);
return filename.substring(index + 1);
}
/**
* Check the input for null bytes, a sign of unsanitized data being passed to to file level functions.
*
* This may be used for poison byte attacks.
*
* @param path
* the path to check
*/
private static void failIfNullBytePresent(final String path){
final int len = path.length();
for (int i = 0; i < len; i++){
if (path.charAt(i) == 0){
throw new IllegalArgumentException(
"Null byte present in file/path name. There are no "
+ "known legitimate use cases for such data, but several injection attacks may use it");
}
}
}
/**
* Gets the extension of a filename.
*
* This method returns the textual part of the filename after the last dot.
* There must be no directory separator after the dot.
*
*
* foo.txt --> "txt"
* a/b/c.jpg --> "jpg"
* a/b.txt/c --> ""
* a/b/c --> ""
*
*
* The output will be the same irrespective of the machine that the code is running on.
*
* @param filename
* the filename to retrieve the extension of.
* @return the extension of the file or an empty string if none exists or {@code null}
* if the filename is {@code null}.
*/
public static String getExtension(final String filename){
if (filename == null){
return null;
}
final int index = indexOfExtension(filename);
if (index == NOT_FOUND){
return "";
}
return filename.substring(index + 1);
}
/**
* Checks whether the extension of the filename is one of those specified.
*
* This method obtains the extension as the textual part of the filename
* after the last dot. There must be no directory separator after the dot.
* The extension check is case-sensitive on all platforms.
*
* @param filename
* the filename to query, null returns false
* @param extensions
* the extensions to check for, null checks for no extension
* @return true if the filename is one of the extensions
* @throws java.lang.IllegalArgumentException
* if the supplied filename contains null bytes
*/
public static boolean isExtension(final String filename,final String...extensions){
if (filename == null){
return false;
}
failIfNullBytePresent(filename);
if (extensions == null || extensions.length == 0){
return indexOfExtension(filename) == NOT_FOUND;
}
final String fileExt = getExtension(filename);
for (final String extension : extensions){
if (fileExt.equals(extension)){
return true;
}
}
return false;
}
}