Fixes #68 & Reformatted code

This commit is contained in:
simon 2017-12-30 10:35:56 -05:00
parent fd73a47796
commit fe0be03ab8

View File

@ -1,5 +1,7 @@
package net.simon987.server.io; package net.simon987.server.io;
import net.simon987.server.logging.LogManager;
import java.io.*; import java.io.*;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
@ -50,7 +52,7 @@ public class FileUtils {
File file = directory.toFile(); File file = directory.toFile();
//If the directory exists or the directory created successfully return true //If the directory exists or the directory created successfully return true
if(file.exists() || file.mkdir()) { if (file.exists() || file.mkdir()) {
return true; return true;
} else { } else {
@ -118,66 +120,62 @@ public class FileUtils {
File[] files = new File(DIR_PATH.toString()).listFiles(); File[] files = new File(DIR_PATH.toString()).listFiles();
File[] sorted = new File[size]; File[] sorted = new File[size];
File nextSortedFile = null; File currentFile;
File currentFile = null; boolean changed;
boolean changed = false;
for(int i = 0; i < files.length / 2; i++) { if (files != null) {
for (int i = 0; i < files.length / 2; i++) {
currentFile = files[i]; currentFile = files[i];
files[i] = files[files.length - i - 1]; files[i] = files[files.length - i - 1];
files[files.length - i - 1] = currentFile; files[files.length - i - 1] = currentFile;
} }
currentFile = null; for (int f = 0; f < files.length; f++) {
for(int f = 0; f < files.length; f++) {
changed = false; changed = false;
long dirFile = Long.parseLong(files[f].getName().substring(0, (files[f].getName().length() -4)));
if(f < size && sorted[f] == null) { try {
long dirFile = Long.parseLong(files[f].getName().substring(0, (files[f].getName().length() - 4)));
if (f < size && sorted[f] == null) {
sorted[f] = files[f]; sorted[f] = files[f];
} else { } else {
for(int s = 0; s < sorted.length; s++) { for (int s = 0; s < sorted.length; s++) {
long sortedFile = Long.parseLong(sorted[s].getName().substring(0, (sorted[s].getName().length() -4))); long sortedFile = Long.parseLong(sorted[s].getName().substring(0, (sorted[s].getName().length() - 4)));
if(dirFile > sortedFile) { if (dirFile > sortedFile) {
if(s == sorted.length - 1) { if (s == sorted.length - 1) {
sorted[s] = files[f];
} else if(nextSortedFile == null) {
nextSortedFile = sorted[s];
sorted[s] = files[f]; sorted[s] = files[f];
} else { } else {
currentFile = sorted[s]; sorted[s] = files[f];
sorted[s] = nextSortedFile;
nextSortedFile = currentFile;
} }
nextSortedFile = null;
currentFile = null;
changed = true; changed = true;
} }
} }
if(changed == false) { if (!changed) {
files[f].delete(); files[f].delete();
} }
} }
} catch (NumberFormatException e) {
LogManager.LOGGER.info("Non-save file in history directory: " + files[f].getName());
} }
}
}
} }
/** /**
* Converts a byte array into human readable format using the provided encoding * Converts a byte array into human readable format using the provided encoding
* *
* @param bytes * @param bytes data to be encoded to String
* data to be encoded to String
* @return a String containing the encoded bytes * @return a String containing the encoded bytes
*/ */
public static String byteArrAsString(byte[] bytes) throws UnsupportedEncodingException { public static String byteArrAsString(byte[] bytes) throws UnsupportedEncodingException {