From 4ee0a555aae49c46cd0a97c4a7b8dbcc4ef8ef55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=9D=8F=E9=BB=91?= Date: Sun, 28 Oct 2018 23:17:04 +0800 Subject: [PATCH] + exp4j --- pom.xml | 6 + .../skymc/taboolib/fileutils/FileUtils.java | 133 ++++++++---------- .../taboolib/javascript/ScriptHandler.java | 4 +- 3 files changed, 63 insertions(+), 80 deletions(-) diff --git a/pom.xml b/pom.xml index c67aa35..92ace9e 100644 --- a/pom.xml +++ b/pom.xml @@ -88,6 +88,7 @@ com.ilummc.eagletdl org.ow2.asm com.google.code.gson + net.objecthunter false @@ -154,6 +155,11 @@ scala-library 2.12.7 + + net.objecthunter + exp4j + 0.4.8 + bukkit bukkit1_12 diff --git a/src/main/java/me/skymc/taboolib/fileutils/FileUtils.java b/src/main/java/me/skymc/taboolib/fileutils/FileUtils.java index f9c4043..b35dc1f 100644 --- a/src/main/java/me/skymc/taboolib/fileutils/FileUtils.java +++ b/src/main/java/me/skymc/taboolib/fileutils/FileUtils.java @@ -302,76 +302,15 @@ public class FileUtils { * @param file2 文件2 */ public static void fileChannelCopy(File file1, File file2) { - FileInputStream fileIn = null; - FileOutputStream fileOut = null; - FileChannel channelIn = null; - FileChannel channelOut = null; - try { - fileIn = new FileInputStream(file1); - fileOut = new FileOutputStream(file2); - channelIn = fileIn.getChannel(); - channelOut = fileOut.getChannel(); + try (FileInputStream fileIn = new FileInputStream(file1); + FileOutputStream fileOut = new FileOutputStream(file2); + FileChannel channelIn = fileIn.getChannel(); + FileChannel channelOut = fileOut.getChannel()) { channelIn.transferTo(0, channelIn.size(), channelOut); } catch (IOException ignored) { - } finally { - IOUtils.closeQuietly(channelIn); - IOUtils.closeQuietly(channelOut); - IOUtils.closeQuietly(fileIn); - IOUtils.closeQuietly(fileOut); } } - /** - * 通过输入流读取文本 - * - * @param in 输入流 - * @param size 大小 - * @param encode 编码 - * @return 文本 - */ - public static String getStringFromInputStream(InputStream in, int size, String encode) { - try { - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - byte[] b = new byte[size]; - int i; - while ((i = in.read(b)) > 0) { - bos.write(b, 0, i); - } - return new String(bos.toByteArray(), encode); - } catch (IOException ignored) { - } - return null; - } - - /** - * 通过文件读取文本 - * - * @param file 文件 - * @param size 大小 - * @param encode 编码 - * @return 文本 - */ - public static String getStringFromFile(File file, int size, String encode) { - FileInputStream fin = null; - BufferedInputStream bin = null; - try { - fin = new FileInputStream(file); - bin = new BufferedInputStream(fin); - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - byte[] b = new byte[size]; - int i; - while ((i = bin.read(b)) > 0) { - bos.write(b, 0, i); - } - return new String(bos.toByteArray(), encode); - } catch (IOException ignored) { - } finally { - IOUtils.closeQuietly(bin); - IOUtils.closeQuietly(fin); - } - return null; - } - /** * 通过 URL 读取文本 * @@ -397,14 +336,9 @@ public class FileUtils { try { conn = new URL(url).openConnection(); bin = new BufferedInputStream(conn.getInputStream()); - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - byte[] b = new byte[size]; - int i; - while ((i = bin.read(b)) > 0) { - bos.write(b, 0, i); - } - return new String(bos.toByteArray(), conn.getContentEncoding() == null ? "UTF-8" : conn.getContentEncoding()); - } catch (IOException ignored) { + return getStringFromInputStream(bin, size, conn.getContentEncoding() == null ? "UTF-8" : conn.getContentEncoding()); + } catch (IOException e) { + e.printStackTrace(); } finally { IOUtils.close(conn); IOUtils.closeQuietly(bin); @@ -412,6 +346,55 @@ public class FileUtils { return null; } + /** + * 通过文件读取文本 + * + * @param file 文件 + * @param size 大小 + * @param encode 编码 + * @return 文本 + */ + public static String getStringFromFile(File file, int size, String encode) { + try (FileInputStream fin = new FileInputStream(file); BufferedInputStream bin = new BufferedInputStream(fin)) { + return getStringFromInputStream(fin, size, encode); + } catch (IOException e) { + e.printStackTrace(); + } + return null; + } + + /** + * 通过输入流读取文本 + * + * @param in 输入流 + * @param size 大小 + * @param encode 编码 + * @return 文本 + */ + public static String getStringFromInputStream(InputStream in, int size, String encode) { + try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) { + byte[] b = new byte[size]; + int i; + while ((i = in.read(b)) > 0) { + bos.write(b, 0, i); + } + return new String(bos.toByteArray(), encode); + } catch (IOException e) { + e.printStackTrace(); + } + return null; + } + + /** + * 下载文件 + * + * @param url 下载地址 + * @param file 下载位置 + */ + public static void download(String url, File file) { + download(url, file, false); + } + /** * 下载文件 * @@ -440,10 +423,6 @@ public class FileUtils { } } - public static void download(String url, File file) { - download(url, file, false); - } - @Deprecated public static void download(String url, String filename, File saveDir) { download(url, new File(saveDir, filename)); diff --git a/src/main/java/me/skymc/taboolib/javascript/ScriptHandler.java b/src/main/java/me/skymc/taboolib/javascript/ScriptHandler.java index 694bb7b..eb18af2 100644 --- a/src/main/java/me/skymc/taboolib/javascript/ScriptHandler.java +++ b/src/main/java/me/skymc/taboolib/javascript/ScriptHandler.java @@ -18,7 +18,6 @@ public class ScriptHandler { private static ScriptEngine scriptEngine; private static ScriptEngineManager scriptEngineManager = new ScriptEngineManager(); - private static FileConfiguration scriptsFile; public static void inst() { try { @@ -31,8 +30,7 @@ public class ScriptHandler { public static CompiledScript compile(String script) { try { - Compilable compilable = (Compilable) scriptEngine; - return compilable.compile(script); + return ((Compilable) scriptEngine).compile(script); } catch (Exception e) { TLogger.getGlobalLogger().info("§4JavaScript §c" + script + "§4 Compile Failed: §c" + e.toString()); return null;