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;
import net.simon987.server.logging.LogManager;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
@ -11,176 +13,172 @@ import java.util.zip.ZipOutputStream;
public class FileUtils {
private static final int BUFFER_SIZE = 1024;
private static final String STR_ENCODING = "UTF-8";
private static final String DATE_FORMAT = "yyyyMMddHHmmss";
private static final String FILE_TYPE = ".zip";
private static final Path ROOT_DIR;
private static final String DIR_NAME = "history";
public static final Path DIR_PATH;
private static final int BUFFER_SIZE = 1024;
private static final String STR_ENCODING = "UTF-8";
private static final String DATE_FORMAT = "yyyyMMddHHmmss";
private static final String FILE_TYPE = ".zip";
private static final Path ROOT_DIR;
private static final String DIR_NAME = "history";
public static final Path DIR_PATH;
static {
ROOT_DIR = Paths.get(".").normalize();
DIR_PATH = ROOT_DIR.resolve(DIR_NAME);
}
static {
ROOT_DIR = Paths.get(".").normalize();
DIR_PATH = ROOT_DIR.resolve(DIR_NAME);
}
//Private constructor
private FileUtils() {
//Private constructor
private FileUtils() {
}
}
/**
* Creates a new stamp containing the current date and time
*
* @return date and time stamp
*/
private static String getDateTimeStamp() {
Date millisToDate = new Date(System.currentTimeMillis());
SimpleDateFormat f = new SimpleDateFormat(DATE_FORMAT);
return f.format(millisToDate);
}
/**
* Creates a new stamp containing the current date and time
*
* @return date and time stamp
*/
private static String getDateTimeStamp() {
Date millisToDate = new Date(System.currentTimeMillis());
SimpleDateFormat f = new SimpleDateFormat(DATE_FORMAT);
return f.format(millisToDate);
}
/**
* Created a directory if none exists with the specified name
*
/**
* Created a directory if none exists with the specified name
*
* @param directory folder to create
* @return true is the file exists or create operation is successful
*/
public static boolean prepDirectory(Path directory) {
File file = directory.toFile();
*/
public static boolean prepDirectory(Path directory) {
File file = directory.toFile();
//If the directory exists or the directory created successfully return true
if(file.exists() || file.mkdir()) {
return true;
//If the directory exists or the directory created successfully return true
if (file.exists() || file.mkdir()) {
return true;
} else {
System.out.println("Error creating directory: " + file.toString());
return false;
}
}
} else {
System.out.println("Error creating directory: " + file.toString());
return false;
}
}
/**
* Converts a file into an array of bytes
*
/**
* Converts a file into an array of bytes
*
* @param path the file to be converted into bytes
* @return the byte array of the given file
*/
public static byte[] bytifyFile(Path path) {
byte[] bytes = null;
*/
public static byte[] bytifyFile(Path path) {
byte[] bytes = null;
try {
bytes = Files.readAllBytes(path);
try {
bytes = Files.readAllBytes(path);
} catch (IOException e) {
System.out.println("Failed to extract bytes from: " + path);
e.printStackTrace();
}
} catch (IOException e) {
System.out.println("Failed to extract bytes from: " + path);
e.printStackTrace();
}
return bytes;
}
return bytes;
}
/**
* Takes in a file that had been converted to a byte[] to be written to a new
* zip file
*
/**
* Takes in a file that had been converted to a byte[] to be written to a new
* zip file
*
* @param data
* contains data in byte array form to be written, typically a file
* that has been converted with bytifyFile()
* @throws IOException
* if an error occurs during the write process
*/
public static void writeSaveToZip(String name, byte[] data) throws IOException {
* that has been converted with bytifyFile()
* @throws IOException
* if an error occurs during the write process
*/
public static void writeSaveToZip(String name, byte[] data) throws IOException {
String newFile = DIR_PATH.resolve(getDateTimeStamp() + FILE_TYPE).toString();
FileOutputStream output = new FileOutputStream(newFile);
ZipOutputStream stream = new ZipOutputStream(output);
byte[] buffer = new byte[BUFFER_SIZE];
ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
String newFile = DIR_PATH.resolve(getDateTimeStamp() + FILE_TYPE).toString();
FileOutputStream output = new FileOutputStream(newFile);
ZipOutputStream stream = new ZipOutputStream(output);
byte[] buffer = new byte[BUFFER_SIZE];
ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
while ((bais.read(buffer)) > -1) {
// File name
ZipEntry entry = new ZipEntry(name);
// Set to start of next entry in the stream.
stream.putNextEntry(entry);
// Data to write.
stream.write(data);
// Close the current entry.
stream.closeEntry();
}
while ((bais.read(buffer)) > -1) {
// File name
ZipEntry entry = new ZipEntry(name);
// Set to start of next entry in the stream.
stream.putNextEntry(entry);
// Data to write.
stream.write(data);
// Close the current entry.
stream.closeEntry();
}
stream.close();
output.close();
}
stream.close();
output.close();
}
public static void cleanHistory(int size) {
public static void cleanHistory(int size) {
File[] files = new File(DIR_PATH.toString()).listFiles();
File[] sorted = new File[size];
File[] files = new File(DIR_PATH.toString()).listFiles();
File[] sorted = new File[size];
File nextSortedFile = null;
File currentFile = null;
boolean changed = false;
File currentFile;
boolean changed;
for(int i = 0; i < files.length / 2; i++) {
currentFile = files[i];
files[i] = files[files.length - i - 1];
files[files.length - i - 1] = currentFile;
}
if (files != null) {
for (int i = 0; i < files.length / 2; i++) {
currentFile = files[i];
files[i] = files[files.length - i - 1];
files[files.length - i - 1] = currentFile;
}
currentFile = null;
for (int f = 0; f < files.length; f++) {
changed = false;
for(int f = 0; f < files.length; f++) {
changed = false;
long dirFile = Long.parseLong(files[f].getName().substring(0, (files[f].getName().length() -4)));
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];
if (f < size && sorted[f] == null) {
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) {
sorted[s] = files[f];
if (s == sorted.length - 1) {
sorted[s] = files[f];
} else if(nextSortedFile == null) {
nextSortedFile = sorted[s];
sorted[s] = files[f];
} else {
sorted[s] = files[f];
}
} else {
currentFile = sorted[s];
sorted[s] = nextSortedFile;
nextSortedFile = currentFile;
}
changed = true;
}
}
nextSortedFile = null;
currentFile = null;
changed = true;
}
}
if (!changed) {
files[f].delete();
}
if(changed == false) {
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
*
* @param bytes
* data to be encoded to String
* @return a String containing the encoded bytes
*/
public static String byteArrAsString(byte[] bytes) throws UnsupportedEncodingException {
return new String(bytes, STR_ENCODING);
}
/**
* Converts a byte array into human readable format using the provided encoding
*
* @param bytes data to be encoded to String
* @return a String containing the encoded bytes
*/
public static String byteArrAsString(byte[] bytes) throws UnsupportedEncodingException {
return new String(bytes, STR_ENCODING);
}
}