java.nio.file.Path#normalize ( )源码实例Demo

下面列出了java.nio.file.Path#normalize ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: meghanada-server   文件: ClassPathUtils.java
public static void addToolsJar() throws Exception {
  String home = System.getProperty("java.home");
  String parent = new File(home).getParent();
  Path path = Paths.get(parent, "lib", "tools.jar");
  path = path.normalize();
  File file = path.toFile();
  if (file.exists()) {
    Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
    method.setAccessible(true);
    method.invoke(ClassLoader.getSystemClassLoader(), file.toURI().toURL());
    log.info("add tools.jar path:{}", path.toFile().getCanonicalPath());
  } else {
    if (isJava8()) {
      log.error("missing tools.jar path:{}", path.toFile().getCanonicalPath());
    }
  }
}
 
源代码2 项目: nexus-public   文件: DirectoryHelper.java
/**
 * Return {@code true} if paths {@code from} and {@code to} are located on same FileStore (volume or
 * partition). The {@code from} must exists, while {@code to} does not have to.
 */
private static boolean areOnSameFileStore(final Path from, final Path to) {
  try {
    final FileStore fromStore = Files.getFileStore(from); // from must exist
    Path toExistingParent = to.normalize(); // to usually does not exists, is about to be created as part of move
    while (toExistingParent != null && !Files.exists(toExistingParent)) {
      toExistingParent = toExistingParent.getParent();
    }
    if (toExistingParent != null) {
      final FileStore toStore = Files.getFileStore(toExistingParent);
      return fromStore.equals(toStore);
    }
    else {
      log.warn("No ultimate parent path found for '{}'", to, new RuntimeException("marker")); // record the stack trace?
      return false; // no ultimate parent? be on safe side
    }
  }
  catch (IOException e) {
    return false; // be on safe side
  }
}
 
源代码3 项目: Strata   文件: ZipUtils.java
private static Path validateZipPathName(Path rootPath, ZipEntry entry) throws ZipException {
  Path normalizedRootPath = rootPath.normalize();
  Path resolved = normalizedRootPath.resolve(entry.getName()).normalize();
  if (!resolved.startsWith(normalizedRootPath)) {
    throw new ZipException("ZIP file contains illegal file name: " + entry.getName());
  }
  return resolved;
}
 
源代码4 项目: jasperreports   文件: FileRepositoryService.java
protected Path rootNormalizedPath()
{
	Path rootPath = rootNormalizedPath;
	if (rootPath == null)
	{
		Path path = Paths.get(root);
		rootPath = rootNormalizedPath = path.normalize();
	}
	return rootPath;
}
 
源代码5 项目: buck   文件: MorePaths.java
/**
 * Get a path without unnecessary path parts.
 *
 * <p>This method is a workaround for JDK-8037945 (Paths.get("").normalize() throws
 * ArrayIndexOutOfBoundsException).
 */
public static Path normalize(Path path) {
  if (!isEmpty(path)) {
    path = path.normalize();
  }
  return path;
}
 
源代码6 项目: saros   文件: IntellijPath.java
private IntellijPath(Path delegate) {
  /*
   * OpenJDK 7 on Linux has a bug which causes normalize() to throw an
   * ArrayIndexOutOfBoundsException if called on the empty path.
   */
  this.delegate = delegate.equals(Paths.get("")) ? delegate : delegate.normalize();
}
 
源代码7 项目: buck   文件: LimitedFileHashCacheEngine.java
@Override
public HashCode getForArchiveMember(Path archiveRelativePath, Path memberPath)
    throws IOException {
  Path relativeFilePath = archiveRelativePath.normalize();
  Preconditions.checkState(
      isArchive(relativeFilePath), "%s is not an archive.", relativeFilePath);
  Data data = fileSystemMap.get(relativeFilePath);
  HashCode hashCode = data.getJarContentsHashes().get(memberPath);
  if (hashCode == null) {
    throw new NoSuchFileException(archiveRelativePath.toString());
  }
  return hashCode;
}
 
源代码8 项目: openjdk-jdk9   文件: JarUtils.java
/**
 * Map a file path to the equivalent name in a JAR file
 */
private static String toJarEntryName(Path file) {
    Path normalized = file.normalize();
    return normalized.subpath(0, normalized.getNameCount())  // drop root
            .toString()
            .replace(File.separatorChar, '/');
}
 
源代码9 项目: ongdb-lab-apoc   文件: PathUtils.java
/**
 * Tries to resolve the given path against the list of available roots.
 * <p>
 * If path starts with one of the listed roots, it returned back by this method, otherwise null is returned.
 */
public static Path get(Path[] roots, String path) {
    for (Path root : roots) {
        Path normalizedRoot = root.normalize();
        Path normalizedPath = normalizedRoot.resolve(path).normalize();
        if (normalizedPath.startsWith(normalizedRoot)) {
            return normalizedPath;
        }
    }
    return null;
}
 
源代码10 项目: buck   文件: WorkspaceGenerator.java
/**
 * Adds a reference to a project file to the generated workspace.
 *
 * @param path Path to the referenced project file in the repository.
 */
public void addFilePath(Path path) {
  path = path.normalize();

  Optional<Path> groupPath = Optional.empty();
  // We skip the last name before the file name as it's usually the same as the project name, and
  // adding a group would add an unnecessary level of nesting. We don't check whether it's the
  // same or not to avoid inconsistent behaviour: this will result in all projects to show up in a
  // group with the path of their grandparent directory in all cases.
  if (path.getNameCount() > 2) {
    groupPath = Optional.of(path.subpath(0, path.getNameCount() - 2));
  }
  addFilePath(path, groupPath);
}
 
