From f99e1b0e8923bcc8797927b7299a115173f8a99e Mon Sep 17 00:00:00 2001 From: Brettflan Date: Sun, 26 Aug 2012 04:44:55 -0500 Subject: [PATCH] Partial backport of Olof's file handling changes made in the main 1.7.x branch. Reworked a bit to use older file handling methods which don't require Java 1.7. Main change is that files as a whole are read into (and written from) an array of bytes, which is converted to/from UTF-8 encoded text. Previously files were read line-by-line as text from a BufferedReader. Surprisingly, this seems to vastly speed up startup time. I tested on a ~2 MB data set which is now loading in just over 1 second where it was previously loading in ~95 seconds. --- .../factions/zcore/util/DiscUtil.java | 110 +++++++++++++++--- 1 file changed, 95 insertions(+), 15 deletions(-) diff --git a/src/com/massivecraft/factions/zcore/util/DiscUtil.java b/src/com/massivecraft/factions/zcore/util/DiscUtil.java index 43e4c244..344be452 100644 --- a/src/com/massivecraft/factions/zcore/util/DiscUtil.java +++ b/src/com/massivecraft/factions/zcore/util/DiscUtil.java @@ -7,28 +7,55 @@ import java.nio.channels.ReadableByteChannel; public class DiscUtil { + // -------------------------------------------- // + // CONSTANTS + // -------------------------------------------- // + + private final static String UTF8 = "UTF-8"; + + // -------------------------------------------- // + // BYTE + // -------------------------------------------- // + + public static byte[] readBytes(File file) throws IOException + { + int length = (int) file.length(); + byte[] output = new byte[length]; + InputStream in = new FileInputStream(file); + int offset = 0; + while (offset < length) + { + offset += in.read(output, offset, (length - offset)); + } + in.close(); + return output; + } + + public static void writeBytes(File file, byte[] bytes) throws IOException + { + FileOutputStream out = new FileOutputStream(file); + out.write(bytes); + out.close(); + } + + // -------------------------------------------- // + // STRING + // -------------------------------------------- // + public static void write(File file, String content) throws IOException { - BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, false), "UTF8")); - out.write(content); - out.close(); + writeBytes(file, utf8(content)); } public static String read(File file) throws IOException { - BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8")); - String ret = new String(new byte[0], "UTF-8"); - - String line; - while ((line = in.readLine()) != null) - { - ret += line; - } - - in.close(); - return ret; + return utf8(readBytes(file)); } + // -------------------------------------------- // + // CATCH + // -------------------------------------------- // + public static boolean writeCatch(File file, String content) { try @@ -54,6 +81,10 @@ public class DiscUtil } } + // -------------------------------------------- // + // DOWNLOAD + // -------------------------------------------- // + public static boolean downloadUrl(String urlstring, File file) { try @@ -75,4 +106,53 @@ public class DiscUtil { return downloadUrl(urlstring, new File(filename)); } -} + + // -------------------------------------------- // + // FILE DELETION + // -------------------------------------------- // + + public static boolean deleteRecursive(File path) throws FileNotFoundException + { + if ( ! path.exists()) throw new FileNotFoundException(path.getAbsolutePath()); + boolean ret = true; + if (path.isDirectory()) + { + for (File f : path.listFiles()) + { + ret = ret && deleteRecursive(f); + } + } + return ret && path.delete(); + } + + // -------------------------------------------- // + // UTF8 ENCODE AND DECODE + // -------------------------------------------- // + + public static byte[] utf8(String string) + { + try + { + return string.getBytes(UTF8); + } + catch (UnsupportedEncodingException e) + { + e.printStackTrace(); + return null; + } + } + + public static String utf8(byte[] bytes) + { + try + { + return new String(bytes, UTF8); + } + catch (UnsupportedEncodingException e) + { + e.printStackTrace(); + return null; + } + } + +} \ No newline at end of file