org.apache.commons.io.FilenameUtils.getPrefixLength()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(9.4k)|赞(0)|评价(0)|浏览(179)

本文整理了Java中org.apache.commons.io.FilenameUtils.getPrefixLength()方法的一些代码示例,展示了FilenameUtils.getPrefixLength()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。FilenameUtils.getPrefixLength()方法的具体详情如下:
包路径:org.apache.commons.io.FilenameUtils
类名称:FilenameUtils
方法名:getPrefixLength

FilenameUtils.getPrefixLength介绍

[英]Returns the length of the filename prefix, such as C:/ or ~/.

This method will handle a file in either Unix or Windows format.

The prefix length includes the first slash in the full filename if applicable. Thus, it is possible that the length returned is greater than the length of the input string.

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)

The output will be the same irrespective of the machine that the code is running on. ie. both Unix and Windows prefixes are matched regardless.
[中]返回文件名前缀的长度,例如C:/~/
此方法将处理Unix或Windows格式的文件。
前缀长度包括完整文件名中的第一个斜杠(如果适用)。因此,返回的长度可能大于输入字符串的长度。

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)

无论代码在哪台机器上运行,输出都是相同的。Unix和Windows前缀都是匹配的。

代码示例

代码示例来源:origin: commons-io/commons-io

/**
 * Does the work of getting the path.
 *
 * @param filename  the filename
 * @param separatorAdd  0 to omit the end separator, 1 to return it
 * @return the path. Null bytes inside string will be removed
 */
private static String doGetPath(final String filename, final int separatorAdd) {
  if (filename == null) {
    return null;
  }
  final int prefix = getPrefixLength(filename);
  if (prefix < 0) {
    return null;
  }
  final int index = indexOfLastSeparator(filename);
  final int endIndex = index+separatorAdd;
  if (prefix >= filename.length() || index < 0 || prefix >= endIndex) {
    return "";
  }
  final String path = filename.substring(prefix, endIndex);
  failIfNullBytePresent(path);
  return path;
}

代码示例来源:origin: commons-io/commons-io

