v4.7.1u Update

This commit is contained in:
NotAlexNoyle committed 2019-05-10 03:55:16 +00:00
1 parent 5825c7353c
commit e81fcd9254
178 files changed
+1374 -13348

No files matched your search

+104 -34
View File
@@ -7,94 +7,164 @@ import java.io.IOException;
import org.jdesktop.application.Application;
/**
*
* @author Jason
* @author Jason Robitaille
* @maintainer NotAlexNoyle
*/
public class FileUtils {
public static File appDirectory() {
File app, dir;
try {
app = new File(Application.getInstance()
.getClass().getProtectionDomain().getCodeSource().getLocation().toURI());
dir = app.getParentFile();
} catch(Exception e) {
app = new File(Application.getInstance().getClass().getProtectionDomain()
.getCodeSource().getLocation()
.getPath().replaceAll("%20", " "));
app = new File(Application.getInstance().getClass().getProtectionDomain().getCodeSource().getLocation().toURI());
dir = app.getParentFile();
}
catch(Exception e) {
app = new File(Application.getInstance().getClass().getProtectionDomain().getCodeSource().getLocation().getPath().replaceAll("%20", " "));
dir = app.getParentFile();
}
return dir;
}
public static void copy(File from, File to) throws IOException {
FileInputStream fis = new FileInputStream(from);
FileOutputStream fos = new FileOutputStream(to);
try {
byte[] buf = new byte[1024];
int i = 0;
while ((i = fis.read(buf)) != -1) {
fos.write(buf, 0, i);
}
} catch (IOException e) {
throw e;
} finally {
if (fis != null)
fis.close();
if (fos != null)
fos.close();
}
catch (IOException e) {
throw e;
}
finally {
if (fis != null) {
fis.close();
}
if (fos != null) {
fos.close();
}
}
}
public static String getFilename(File file) {
return getFilename(file.getName());
}
public static String getFilename(String file) {
String name = file;
int index = name.lastIndexOf("\\");
if(index>-1) {
if(index > -1) {
name = name.substring(index+1, name.length());
} else {
index = name.lastIndexOf("/");
if(index>-1)
name = name.substring(index+1, name.length());
}
else {
index = name.lastIndexOf("/");
if(index > -1) {
name = name.substring(index+1, name.length());
}
}
return name;
}
public static String formatFilesize(File file) {
return formatFilesize(file.length());
}
public static String formatFilesize(long size) {
String result = "";
if(size<1000) { //bytes
// bytes
if(size < 1000) {
result = size + "B";
} else if((size/1024)<1000) { //kilobytes
result = roundNicely(((double)size)/1024.0) + "KB";
} else {
result = roundNicely((((double)size)/1024.0)/1024.0) + "MB";
}
//kilobytes
else if((size / 1024) < 1000) {
result = roundNicely(((double)size) / 1024.0) + "KB";
}
else {
result = roundNicely((((double)size) / 1024.0) /1024.0) + "MB";
}
return result;
}
private static String roundNicely(double dbl) {
long rounded = Math.round((dbl*100.0));
String result = String.valueOf(((double)rounded)/100.0);
if(dbl<10) {
result = String.valueOf(Math.round(dbl*100.0)/100.0);
} else if(dbl<100) {
result = String.valueOf(Math.round(dbl*10.0)/10.0);
} else {
long rounded = Math.round((dbl * 100.0));
String result = String.valueOf(((double)rounded) / 100.0);
if(dbl < 10) {
result = String.valueOf(Math.round(dbl * 100.0) / 100.0);
}
else if(dbl < 100) {
result = String.valueOf(Math.round(dbl * 10.0) / 10.0);
}
else {
result = String.valueOf(((long)dbl));
}
if(result.endsWith(".0")) {
result = result.replace(".0", "");
}
return result;
}
public static String getMD5Sum(File file) {
return MD5.getMD5Checksum(file);
}
}
}
+35 -5
View File
@@ -7,48 +7,78 @@ import java.security.MessageDigest;
/**
* @author Jason Robitaille
* @maintainer NotAlexNoyle
*/
public class MD5 {
private static final byte[] HEX_CHAR_TABLE = {
(byte)'0', (byte)'1', (byte)'2', (byte)'3',
(byte)'4', (byte)'5', (byte)'6', (byte)'7',
(byte)'8', (byte)'9', (byte)'a', (byte)'b',
(byte)'c', (byte)'d', (byte)'e', (byte)'f'
};
};
public static byte[] createChecksum(File f) throws Exception {
InputStream fis = new FileInputStream(f);
byte[] buffer = new byte[2048];
MessageDigest complete = MessageDigest.getInstance("MD5");
int numRead;
do {
numRead = fis.read(buffer);
if (numRead > 0) {
complete.update(buffer, 0, numRead);
}
} while (numRead != -1);
}
}
while (numRead != -1);
fis.close();
return complete.digest();
}
public static String getMD5Checksum(File f) {
String result = null;
try {
byte[] raw = createChecksum(f);
result = getHexString(raw);
} catch(Exception e) {}
}
catch(Exception e) {
System.out.println("ERROR: Could not get md5 checksum.");
}
return result;
}
private static String getHexString(byte[] raw) throws Exception {
byte[] hex = new byte[2 * raw.length];
int index = 0;
for (byte b : raw) {
int v = b & 0xFF;
hex[index++] = HEX_CHAR_TABLE[v >>> 4];
hex[index++] = HEX_CHAR_TABLE[v & 0xF];
}
return new String(hex, "ASCII");
}
}
}