Skip to content

Commit

Permalink
Use more Java 8, NIO
Browse files Browse the repository at this point in the history
  • Loading branch information
slachiewicz committed Nov 11, 2023
1 parent d4f0d2d commit 86bb500
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 48 deletions.
5 changes: 3 additions & 2 deletions src/main/java/org/codehaus/plexus/util/DirectoryScanner.java
Expand Up @@ -56,6 +56,7 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;

Expand Down Expand Up @@ -584,7 +585,7 @@ public String[] getDeselectedDirectories() {
* @since Ant 1.5
*/
public boolean isSymbolicLink(File parent, String name) throws IOException {
return NioFiles.isSymbolicLink(new File(parent, name));
return Files.isSymbolicLink(new File(parent, name).toPath());
}

/**
Expand All @@ -600,6 +601,6 @@ public boolean isSymbolicLink(File parent, String name) throws IOException {
* @since Ant 1.5
*/
public boolean isParentSymbolicLink(File parent, String name) throws IOException {
return NioFiles.isSymbolicLink(parent);
return Files.isSymbolicLink(parent.toPath());
}
}
72 changes: 33 additions & 39 deletions src/main/java/org/codehaus/plexus/util/FileUtils.java
Expand Up @@ -66,13 +66,16 @@
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption;
import java.security.SecureRandom;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;

