better Utils#cleanFileName

This commit is contained in:
yugecin 2017-05-26 17:57:02 +02:00
parent c8df3d5b57
commit b8dd507dc5
2 changed files with 32 additions and 51 deletions

View File

@ -27,7 +27,6 @@ import java.net.URL;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate; import java.security.cert.X509Certificate;
import java.util.Arrays;
import java.util.Scanner; import java.util.Scanner;
import java.util.jar.JarFile; import java.util.jar.JarFile;
@ -45,7 +44,6 @@ import org.newdawn.slick.Color;
import org.newdawn.slick.util.Log; import org.newdawn.slick.util.Log;
import com.sun.jna.platform.FileUtils; import com.sun.jna.platform.FileUtils;
import yugecin.opsudance.core.DisplayContainer;
import yugecin.opsudance.core.NotNull; import yugecin.opsudance.core.NotNull;
import yugecin.opsudance.core.Nullable; import yugecin.opsudance.core.Nullable;
import yugecin.opsudance.core.errorhandling.ErrorHandler; import yugecin.opsudance.core.errorhandling.ErrorHandler;
@ -56,30 +54,6 @@ import static yugecin.opsudance.core.InstanceContainer.*;
* Contains miscellaneous utilities. * Contains miscellaneous utilities.
*/ */
public class Utils { public class Utils {
/**
* List of illegal filename characters.
* @see #cleanFileName(String, char)
*/
private final static int[] illegalChars = {
34, 60, 62, 124, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
24, 25, 26, 27, 28, 29, 30, 31, 58, 42, 63, 92, 47
};
static {
Arrays.sort(illegalChars);
}
// This class should not be instantiated.
private Utils() {}
/**
* Initializes game settings and class data.
*/
public static void init(DisplayContainer displayContainer) {
// TODO clean this up
// game settings
}
/** /**
* Draws an animation based on its center. * Draws an animation based on its center.
@ -205,26 +179,31 @@ public class Utils {
} }
/** /**
* Cleans a file name. * Changes bad characters to the replacement char given.
* @param badFileName the original name string * Bad characters:
* @param replace the character to replace illegal characters with (or 0 if none) * non-printable (0-31)
* @return the cleaned file name * " (34) * (42) / (47) : (58)
* @author Sarel Botha (http://stackoverflow.com/a/5626340) * < (60) > (62) ? (63) \ (92)
* DEL (124)
*/ */
public static String cleanFileName(String badFileName, char replace) { public static String cleanFileName(@NotNull String badFileName, char replacement) {
if (badFileName == null) char[] chars = badFileName.toCharArray();
return null; long additionalBadChars =
1L << (34 - 32)|
boolean doReplace = (replace > 0 && Arrays.binarySearch(illegalChars, replace) < 0); 1L << (42 - 32)|
StringBuilder cleanName = new StringBuilder(); 1L << (47 - 32)|
for (int i = 0, n = badFileName.length(); i < n; i++) { 1L << (58 - 32)|
int c = badFileName.charAt(i); 1L << (60 - 32)|
if (Arrays.binarySearch(illegalChars, c) < 0) 1L << (62 - 32)|
cleanName.append((char) c); 1L << (63 - 32)|
else if (doReplace) 1L << (92 - 32);
cleanName.append(replace); for (int i = 0; i < chars.length; i++) {
char c = chars[i];
if (c < 32 || c == 124 || (c < 93 && 0 != (additionalBadChars & (1L << (c - 32))))) {
chars[i] = replacement;
}
} }
return cleanName.toString(); return new String(chars);
} }
/** /**

View File

@ -92,7 +92,7 @@ public class Download {
private String localPath; private String localPath;
/** The local path to rename the file to when finished. */ /** The local path to rename the file to when finished. */
private String rename; private String renamedFileName;
/** The download URL. */ /** The download URL. */
private URL url; private URL url;
@ -137,9 +137,9 @@ public class Download {
* Constructor. * Constructor.
* @param remoteURL the download URL * @param remoteURL the download URL
* @param localPath the path to save the download * @param localPath the path to save the download
* @param rename the file name to rename the download to when complete * @param renamedFileName the file name to rename the download to when complete
*/ */
public Download(String remoteURL, String localPath, String rename) { public Download(String remoteURL, String localPath, String renamedFileName) {
try { try {
this.url = new URL(remoteURL); this.url = new URL(remoteURL);
} catch (MalformedURLException e) { } catch (MalformedURLException e) {
@ -148,7 +148,9 @@ public class Download {
return; return;
} }
this.localPath = localPath; this.localPath = localPath;
this.rename = Utils.cleanFileName(rename, '-'); if (renamedFileName != null) {
this.renamedFileName = Utils.cleanFileName(renamedFileName, '-');
}
} }
/** /**
@ -159,7 +161,7 @@ public class Download {
/** /**
* Returns the local path to save the download (after renamed). * Returns the local path to save the download (after renamed).
*/ */
public String getLocalPath() { return (rename != null) ? rename : localPath; } public String getLocalPath() { return (renamedFileName != null) ? renamedFileName : localPath; }
/** /**
* Sets the download listener. * Sets the download listener.
@ -263,9 +265,9 @@ public class Download {
status = Status.COMPLETE; status = Status.COMPLETE;
rbc.close(); rbc.close();
fos.close(); fos.close();
if (rename != null) { if (renamedFileName != null) {
Path source = new File(localPath).toPath(); Path source = new File(localPath).toPath();
Files.move(source, source.resolveSibling(rename), StandardCopyOption.REPLACE_EXISTING); Files.move(source, source.resolveSibling(renamedFileName), StandardCopyOption.REPLACE_EXISTING);
} }
if (listener != null) if (listener != null)
listener.completed(); listener.completed();