From 3b61a2be9baf96ceed098d7605f937bfdb70f0e7 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Wed, 10 Apr 2013 10:34:12 +0200 Subject: [PATCH] Using MCore DiscUtil is easy enough since it was backported. --- src/com/massivecraft/factions/Board.java | 2 +- .../zcore/persist/EntityCollection.java | 2 +- .../factions/zcore/util/DiscUtil.java | 160 ------------------ .../factions/zcore/util/LibLoader.java | 1 + .../factions/zcore/util/Persist.java | 1 + 5 files changed, 4 insertions(+), 162 deletions(-) delete mode 100644 src/com/massivecraft/factions/zcore/util/DiscUtil.java diff --git a/src/com/massivecraft/factions/Board.java b/src/com/massivecraft/factions/Board.java index 5a13d6e7..f2676e73 100644 --- a/src/com/massivecraft/factions/Board.java +++ b/src/com/massivecraft/factions/Board.java @@ -13,12 +13,12 @@ import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.block.Block; +import com.massivecraft.mcore.util.DiscUtil; import com.massivecraft.mcore.util.Txt; import com.massivecraft.mcore.xlib.gson.reflect.TypeToken; import com.massivecraft.factions.integration.LWCFeatures; import com.massivecraft.factions.iface.RelationParticipator; import com.massivecraft.factions.util.AsciiCompass; -import com.massivecraft.factions.zcore.util.DiscUtil; public class Board diff --git a/src/com/massivecraft/factions/zcore/persist/EntityCollection.java b/src/com/massivecraft/factions/zcore/persist/EntityCollection.java index dcab2904..9c85534c 100644 --- a/src/com/massivecraft/factions/zcore/persist/EntityCollection.java +++ b/src/com/massivecraft/factions/zcore/persist/EntityCollection.java @@ -7,8 +7,8 @@ import java.util.logging.Level; import java.util.Map.Entry; import org.bukkit.Bukkit; -import com.massivecraft.factions.zcore.util.DiscUtil; import com.massivecraft.factions.zcore.util.TextUtil; +import com.massivecraft.mcore.util.DiscUtil; import com.massivecraft.mcore.xlib.gson.Gson; import com.massivecraft.mcore.xlib.gson.JsonSyntaxException; diff --git a/src/com/massivecraft/factions/zcore/util/DiscUtil.java b/src/com/massivecraft/factions/zcore/util/DiscUtil.java deleted file mode 100644 index 09ed9bc0..00000000 --- a/src/com/massivecraft/factions/zcore/util/DiscUtil.java +++ /dev/null @@ -1,160 +0,0 @@ -package com.massivecraft.factions.zcore.util; - -import java.io.*; -import java.net.URL; -import java.nio.channels.Channels; -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; - // normally it should be able to read the entire file with just a single iteration below, but it depends on the whims of the FileInputStream - 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 - { - writeBytes(file, utf8(content)); - } - - public static String read(File file) throws IOException - { - return utf8(readBytes(file)); - } - - // -------------------------------------------- // - // CATCH - // -------------------------------------------- // - - public static boolean writeCatch(File file, String content) - { - try - { - write(file, content); - return true; - } - catch (Exception e) - { - return false; - } - } - - public static String readCatch(File file) - { - try - { - return read(file); - } - catch (IOException e) - { - return null; - } - } - - // -------------------------------------------- // - // DOWNLOAD - // -------------------------------------------- // - - public static boolean downloadUrl(String urlstring, File file) - { - try - { - URL url = new URL(urlstring); - ReadableByteChannel rbc = Channels.newChannel(url.openStream()); - FileOutputStream fos = new FileOutputStream(file); - fos.getChannel().transferFrom(rbc, 0, 1 << 24); - fos.close(); - return true; - } - catch (Exception e) - { - e.printStackTrace(); - return false; - } - } - - public static boolean downloadUrl(String urlstring, String filename) - { - 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 diff --git a/src/com/massivecraft/factions/zcore/util/LibLoader.java b/src/com/massivecraft/factions/zcore/util/LibLoader.java index 63bafc77..ee7dba4f 100644 --- a/src/com/massivecraft/factions/zcore/util/LibLoader.java +++ b/src/com/massivecraft/factions/zcore/util/LibLoader.java @@ -3,6 +3,7 @@ package com.massivecraft.factions.zcore.util; import java.io.File; import com.massivecraft.factions.zcore.MPlugin; +import com.massivecraft.mcore.util.DiscUtil; public class LibLoader { diff --git a/src/com/massivecraft/factions/zcore/util/Persist.java b/src/com/massivecraft/factions/zcore/util/Persist.java index 3d7fd8d7..115eaa28 100644 --- a/src/com/massivecraft/factions/zcore/util/Persist.java +++ b/src/com/massivecraft/factions/zcore/util/Persist.java @@ -5,6 +5,7 @@ import java.lang.reflect.Type; import java.util.logging.Level; import com.massivecraft.factions.zcore.MPlugin; +import com.massivecraft.mcore.util.DiscUtil; // TODO: Give better name and place to differenciate from the entity-orm-ish system in "com.massivecraft.core.persist".