Expand Down Expand Up @@ -165,7 +168,7 @@ public static List<String> getDefaultExcludesAsList() {
* @see StringUtils#join(Object[], String)
*/
public static String getDefaultExcludesAsString() {
return StringUtils.join(DirectoryScanner.DEFAULTEXCLUDES, ",");
return String.join(",", DirectoryScanner.DEFAULTEXCLUDES);
}

/**
Expand All @@ -178,13 +181,13 @@ public static String byteCountToDisplaySize(int size) {
String displaySize;

if (size / ONE_GB > 0) {
displaySize = String.valueOf(size / ONE_GB) + " GB";
displaySize = size / ONE_GB + " GB";
} else if (size / ONE_MB > 0) {
displaySize = String.valueOf(size / ONE_MB) + " MB";
displaySize = size / ONE_MB + " MB";
} else if (size / ONE_KB > 0) {
displaySize = String.valueOf(size / ONE_KB) + " KB";
displaySize = size / ONE_KB + " KB";
} else {
displaySize = String.valueOf(size) + " bytes";
displaySize = size + " bytes";
}

return displaySize;
Expand Down Expand Up @@ -276,8 +279,7 @@ public static String extension(String filename) {
* @return true if file exists.
*/
public static boolean fileExists(String fileName) {
File file = new File(fileName);
return file.exists();
return new File(fileName).exists();
}

/**
Expand Down Expand Up @@ -411,9 +413,8 @@ public static void fileWrite(File file, String encoding, String data) throws IOE
* @param fileName The path of the file to delete.
*/
public static void fileDelete(String fileName) {
File file = new File(fileName);
try {
NioFiles.deleteIfExists(file);
Files.deleteIfExists(new File(fileName).toPath());
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -500,12 +501,10 @@ public static String[] getFilesFromExtension(String directory, String[] extensio

// ok... transverse into this directory and get all the files... then combine
// them with the current list.

String[] fetchFiles = getFilesFromExtension(currentFileName, extensions);
files = blendFilesToVector(files, fetchFiles);
Collections.addAll(files, fetchFiles);
} else {
// ok... add the file

String add = currentFile.getAbsolutePath();
if (isValidFile(add, extensions)) {
files.add(add);
Expand Down Expand Up @@ -838,13 +837,16 @@ public static void copyFileToDirectoryIfModified(final File source, final File d
*/
public static void mkDirs(final File sourceBase, String[] dirs, final File destination) throws IOException {
for (String dir : dirs) {
File src = new File(sourceBase, dir);
File dst = new File(destination, dir);
if (NioFiles.isSymbolicLink(src)) {
File target = NioFiles.readSymbolicLink(src);
NioFiles.createSymbolicLink(dst, target);
Path src = sourceBase.toPath().resolve(dir);
Path dst = destination.toPath().resolve(dir);
if (Files.isSymbolicLink(src)) {
Path target = Files.readSymbolicLink(src);
if (Files.exists(dst, LinkOption.NOFOLLOW_LINKS)) {
Files.delete(dst);
}
Files.createSymbolicLink(dst, target);
} else {
dst.mkdirs();
Files.createDirectories(dst);
}
}
}
Expand All @@ -861,33 +863,21 @@ public static void mkDirs(final File sourceBase, String[] dirs, final File desti
* {@link #copyFileToDirectory}).
*/
public static void copyFile(final File source, final File destination) throws IOException {
Path srcPath = source.toPath();
Path dstPath = destination.toPath();
// check source exists
if (!source.exists()) {
final String message = "File " + source + " does not exist";
throw new IOException(message);
}

// check source != destination, see PLXUTILS-10
if (source.getCanonicalPath().equals(destination.getCanonicalPath())) {
if (Files.exists(dstPath) && Files.isSameFile(srcPath, dstPath)) {
// if they are equal, we can exit the method without doing any work
return;
}
mkdirsFor(destination);
Files.createDirectories(dstPath.getParent());

doCopyFile(source, destination);

if (source.length() != destination.length()) {
String message = "Failed to copy full contents from " + source + " to " + destination;
throw new IOException(message);
}
}

private static void doCopyFile(File source, File destination) throws IOException {
doCopyFileUsingNewIO(source, destination);
}

private static void doCopyFileUsingNewIO(File source, File destination) throws IOException {
NioFiles.copy(source, destination);
Files.copy(srcPath, dstPath, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES);
}

/**
Expand All @@ -902,20 +892,24 @@ private static void doCopyFileUsingNewIO(File source, File destination) throws I
* {@link #copyFileToDirectory}).
*/
public static void linkFile(final File source, final File destination) throws IOException {
Path srcPath = source.toPath();
Path dstPath = destination.toPath();
// check source exists
if (!source.exists()) {
final String message = "File " + source + " does not exist";
throw new IOException(message);
}

// check source != destination, see PLXUTILS-10
if (source.getCanonicalPath().equals(destination.getCanonicalPath())) {
if (Files.exists(dstPath) && Files.isSameFile(srcPath, dstPath)) {
// if they are equal, we can exit the method without doing any work
return;
}
mkdirsFor(destination);
Files.createDirectories(dstPath.getParent());

NioFiles.createSymbolicLink(destination, source);
if (Files.exists(dstPath, LinkOption.NOFOLLOW_LINKS)) {
Files.delete(dstPath);
}
Files.createSymbolicLink(dstPath, source.toPath());
}

/**
Expand Down
13 changes: 6 additions & 7 deletions src/main/java/org/codehaus/plexus/util/NioFiles.java
Expand Up @@ -30,7 +30,7 @@
/**
* Encapsulates use of java7 features, exposing mostly backward compatible types
*/
@SuppressWarnings("Since15")
@Deprecated
public class NioFiles {
public static boolean isSymbolicLink(File file) {
return Files.isSymbolicLink(file.toPath());
Expand Down Expand Up @@ -80,8 +80,9 @@ private static Set<PosixFilePermission> getPermissions(int mode) {
}

public static long getLastModified(File file) throws IOException {
BasicFileAttributes basicFileAttributes = Files.readAttributes(file.toPath(), BasicFileAttributes.class);
return basicFileAttributes.lastModifiedTime().toMillis();
return Files.readAttributes(file.toPath(), BasicFileAttributes.class)
.lastModifiedTime()
.toMillis();
}

/**
Expand All @@ -92,17 +93,15 @@ public static long getLastModified(File file) throws IOException {
* @throws java.io.IOException io issue
*/
public static File readSymbolicLink(File symlink) throws IOException {
Path path = Files.readSymbolicLink(symlink.toPath());
return path.toFile();
return Files.readSymbolicLink(symlink.toPath()).toFile();
}

public static File createSymbolicLink(File symlink, File target) throws IOException {
Path link = symlink.toPath();
if (Files.exists(link, LinkOption.NOFOLLOW_LINKS)) {
Files.delete(link);
}
link = Files.createSymbolicLink(link, target.toPath());
return link.toFile();
return Files.createSymbolicLink(link, target.toPath()).toFile();
}

public static boolean deleteIfExists(File file) throws IOException {
Expand Down

0 comments on commit 86bb500

Please sign in to comment.