源代码11 项目: encfs4j   文件: CipherFileChannel.java
public CipherFileChannel(Path path, String cipherTransformation,
		SecretKeySpec secretKeySpec, Path fileSystemRoot, boolean isReverse)
		throws IOException {

	try {
		this.path = path.normalize();

		// this.encrypt =
		// "encfs".equals(path.getFileSystem().provider().getScheme());

		this.persistentFile = new RandomAccessFile(path.toFile(), "rw");

		this.persistentChannel = this.persistentFile.getChannel();
		this.isOpen = true;

		this.cipherTransformation = cipherTransformation;
		this.secretKeySpec = secretKeySpec;

		this.relativeFilename = fileSystemRoot.relativize(this.path)
				.toString();

		this.isReverse = isReverse;

	} catch (FileNotFoundException e) {
		throw new IOException(e.getMessage(), e);
	}
}
 
源代码12 项目: lucene-solr   文件: SolrResourceLoader.java
private Path checkPathIsSafe(Path pathToCheck) throws IOException {
  if (Boolean.getBoolean("solr.allow.unsafe.resourceloading"))
    return pathToCheck;
  pathToCheck = pathToCheck.normalize();
  if (pathToCheck.startsWith(instanceDir))
    return pathToCheck;
  throw new IOException("File " + pathToCheck + " is outside resource loader dir " + instanceDir +
      "; set -Dsolr.allow.unsafe.resourceloading=true to allow unsafe loading");
}
 
源代码13 项目: Elasticsearch   文件: PathUtils.java
/**
 * Tries to resolve the given path against the list of available roots.
 *
 * If path starts with one of the listed roots, it returned back by this method, otherwise null is returned.
 */
public static Path get(Path[] roots, String path) {
    for (Path root : roots) {
        Path normalizedRoot = root.normalize();
        Path normalizedPath = normalizedRoot.resolve(path).normalize();
        if(normalizedPath.startsWith(normalizedRoot)) {
            return normalizedPath;
        }
    }
    return null;
}
 
源代码14 项目: jeka   文件: SourceParser.java
private static JkDependencySet dependenciesFromImports(Path baseDir, List<String> deps) {
    JkDependencySet result = JkDependencySet.of();
    for (final String dependency : deps) {
        if (isModuleDependencyDescription(dependency)) {
            result = result.and(JkModuleDependency.of(dependency));
        } else  if (dependency.contains("*")) {
            if (dependency.contains("*")) {
                for (Path path : JkPathTree.of(baseDir).andMatching(true, dependency).getFiles()) {
                    result = result.andFile(path);
                }
            }
        } else {
            Path depFile = Paths.get(dependency);
            if (!Files.exists(depFile)) {
                final Path relativeFile = baseDir.resolve(dependency);
                if (Files.exists(relativeFile)) {
                    depFile = relativeFile.normalize();
                } else {
                    JkLog.warn("File '" + dependency
                            + "' mentionned in @JkDefClasspath does not exist.");
                }
            }
            result = result.andFile(depFile);
        }
    }
    return result;
}
 
源代码15 项目: buck   文件: FileSystemMapFileHashCache.java
@Override
public HashCode getForArchiveMember(Path archiveRelativePath, Path memberPath)
    throws IOException {
  Path relativeFilePath = archiveRelativePath.normalize();
  JarHashCodeAndFileType fileHashCodeAndFileType =
      (JarHashCodeAndFileType) loadingCache.get(relativeFilePath);
  HashCodeAndFileType memberHashCodeAndFileType =
      fileHashCodeAndFileType.getContents().get(memberPath);
  if (memberHashCodeAndFileType == null) {
    throw new NoSuchFileException(archiveRelativePath.toString());
  }
  return memberHashCodeAndFileType.getHashCode();
}
 
源代码16 项目: galleon   文件: MavenConfig.java
public void setLocalRepository(Path path) throws XMLStreamException, IOException {
    localRepository = path.normalize();
    advertise();
}
 
源代码17 项目: tutorials   文件: PathManualTest.java
@Test
public void givenPath_whenRemovesRedundancies_thenCorrect1() {
    Path p = Paths.get("/home/./baeldung/articles");
    p = p.normalize();
    assertEquals("\\home\\baeldung\\articles", p.toString());
}
 
源代码18 项目: copybara   文件: CheckoutPath.java
static CheckoutPath createWithCheckoutDir(Path relative, Path checkoutDir) throws EvalException {
  if (relative.isAbsolute()) {
    throw Starlark.errorf("Absolute paths are not allowed: %s", relative);
  }
  return new CheckoutPath(relative.normalize(), checkoutDir);
}
 
源代码19 项目: spring-analysis-note   文件: PathResource.java
/**
 * Create a new PathResource from a Path handle.
 * <p>Note: Unlike {@link FileSystemResource}, when building relative resources
 * via {@link #createRelative}, the relative path will be built <i>underneath</i>
 * the given root: e.g. Paths.get("C:/dir1/"), relative path "dir2" -> "C:/dir1/dir2"!
 * @param path a Path handle
 */
public PathResource(Path path) {
	Assert.notNull(path, "Path must not be null");
	this.path = path.normalize();
}
 
源代码20 项目: lams   文件: PathResource.java
/**
 * Create a new PathResource from a Path handle.
 * <p>Note: Unlike {@link FileSystemResource}, when building relative resources
 * via {@link #createRelative}, the relative path will be built <i>underneath</i>
 * the given root: e.g. Paths.get("C:/dir1/"), relative path "dir2" -> "C:/dir1/dir2"!
 * @param path a Path handle
 */
public PathResource(Path path) {
	Assert.notNull(path, "Path must not be null");
	this.path = path.normalize();
}