史诗级大饼,请叫我黑饼王。
This commit is contained in:
@@ -1,97 +0,0 @@
|
||||
package me.skymc.taboolib.fileutils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.FileChannel;
|
||||
|
||||
@Deprecated
|
||||
public class CopyUtils {
|
||||
|
||||
public static long Copy(File file1, File file2) throws IOException {
|
||||
// CHECK THE FILE
|
||||
if (!file1.exists()) {
|
||||
file1.createNewFile();
|
||||
}
|
||||
if (!file2.exists()) {
|
||||
file2.createNewFile();
|
||||
}
|
||||
|
||||
// RESET TIME
|
||||
long time = System.currentTimeMillis();
|
||||
|
||||
// I/O SETTING
|
||||
FileInputStream in = new FileInputStream(file1);
|
||||
FileOutputStream out = new FileOutputStream(file2);
|
||||
FileChannel inC = in.getChannel();
|
||||
FileChannel outC = out.getChannel();
|
||||
ByteBuffer b = null;
|
||||
|
||||
// CAPACITY [2GB]
|
||||
Integer length = 2097152;
|
||||
|
||||
// WORKSPACE
|
||||
while (true) {
|
||||
if (inC.position() == inC.size()) {
|
||||
inC.close();
|
||||
outC.close();
|
||||
return System.currentTimeMillis() - time;
|
||||
}
|
||||
if ((inC.size() - inC.position()) < length) {
|
||||
length = (int) (inC.size()-inC.position());
|
||||
}
|
||||
else {
|
||||
length = 2097152;
|
||||
}
|
||||
|
||||
b = ByteBuffer.allocateDirect(length);
|
||||
inC.read(b);
|
||||
b.flip();
|
||||
outC.write(b);
|
||||
outC.force(false);
|
||||
}
|
||||
}
|
||||
|
||||
public static long Copy(FileInputStream in, File file2) throws IOException {
|
||||
// CHECK THE FILE
|
||||
if (!file2.exists()) {
|
||||
file2.createNewFile();
|
||||
}
|
||||
|
||||
// RESET TIME
|
||||
long time = System.currentTimeMillis();
|
||||
|
||||
// I/O SETTING
|
||||
FileOutputStream out = new FileOutputStream(file2);
|
||||
FileChannel inC = in.getChannel();
|
||||
FileChannel outC = out.getChannel();
|
||||
ByteBuffer b = null;
|
||||
|
||||
// CAPACITY [2GB]
|
||||
Integer length = 2097152;
|
||||
|
||||
// WORKSPACE
|
||||
while (true) {
|
||||
if (inC.position() == inC.size()) {
|
||||
inC.close();
|
||||
outC.close();
|
||||
return System.currentTimeMillis() - time;
|
||||
}
|
||||
if ((inC.size() - inC.position()) < length) {
|
||||
length = (int) (inC.size()-inC.position());
|
||||
}
|
||||
else {
|
||||
length = 2097152;
|
||||
}
|
||||
|
||||
b = ByteBuffer.allocateDirect(length);
|
||||
inC.read(b);
|
||||
b.flip();
|
||||
outC.write(b);
|
||||
outC.force(false);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -4,6 +4,9 @@ import java.io.*;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.UnsupportedCharsetException;
|
||||
|
||||
/**
|
||||
* @author Unknown
|
||||
*/
|
||||
public class EncodeUtils {
|
||||
|
||||
/**
|
||||
@@ -53,7 +56,7 @@ public class EncodeUtils {
|
||||
*/
|
||||
public static void convert(File file, String fromCharsetName, String toCharsetName, FilenameFilter filter) throws Exception {
|
||||
if (file.isDirectory()) {
|
||||
File[] fileList = null;
|
||||
File[] fileList;
|
||||
if (filter == null) {
|
||||
fileList = file.listFiles();
|
||||
} else {
|
||||
@@ -63,10 +66,8 @@ public class EncodeUtils {
|
||||
convert(f, fromCharsetName, toCharsetName, filter);
|
||||
}
|
||||
} else {
|
||||
if (filter == null
|
||||
|| filter.accept(file.getParentFile(), file.getName())) {
|
||||
String fileContent = getFileContentFromCharset(file,
|
||||
fromCharsetName);
|
||||
if (filter == null || filter.accept(file.getParentFile(), file.getName())) {
|
||||
String fileContent = getFileContentFromCharset(file, fromCharsetName);
|
||||
saveFile2Charset(file, toCharsetName, fileContent);
|
||||
}
|
||||
}
|
||||
@@ -85,8 +86,7 @@ public class EncodeUtils {
|
||||
throw new UnsupportedCharsetException(fromCharsetName);
|
||||
}
|
||||
InputStream inputStream = new FileInputStream(file);
|
||||
InputStreamReader reader = new InputStreamReader(inputStream,
|
||||
fromCharsetName);
|
||||
InputStreamReader reader = new InputStreamReader(inputStream, fromCharsetName);
|
||||
char[] chs = new char[(int) file.length()];
|
||||
reader.read(chs);
|
||||
String str = new String(chs).trim();
|
||||
@@ -110,7 +110,5 @@ public class EncodeUtils {
|
||||
OutputStreamWriter outWrite = new OutputStreamWriter(outputStream, toCharsetName);
|
||||
outWrite.write(content);
|
||||
outWrite.close();
|
||||
|
||||
System.out.println("[Encodeing...] 更改文件: " + file.getPath());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,22 +2,32 @@ package me.skymc.taboolib.fileutils;
|
||||
|
||||
import ch.njol.util.Closeable;
|
||||
import com.ilummc.tlib.util.IO;
|
||||
import javafx.print.PageLayout;
|
||||
import me.skymc.taboolib.Main;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.logging.Level;
|
||||
import java.util.jar.JarFile;
|
||||
|
||||
/**
|
||||
* @author sky
|
||||
*/
|
||||
public class FileUtils {
|
||||
|
||||
/**
|
||||
* 获取本地 IP 地址
|
||||
*
|
||||
* @return {@link String}
|
||||
*/
|
||||
public static String ip() {
|
||||
URL url;
|
||||
URLConnection con;
|
||||
@@ -49,10 +59,77 @@ public class FileUtils {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取插件所有类
|
||||
*
|
||||
* @return {@link List<Class>}
|
||||
*/
|
||||
public static List<Class> getClasses(Class<?> obj) {
|
||||
List<Class> classes = new ArrayList<>();
|
||||
URL url = getCaller(obj).getProtectionDomain().getCodeSource().getLocation();
|
||||
try {
|
||||
File src;
|
||||
try {
|
||||
src = new File(url.toURI());
|
||||
} catch (URISyntaxException e) {
|
||||
src = new File(url.getPath());
|
||||
}
|
||||
new JarFile(src).stream().filter(entry -> entry.getName().endsWith(".class")).forEach(entry -> {
|
||||
String className = entry.getName().replace('/', '.').substring(0, entry.getName().length() - 6);
|
||||
try {
|
||||
classes.add(Class.forName(className, false, obj.getClassLoader()));
|
||||
} catch (Throwable ignored) {
|
||||
}
|
||||
});
|
||||
} catch (Throwable ignored) {
|
||||
}
|
||||
return classes;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取插件所有类
|
||||
*
|
||||
* @return {@link List<Class>}
|
||||
*/
|
||||
public static List<Class> getClasses(Plugin plugin) {
|
||||
List<Class> classes = new ArrayList<>();
|
||||
URL url = plugin.getClass().getProtectionDomain().getCodeSource().getLocation();
|
||||
try {
|
||||
File src;
|
||||
try {
|
||||
src = new File(url.toURI());
|
||||
} catch (URISyntaxException e) {
|
||||
src = new File(url.getPath());
|
||||
}
|
||||
new JarFile(src).stream().filter(entry -> entry.getName().endsWith(".class")).forEach(entry -> {
|
||||
String className = entry.getName().replace('/', '.').substring(0, entry.getName().length() - 6);
|
||||
try {
|
||||
classes.add(Class.forName(className, false, plugin.getClass().getClassLoader()));
|
||||
} catch (Throwable ignored) {
|
||||
}
|
||||
});
|
||||
} catch (Throwable ignored) {
|
||||
}
|
||||
return classes;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取资源文件
|
||||
*
|
||||
* @param filename 文件名
|
||||
* @return {@link InputStream}
|
||||
*/
|
||||
public static InputStream getResource(String filename) {
|
||||
return getResource(Main.getInst(), filename);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取插件资源文件
|
||||
*
|
||||
* @param plugin 插件
|
||||
* @param filename 文件名
|
||||
* @return {@link InputStream}
|
||||
*/
|
||||
public static InputStream getResource(Plugin plugin, String filename) {
|
||||
try {
|
||||
URL url = plugin.getClass().getClassLoader().getResource(filename);
|
||||
@@ -68,6 +145,12 @@ public class FileUtils {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入文件
|
||||
*
|
||||
* @param inputStream 输入流
|
||||
* @param file 文件
|
||||
*/
|
||||
public static void inputStreamToFile(InputStream inputStream, File file) {
|
||||
try {
|
||||
String text = new String(IO.readFully(inputStream), Charset.forName("utf-8"));
|
||||
@@ -93,15 +176,59 @@ public class FileUtils {
|
||||
return file;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检测文件并创建(目录)
|
||||
*
|
||||
* @param file 文件
|
||||
*/
|
||||
public static void createNewFileAndPath(File file) {
|
||||
if (!file.exists()) {
|
||||
String filePath = file.getPath();
|
||||
int index = filePath.lastIndexOf(File.separator);
|
||||
String folderPath;
|
||||
File folder;
|
||||
if ((index >= 0) && (!(folder = new File(filePath.substring(0, index))).exists())) {
|
||||
folder.mkdirs();
|
||||
}
|
||||
try {
|
||||
file.createNewFile();
|
||||
} catch (IOException ignored) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建并获取目录
|
||||
*
|
||||
* @param path 目录文件
|
||||
* @return
|
||||
*/
|
||||
public static File folder(File path) {
|
||||
if (!path.exists()) {
|
||||
path.mkdirs();
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建并获取目录
|
||||
*
|
||||
* @param path 目录地址
|
||||
* @return
|
||||
*/
|
||||
public static File folder(String path) {
|
||||
return folder(new File(path));
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建并获取文件
|
||||
*
|
||||
* @param Path 目录
|
||||
* @param path 目录
|
||||
* @param filePath 地址
|
||||
* @return
|
||||
*/
|
||||
public static File file(File Path, String filePath) {
|
||||
return createNewFile(new File(Path, filePath));
|
||||
public static File file(File path, String filePath) {
|
||||
return createNewFile(new File(path, filePath));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -136,29 +263,29 @@ public class FileUtils {
|
||||
/**
|
||||
* 复制文件夹
|
||||
*
|
||||
* @param file1 文件1
|
||||
* @param file2 文件2
|
||||
* @param originFileName 文件1
|
||||
* @param targetFileName 文件2
|
||||
*/
|
||||
public static void copyAllFile(String file1, String file2) {
|
||||
File _file1 = new File(file1);
|
||||
File _file2 = new File(file2);
|
||||
if (!_file2.exists()) {
|
||||
if (!_file1.isDirectory()) {
|
||||
createNewFile(_file2);
|
||||
public static void copyAllFile(String originFileName, String targetFileName) {
|
||||
File originFile = new File(originFileName);
|
||||
File targetFile = new File(targetFileName);
|
||||
if (!targetFile.exists()) {
|
||||
if (!originFile.isDirectory()) {
|
||||
createNewFile(targetFile);
|
||||
} else {
|
||||
_file2.mkdirs();
|
||||
targetFile.mkdirs();
|
||||
}
|
||||
}
|
||||
if (_file1.isDirectory()) {
|
||||
for (File file : Objects.requireNonNull(_file1.listFiles())) {
|
||||
if (originFile.isDirectory()) {
|
||||
for (File file : Objects.requireNonNull(originFile.listFiles())) {
|
||||
if (file.isDirectory()) {
|
||||
copyAllFile(file.getAbsolutePath(), file2 + "/" + file.getName());
|
||||
copyAllFile(file.getAbsolutePath(), targetFileName + "/" + file.getName());
|
||||
} else {
|
||||
fileChannelCopy(file, new File(file2 + "/" + file.getName()));
|
||||
fileChannelCopy(file, new File(targetFileName + "/" + file.getName()));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
fileChannelCopy(_file1, _file2);
|
||||
fileChannelCopy(originFile, targetFile);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -313,6 +440,17 @@ public class FileUtils {
|
||||
download(downloadURL, new File(saveDir, filename));
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static void close(Closeable closeable) {
|
||||
try {
|
||||
if (closeable != null) {
|
||||
closeable.close();
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static byte[] read(InputStream in) {
|
||||
byte[] buffer = new byte[1024];
|
||||
int len;
|
||||
@@ -326,12 +464,17 @@ public class FileUtils {
|
||||
return bos.toByteArray();
|
||||
}
|
||||
|
||||
public static void close(Closeable closeable) {
|
||||
// *********************************
|
||||
//
|
||||
// Private Methods
|
||||
//
|
||||
// *********************************
|
||||
|
||||
private static Class getCaller(Class<?> obj) {
|
||||
try {
|
||||
if (closeable != null) {
|
||||
closeable.close();
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
return Class.forName(Thread.currentThread().getStackTrace()[3].getClassName(), false, obj.getClassLoader());
|
||||
} catch (ClassNotFoundException ignored) {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,48 +0,0 @@
|
||||
package me.skymc.taboolib.fileutils;
|
||||
|
||||
import me.skymc.taboolib.Main;
|
||||
import me.skymc.taboolib.other.DateUtils;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
|
||||
@Deprecated
|
||||
public class LogUtils {
|
||||
|
||||
public static void Log(String s, String s2) {
|
||||
try {
|
||||
File file = new File(Main.getInst().getDataFolder(), s2 + ".txt");
|
||||
if (!file.exists()) {
|
||||
file.createNewFile();
|
||||
}
|
||||
|
||||
FileWriter fileWritter = new FileWriter(file, true);
|
||||
BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
|
||||
bufferWritter.write(s);
|
||||
bufferWritter.newLine();
|
||||
bufferWritter.close();
|
||||
} catch (Exception e) {
|
||||
Main.getInst().getLogger().warning(s2 + ":" + s);
|
||||
}
|
||||
}
|
||||
|
||||
public static void newLog(Plugin main, String s, String s2) {
|
||||
try {
|
||||
File file = new File(main.getDataFolder(), s2 + ".txt");
|
||||
if (!file.exists()) {
|
||||
file.createNewFile();
|
||||
}
|
||||
|
||||
FileWriter fileWritter = new FileWriter(file, true);
|
||||
BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
|
||||
bufferWritter.write("[" + DateUtils.CH_ALL.format(System.currentTimeMillis()) + "]" + s);
|
||||
bufferWritter.newLine();
|
||||
bufferWritter.close();
|
||||
} catch (Exception e) {
|
||||
Main.getInst().getLogger().warning(s2 + ":" + s);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
153
src/main/java/me/skymc/taboolib/fileutils/TLogs.java
Normal file
153
src/main/java/me/skymc/taboolib/fileutils/TLogs.java
Normal file
@@ -0,0 +1,153 @@
|
||||
package me.skymc.taboolib.fileutils;
|
||||
|
||||
import com.ilummc.tlib.resources.TLocale;
|
||||
import com.ilummc.tlib.util.Strings;
|
||||
import me.skymc.taboolib.TabooLib;
|
||||
import me.skymc.taboolib.commands.internal.BaseMainCommand;
|
||||
import me.skymc.taboolib.commands.internal.BaseSubCommand;
|
||||
import me.skymc.taboolib.commands.internal.TCommand;
|
||||
import me.skymc.taboolib.commands.internal.type.CommandArgument;
|
||||
import me.skymc.taboolib.commands.internal.type.CommandRegister;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.text.SimpleDateFormat;
|
||||
|
||||
/**
|
||||
* @author sky
|
||||
*/
|
||||
@TCommand(name = "tabooliblogs")
|
||||
public class TLogs extends BaseMainCommand {
|
||||
|
||||
private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
|
||||
|
||||
@Override
|
||||
public String getCommandTitle() {
|
||||
return TLocale.asString("COMMANDS.TLOGS.COMMAND-TITLE");
|
||||
}
|
||||
|
||||
@CommandRegister(priority = 0)
|
||||
BaseSubCommand info = new BaseSubCommand() {
|
||||
@Override
|
||||
public String getLabel() {
|
||||
return "info";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return TLocale.asString("COMMANDS.TLOGS.INFO.DESCRIPTION");
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommandArgument[] getArguments() {
|
||||
return new CommandArgument[] {
|
||||
new CommandArgument(TLocale.asString("COMMANDS.TLOGS.INFO.ARGUMENTS.0")),
|
||||
new CommandArgument(TLocale.asString("COMMANDS.TLOGS.INFO.ARGUMENTS.1"))
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
info(args[0], args[1]);
|
||||
if (sender instanceof Player) {
|
||||
TLocale.sendTo(sender, "COMMANDS.TLOGS.INFO.SUCCESS");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@CommandRegister(priority = 1)
|
||||
BaseSubCommand error = new BaseSubCommand() {
|
||||
@Override
|
||||
public String getLabel() {
|
||||
return "error";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return TLocale.asString("COMMANDS.TLOGS.ERROR.DESCRIPTION");
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommandArgument[] getArguments() {
|
||||
return new CommandArgument[] {
|
||||
new CommandArgument(TLocale.asString("COMMANDS.TLOGS.ERROR.ARGUMENTS.0")),
|
||||
new CommandArgument(TLocale.asString("COMMANDS.TLOGS.ERROR.ARGUMENTS.1"))
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
info(args[0], args[1]);
|
||||
if (sender instanceof Player) {
|
||||
TLocale.sendTo(sender, "COMMANDS.TLOGS.ERROR.SUCCESS");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@CommandRegister(priority = 2)
|
||||
BaseSubCommand warning = new BaseSubCommand() {
|
||||
@Override
|
||||
public String getLabel() {
|
||||
return "warning";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return TLocale.asString("COMMANDS.TLOGS.WARNING.DESCRIPTION");
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommandArgument[] getArguments() {
|
||||
return new CommandArgument[] {
|
||||
new CommandArgument(TLocale.asString("COMMANDS.TLOGS.WARNING.ARGUMENTS.0")),
|
||||
new CommandArgument(TLocale.asString("COMMANDS.TLOGS.WARNING.ARGUMENTS.1"))
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
info(args[0], args[1]);
|
||||
if (sender instanceof Player) {
|
||||
TLocale.sendTo(sender, "COMMANDS.TLOGS.WARNING.SUCCESS");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
public static void info(String filePath, String text) {
|
||||
info(new File(!filePath.contains(".") ? filePath + ".txt" : filePath), text);
|
||||
}
|
||||
|
||||
public static void info(File file, String text) {
|
||||
write(file, "[{0} INFO]: {1}\n", text);
|
||||
}
|
||||
|
||||
public static void error(String filePath, String text) {
|
||||
info(new File(!filePath.contains(".") ? filePath + ".txt" : filePath), text);
|
||||
}
|
||||
|
||||
public static void error(File file, String text) {
|
||||
write(file, "[{0} ERROR]: {1}\n", text);
|
||||
}
|
||||
|
||||
public static void warning(String filePath, String text) {
|
||||
info(new File(!filePath.contains(".") ? filePath + ".txt" : filePath), text);
|
||||
}
|
||||
|
||||
public static void warning(File file, String text) {
|
||||
write(file, "[{0} WARNING]: {1}\n", text);
|
||||
}
|
||||
|
||||
public static void write(File file, String format, String text) {
|
||||
Bukkit.getScheduler().runTask(TabooLib.instance(), () -> {
|
||||
FileUtils.createNewFileAndPath(file);
|
||||
try (FileWriter writer = new FileWriter(file, true)) {
|
||||
writer.write(Strings.replaceWithOrder(format, dateFormat.format(System.currentTimeMillis()), text));
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user