return null;
final int prefix = getPrefixLength(filename);
if (prefix < 0) {
  return null;

代码示例来源:origin: org.apache.commons/commons-io

return null;
int len = getPrefixLength(filename);
if (len < 0) {
  return null;

代码示例来源:origin: commons-io/commons-io

return filename;
final int prefix = getPrefixLength(filename);
if (prefix < 0) {
  return null;

代码示例来源:origin: commons-io/commons-io

return null;
final int len = getPrefixLength(filename);
if (len < 0) {
  return null;

代码示例来源:origin: org.apache.commons/commons-io

/**
 * Does the work of getting the path.
 * 
 * @param filename  the filename
 * @param separatorAdd  0 to omit the end separator, 1 to return it
 * @return the path
 */
private static String doGetPath(String filename, int separatorAdd) {
  if (filename == null) {
    return null;
  }
  int prefix = getPrefixLength(filename);
  if (prefix < 0) {
    return null;
  }
  int index = indexOfLastSeparator(filename);
  if (prefix >= filename.length() || index < 0) {
    return "";
  }
  return filename.substring(prefix, index + separatorAdd);
}

代码示例来源:origin: org.apache.commons/commons-io

/**
 * Does the work of getting the path.
 * 
 * @param filename  the filename
 * @param includeSeparator  true to include the end separator
 * @return the path
 */
private static String doGetFullPath(String filename, boolean includeSeparator) {
  if (filename == null) {
    return null;
  }
  int prefix = getPrefixLength(filename);
  if (prefix < 0) {
    return null;
  }
  if (prefix >= filename.length()) {
    if (includeSeparator) {
      return getPrefix(filename);  // add end slash if necessary
    } else {
      return filename;
    }
  }
  int index = indexOfLastSeparator(filename);
  if (index < 0) {
    return filename.substring(0, prefix);
  }
  int end = index + (includeSeparator ?  1 : 0);
  return filename.substring(0, end);
}

代码示例来源:origin: org.apache.commons/commons-io

return filename;
int prefix = getPrefixLength(filename);
if (prefix < 0) {
  return null;

代码示例来源:origin: primefaces/primefaces

public static String getValidFilename(String filename) {
  if (LangUtils.isValueBlank(filename)) {
    return null;
  }
  if (isSystemWindows()) {
    if (!filename.contains("\\\\")) {
      String[] parts = filename.substring(FilenameUtils.getPrefixLength(filename)).split(Pattern.quote(File.separator));
      for (String part : parts) {
        if (INVALID_FILENAME_PATTERN.matcher(part).find()) {
          throw new FacesException("Invalid filename: " + filename);
        }
      }
    }
    else {
      throw new FacesException("Invalid filename: " + filename);
    }
  }
  String name = FilenameUtils.getName(filename);
  String extension = FilenameUtils.EXTENSION_SEPARATOR_STR + FilenameUtils.getExtension(filename);
  if (extension.equals(FilenameUtils.EXTENSION_SEPARATOR_STR)) {
    throw new FacesException("File must have an extension");
  }
  else if (name.isEmpty() || extension.equals(name)) {
    throw new FacesException("Filename can not be the empty string");
  }
  return name;
}

代码示例来源:origin: commons-io/commons-io

@Test
public void testGetPrefixLength() {
  assertEquals(-1, FilenameUtils.getPrefixLength(null));
  assertEquals(-1, FilenameUtils.getPrefixLength(":"));
  assertEquals(-1, FilenameUtils.getPrefixLength("1:\\a\\b\\c.txt"));
  assertEquals(-1, FilenameUtils.getPrefixLength("1:"));
  assertEquals(-1, FilenameUtils.getPrefixLength("1:a"));
  assertEquals(-1, FilenameUtils.getPrefixLength("\\\\\\a\\b\\c.txt"));
  assertEquals(-1, FilenameUtils.getPrefixLength("\\\\a"));
  assertEquals(0, FilenameUtils.getPrefixLength(""));
  assertEquals(1, FilenameUtils.getPrefixLength("\\"));
  assertEquals(2, FilenameUtils.getPrefixLength("C:"));
  assertEquals(3, FilenameUtils.getPrefixLength("C:\\"));
  assertEquals(9, FilenameUtils.getPrefixLength("//server/"));
  assertEquals(2, FilenameUtils.getPrefixLength("~"));
  assertEquals(2, FilenameUtils.getPrefixLength("~/"));
  assertEquals(6, FilenameUtils.getPrefixLength("~user"));
  assertEquals(6, FilenameUtils.getPrefixLength("~user/"));
  assertEquals(0, FilenameUtils.getPrefixLength("a\\b\\c.txt"));
  assertEquals(1, FilenameUtils.getPrefixLength("\\a\\b\\c.txt"));
  assertEquals(2, FilenameUtils.getPrefixLength("C:a\\b\\c.txt"));
  assertEquals(3, FilenameUtils.getPrefixLength("C:\\a\\b\\c.txt"));
  assertEquals(9, FilenameUtils.getPrefixLength("\\\\server\\a\\b\\c.txt"));
  assertEquals(0, FilenameUtils.getPrefixLength("a/b/c.txt"));
  assertEquals(1, FilenameUtils.getPrefixLength("/a/b/c.txt"));
  assertEquals(3, FilenameUtils.getPrefixLength("C:/a/b/c.txt"));
  assertEquals(9, FilenameUtils.getPrefixLength("//server/a/b/c.txt"));

代码示例来源:origin: commons-io/commons-io

final int prefix = getPrefixLength(fullFilenameToAdd);
if (prefix < 0) {
  return null;

代码示例来源:origin: org.apache.commons/commons-io

int prefix = getPrefixLength(fullFilenameToAdd);
if (prefix < 0) {
  return null;

代码示例来源:origin: apache/tika

private String getFinalName(String name, Map<String, byte[]> zout) {
  name = name.replaceAll("\u0000", " ");
  String normalizedName = FilenameUtils.normalize(name);
  if (normalizedName == null) {
    normalizedName = FilenameUtils.getName(name);
  }
  if (normalizedName == null) {
    normalizedName = count.toString();
  }
  //strip off initial C:/ or ~/ or /
  int prefixLength = FilenameUtils.getPrefixLength(normalizedName);
  if (prefixLength > -1) {
    normalizedName = normalizedName.substring(prefixLength);
  }
  if (zout.containsKey(normalizedName)) {
    return UUID.randomUUID().toString()+"-"+normalizedName;
  }
  return normalizedName;
}

代码示例来源:origin: apache/tika

int prefixLength = FilenameUtils.getPrefixLength(normalizedName);
if (prefixLength > -1) {
  normalizedName = normalizedName.substring(prefixLength);

代码示例来源:origin: apache/tika

int prefix = FilenameUtils.getPrefixLength(name);
if (prefix > -1) {
  name = name.substring(prefix);

代码示例来源:origin: com.googlecode.cedar-common/cedar-common-util

/**
 * Check whether a path is absolute.
 * @param  path   Path to operate on
 * @return True if the path is absolute, false otherwise.
 * @see <a href="http://stackoverflow.com/questions/1025285">StackOverflow</a>
 */
public static boolean isAbsolutePath(String path) {
  return FilenameUtils.getPrefixLength(path) != 0;
}

代码示例来源:origin: iterate-ch/cyberduck

private boolean isAbsolute(final String path) {
  return FilenameUtils.getPrefixLength(path) != 0;
}

代码示例来源:origin: cloudfoundry-incubator/multiapps-controller

private static boolean isAbsolute(String path) {
  return FilenameUtils.getPrefixLength(path) != 0;
}

代码示例来源:origin: com.opentext.ia/infoarchive-yaml

private String toUnix(String path) {
 String result = FilenameUtils.separatorsToUnix(path);
 int prefixLength = FilenameUtils.getPrefixLength(result);
 return prefixLength <= 1 ? result : result.substring(prefixLength - 1);
}

代码示例来源:origin: vmi/selenese-runner-java

/**
 * Add Selenese file. (test-suite or test-case)
 *
 * @param filename Selenese file name.
 * @param commandFactory command factory.
 */
public void addSeleneseFile(String filename, ICommandFactory commandFactory) {
  if (FilenameUtils.getPrefixLength(filename) == 0 && parentDir != null)
    filename = PathUtils.concat(parentDir, filename);
  else
    filename = PathUtils.normalize(filename);
  addSelenese(Parser.parse(filename, commandFactory));
}

相关文章