Skip to content

Replace StringUtils + Partially cleanup FileUtils + Fix unit test #8688

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Mar 26, 2019
123 changes: 10 additions & 113 deletions arduino-core/src/processing/app/helpers/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@

import org.apache.commons.compress.utils.IOUtils;

import java.io.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.regex.Pattern;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

public class FileUtils {

private static final List<String> SOURCE_CONTROL_FOLDERS = Arrays.asList("CVS", "RCS", ".git", ".svn", ".hg", ".bzr");
private static final Pattern BACKSLASH = Pattern.compile("\\\\");

/**
* Checks, whether the child directory is a subdirectory of the base directory.
Expand Down Expand Up @@ -109,75 +115,6 @@ public static File createTempFolder(File parent, String prefix, String suffix) t
return Files.createDirectories(Paths.get(parent.getAbsolutePath(), prefix + suffix)).toFile();
}

//
// Compute relative path to "target" from a directory "origin".
//
// If "origin" is not absolute, it is relative from the current directory.
// If "target" is not absolute, it is relative from "origin".
//
// by Shigeru KANEMOTO at SWITCHSCIENCE.
//
public static String relativePath(String origin, String target) {
try {
origin = (new File(origin)).getCanonicalPath();
File targetFile = new File(target);
if (targetFile.isAbsolute())
target = targetFile.getCanonicalPath();
else
target = (new File(origin, target)).getCanonicalPath();
} catch (IOException e) {
return null;
}

if (origin.equals(target)) {
// origin and target is identical.
return ".";
}

if (origin.equals(File.separator)) {
// origin is root.
return "." + target;
}

String prefix = "";
String root = File.separator;

if (System.getProperty("os.name").indexOf("Windows") != -1) {
if (origin.startsWith("\\\\") || target.startsWith("\\\\")) {
// Windows UNC path not supported.
return null;
}

char originLetter = origin.charAt(0);
char targetLetter = target.charAt(0);
if (Character.isLetter(originLetter) && Character.isLetter(targetLetter)) {
// Windows only
if (originLetter != targetLetter) {
// Drive letters differ
return null;
}
}

prefix = "" + originLetter + ':';
root = prefix + File.separator;
}

String relative = "";
while (!target.startsWith(origin + File.separator)) {
origin = (new File(origin)).getParent();
if (origin.equals(root))
origin = prefix;
relative += "..";
relative += File.separator;
}

return relative + target.substring(origin.length() + 1);
}

public static String getLinuxPathFrom(File file) {
return BACKSLASH.matcher(file.getAbsolutePath()).replaceAll("/");
}

public static boolean isSCCSOrHiddenFile(File file) {
return isSCCSFolder(file) || isHiddenFile(file);
}
Expand Down Expand Up @@ -209,25 +146,6 @@ public static String readFileToString(File file, String encoding) throws IOExcep
}
}

public static List<String> readFileToListOfStrings(File file) throws IOException {
List<String> strings = new LinkedList<>();
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String line;
while ((line = reader.readLine()) != null) {
line = line.replaceAll("\r", "").replaceAll("\n", "").replaceAll(" ", "");
strings.add(line);
}
return strings;
} finally {
if (reader != null) {
reader.close();
}
}
}


/**
* Returns true if the given file has any of the given extensions.
*
Expand All @@ -236,10 +154,6 @@ public static List<String> readFileToListOfStrings(File file) throws IOException
* dot). Should all be lowercase, case insensitive matching
* is used.
*/
public static boolean hasExtension(File file, String... extensions) {
return hasExtension(file, Arrays.asList(extensions));
}

public static boolean hasExtension(File file, List<String> extensions) {
String extension = splitFilename(file).extension;
return extensions.contains(extension.toLowerCase());
Expand Down Expand Up @@ -364,21 +278,4 @@ public static List<File> listFiles(File folder, boolean recursive,
return result;
}

public static File newFile(File parent, String... parts) {
File result = parent;
for (String part : parts) {
result = new File(result, part);
}

return result;
}

public static boolean deleteIfExists(File file) {
if (file == null) {
return true;
}

return file.delete();
}

}