mirror of
https://github.com/simon987/Much-Assembly-Required.git
synced 2025-04-20 11:06:46 +00:00
Fixes #68 & Reformatted code
This commit is contained in:
parent
fd73a47796
commit
fe0be03ab8
@ -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;
|
||||||
@ -11,176 +13,172 @@ import java.util.zip.ZipOutputStream;
|
|||||||
|
|
||||||
public class FileUtils {
|
public class FileUtils {
|
||||||
|
|
||||||
private static final int BUFFER_SIZE = 1024;
|
private static final int BUFFER_SIZE = 1024;
|
||||||
private static final String STR_ENCODING = "UTF-8";
|
private static final String STR_ENCODING = "UTF-8";
|
||||||
private static final String DATE_FORMAT = "yyyyMMddHHmmss";
|
private static final String DATE_FORMAT = "yyyyMMddHHmmss";
|
||||||
private static final String FILE_TYPE = ".zip";
|
private static final String FILE_TYPE = ".zip";
|
||||||
private static final Path ROOT_DIR;
|
private static final Path ROOT_DIR;
|
||||||
private static final String DIR_NAME = "history";
|
private static final String DIR_NAME = "history";
|
||||||
public static final Path DIR_PATH;
|
public static final Path DIR_PATH;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
ROOT_DIR = Paths.get(".").normalize();
|
ROOT_DIR = Paths.get(".").normalize();
|
||||||
DIR_PATH = ROOT_DIR.resolve(DIR_NAME);
|
DIR_PATH = ROOT_DIR.resolve(DIR_NAME);
|
||||||
}
|
}
|
||||||
|
|
||||||
//Private constructor
|
//Private constructor
|
||||||
private FileUtils() {
|
private FileUtils() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new stamp containing the current date and time
|
* Creates a new stamp containing the current date and time
|
||||||
*
|
*
|
||||||
* @return date and time stamp
|
* @return date and time stamp
|
||||||
*/
|
*/
|
||||||
private static String getDateTimeStamp() {
|
private static String getDateTimeStamp() {
|
||||||
Date millisToDate = new Date(System.currentTimeMillis());
|
Date millisToDate = new Date(System.currentTimeMillis());
|
||||||
SimpleDateFormat f = new SimpleDateFormat(DATE_FORMAT);
|
SimpleDateFormat f = new SimpleDateFormat(DATE_FORMAT);
|
||||||
return f.format(millisToDate);
|
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
|
* @param directory folder to create
|
||||||
* @return true is the file exists or create operation is successful
|
* @return true is the file exists or create operation is successful
|
||||||
*/
|
*/
|
||||||
public static boolean prepDirectory(Path directory) {
|
public static boolean prepDirectory(Path directory) {
|
||||||
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 {
|
||||||
System.out.println("Error creating directory: " + file.toString());
|
System.out.println("Error creating directory: " + file.toString());
|
||||||
return false;
|
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
|
* @param path the file to be converted into bytes
|
||||||
* @return the byte array of the given file
|
* @return the byte array of the given file
|
||||||
*/
|
*/
|
||||||
public static byte[] bytifyFile(Path path) {
|
public static byte[] bytifyFile(Path path) {
|
||||||
byte[] bytes = null;
|
byte[] bytes = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
bytes = Files.readAllBytes(path);
|
bytes = Files.readAllBytes(path);
|
||||||
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
System.out.println("Failed to extract bytes from: " + path);
|
System.out.println("Failed to extract bytes from: " + path);
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
return bytes;
|
return bytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Takes in a file that had been converted to a byte[] to be written to a new
|
* Takes in a file that had been converted to a byte[] to be written to a new
|
||||||
* zip file
|
* zip file
|
||||||
*
|
*
|
||||||
* @param data
|
* @param data
|
||||||
* contains data in byte array form to be written, typically a file
|
* contains data in byte array form to be written, typically a file
|
||||||
* that has been converted with bytifyFile()
|
* that has been converted with bytifyFile()
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
* if an error occurs during the write process
|
* if an error occurs during the write process
|
||||||
*/
|
*/
|
||||||
public static void writeSaveToZip(String name, byte[] data) throws IOException {
|
public static void writeSaveToZip(String name, byte[] data) throws IOException {
|
||||||
|
|
||||||
String newFile = DIR_PATH.resolve(getDateTimeStamp() + FILE_TYPE).toString();
|
String newFile = DIR_PATH.resolve(getDateTimeStamp() + FILE_TYPE).toString();
|
||||||
FileOutputStream output = new FileOutputStream(newFile);
|
FileOutputStream output = new FileOutputStream(newFile);
|
||||||
ZipOutputStream stream = new ZipOutputStream(output);
|
ZipOutputStream stream = new ZipOutputStream(output);
|
||||||
byte[] buffer = new byte[BUFFER_SIZE];
|
byte[] buffer = new byte[BUFFER_SIZE];
|
||||||
ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
|
ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
|
||||||
|
|
||||||
while ((bais.read(buffer)) > -1) {
|
while ((bais.read(buffer)) > -1) {
|
||||||
// File name
|
// File name
|
||||||
ZipEntry entry = new ZipEntry(name);
|
ZipEntry entry = new ZipEntry(name);
|
||||||
// Set to start of next entry in the stream.
|
// Set to start of next entry in the stream.
|
||||||
stream.putNextEntry(entry);
|
stream.putNextEntry(entry);
|
||||||
// Data to write.
|
// Data to write.
|
||||||
stream.write(data);
|
stream.write(data);
|
||||||
// Close the current entry.
|
// Close the current entry.
|
||||||
stream.closeEntry();
|
stream.closeEntry();
|
||||||
}
|
}
|
||||||
|
|
||||||
stream.close();
|
stream.close();
|
||||||
output.close();
|
output.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void cleanHistory(int size) {
|
public static void cleanHistory(int size) {
|
||||||
|
|
||||||
|
|
||||||
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) {
|
||||||
currentFile = files[i];
|
for (int i = 0; i < files.length / 2; i++) {
|
||||||
files[i] = files[files.length - i - 1];
|
currentFile = files[i];
|
||||||
files[files.length - i - 1] = currentFile;
|
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++) {
|
try {
|
||||||
changed = false;
|
long dirFile = Long.parseLong(files[f].getName().substring(0, (files[f].getName().length() - 4)));
|
||||||
long dirFile = Long.parseLong(files[f].getName().substring(0, (files[f].getName().length() -4)));
|
|
||||||
|
|
||||||
if(f < size && sorted[f] == null) {
|
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];
|
sorted[s] = files[f];
|
||||||
|
|
||||||
} else if(nextSortedFile == null) {
|
} else {
|
||||||
nextSortedFile = sorted[s];
|
sorted[s] = files[f];
|
||||||
sorted[s] = files[f];
|
}
|
||||||
|
|
||||||
} else {
|
changed = true;
|
||||||
currentFile = sorted[s];
|
}
|
||||||
sorted[s] = nextSortedFile;
|
}
|
||||||
nextSortedFile = currentFile;
|
|
||||||
}
|
|
||||||
|
|
||||||
nextSortedFile = null;
|
if (!changed) {
|
||||||
currentFile = null;
|
files[f].delete();
|
||||||
changed = true;
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
* 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 {
|
return new String(bytes, STR_ENCODING);
|
||||||
return new String(bytes, STR_ENCODING);
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user