forked from UnlegitDqrk/unlegitlibrary
Initial commit
This commit is contained in:
87
src/main/java/me/finn/unlegitlibrary/file/ClassDefiner.java
Normal file
87
src/main/java/me/finn/unlegitlibrary/file/ClassDefiner.java
Normal file
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
|
||||
*
|
||||
* You are unauthorized to remove this copyright.
|
||||
* You have to give Credits to the Author in your project and link this GitHub site: https://github.com/UnlegitDqrk
|
||||
* See LICENSE-File if exists
|
||||
*/
|
||||
|
||||
package me.finn.unlegitlibrary.file;
|
||||
|
||||
import me.finn.unlegitlibrary.utils.DefaultMethodsOverrider;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.WeakHashMap;
|
||||
|
||||
public class ClassDefiner extends DefaultMethodsOverrider {
|
||||
|
||||
private static final Map<ClassLoader, ClassDefinerLoader> loaders = Collections.synchronizedMap(new WeakHashMap<>());
|
||||
|
||||
public static <T> Class<T> define(final Class<?> parent, final String name, final byte[] data) {
|
||||
return define(parent.getClassLoader(), name, data);
|
||||
}
|
||||
|
||||
public static <T> Class<T> define(final ClassLoader parentLoader, final String name, final byte[] data) {
|
||||
ClassDefinerLoader loader = loaders.computeIfAbsent(parentLoader, ClassDefinerLoader::new);
|
||||
|
||||
synchronized (loader.getClassLoadingLock(name)) {
|
||||
if (loader.hasClass(name)) throw new IllegalStateException(name + " already defined");
|
||||
return (Class<T>) loader.define(name, data);
|
||||
}
|
||||
}
|
||||
|
||||
private static class ClassDefinerLoader extends ClassLoader {
|
||||
static {
|
||||
ClassLoader.registerAsParallelCapable();
|
||||
}
|
||||
|
||||
protected ClassDefinerLoader(final ClassLoader parent) {
|
||||
super(parent);
|
||||
}
|
||||
|
||||
private final Class<?> define(final String name, final byte[] data) {
|
||||
synchronized (this.getClassLoadingLock(name)) {
|
||||
Class<?> c = this.defineClass(name, data, 0, data.length);
|
||||
this.resolveClass(c);
|
||||
return c;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public final Object getClassLoadingLock(final String name) {
|
||||
return super.getClassLoadingLock(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final Object clone() throws CloneNotSupportedException {
|
||||
return super.clone();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final boolean equals(Object obj) {
|
||||
return super.equals(obj);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final String toString() {
|
||||
return super.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final int hashCode() {
|
||||
return super.hashCode();
|
||||
}
|
||||
|
||||
public boolean hasClass(final String name) {
|
||||
synchronized (this.getClassLoadingLock(name)) {
|
||||
try {
|
||||
Class.forName(name);
|
||||
return true;
|
||||
} catch (ClassNotFoundException exception) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
166
src/main/java/me/finn/unlegitlibrary/file/FileUtils.java
Normal file
166
src/main/java/me/finn/unlegitlibrary/file/FileUtils.java
Normal file
@@ -0,0 +1,166 @@
|
||||
/*
|
||||
* Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
|
||||
*
|
||||
* You are unauthorized to remove this copyright.
|
||||
* You have to give Credits to the Author in your project and link this GitHub site: https://github.com/UnlegitDqrk
|
||||
* See LICENSE-File if exists
|
||||
*/
|
||||
|
||||
package me.finn.unlegitlibrary.file;
|
||||
|
||||
import me.finn.unlegitlibrary.utils.DefaultMethodsOverrider;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.*;
|
||||
import java.util.stream.Stream;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipInputStream;
|
||||
|
||||
public class FileUtils extends DefaultMethodsOverrider {
|
||||
|
||||
public static String getSuffix(File file) {
|
||||
String[] splitName = file.getName().split("\\.");
|
||||
return splitName[splitName.length - 1];
|
||||
}
|
||||
|
||||
public static void deleteDirectoryRecursion(File file) {
|
||||
if (file.exists() && file.isDirectory()) {
|
||||
File[] entries = file.listFiles();
|
||||
if (entries != null) for (File entry : entries) deleteDirectoryRecursion(entry);
|
||||
}
|
||||
|
||||
if (!file.exists()) return;
|
||||
file.delete();
|
||||
}
|
||||
|
||||
public static String getName(File file) {
|
||||
String[] splitName = file.getName().split("\\.");
|
||||
return splitName[splitName.length - 2];
|
||||
}
|
||||
|
||||
public static void copyResourceToFile(String resourceName, File targetFile, Class resourceClass) throws IOException {
|
||||
InputStream inputStream = resourceClass.getResourceAsStream("/" + resourceName);
|
||||
OutputStream outputStream = new FileOutputStream(targetFile);
|
||||
|
||||
byte[] buffer = new byte[1024];
|
||||
int bytesRead;
|
||||
while ((bytesRead = inputStream.read(buffer)) != -1) outputStream.write(buffer, 0, bytesRead);
|
||||
}
|
||||
|
||||
public static void unzip(File source, String outputDirectory) throws IOException {
|
||||
ZipInputStream zis = new ZipInputStream(new FileInputStream(source));
|
||||
ZipEntry entry = zis.getNextEntry();
|
||||
|
||||
while (entry != null) {
|
||||
File file = new File(outputDirectory, entry.getName());
|
||||
|
||||
if (entry.isDirectory()) file.mkdirs();
|
||||
else {
|
||||
File parent = file.getParentFile();
|
||||
if (!parent.exists()) parent.mkdirs();
|
||||
|
||||
try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file))) {
|
||||
int bufferSize = Math.toIntExact(entry.getSize());
|
||||
byte[] buffer = new byte[bufferSize > 0 ? bufferSize : 1];
|
||||
int location;
|
||||
|
||||
while ((location = zis.read(buffer)) != -1) bos.write(buffer, 0, location);
|
||||
}
|
||||
}
|
||||
|
||||
entry = zis.getNextEntry();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void downloadFile(String link, File outputFile) throws IOException {
|
||||
URL url = new URL(link);
|
||||
|
||||
BufferedInputStream bis = new BufferedInputStream(url.openStream());
|
||||
FileOutputStream fis = new FileOutputStream(outputFile);
|
||||
|
||||
byte[] buffer = new byte[1024];
|
||||
int count = 0;
|
||||
while ((count = bis.read(buffer, 0, 1024)) != -1) fis.write(buffer, 0, count);
|
||||
|
||||
fis.close();
|
||||
bis.close();
|
||||
}
|
||||
|
||||
public static boolean isEmpty(Path path) throws IOException {
|
||||
if (Files.isDirectory(path)) {
|
||||
try (Stream<Path> entries = Files.list(path)) {
|
||||
return !entries.findFirst().isPresent();
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static String readFile(File file) throws IOException {
|
||||
Long length = file.length();
|
||||
byte[] content = new byte[length.intValue()];
|
||||
|
||||
FileInputStream inputStream = new FileInputStream(file);
|
||||
inputStream.read(content);
|
||||
inputStream.close();
|
||||
|
||||
return new String(content, StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
public static void copyFile(File sourceFile, File toFolder, boolean replaceExisting) throws IOException {
|
||||
// Check if the source file exists and is a regular file
|
||||
if (!sourceFile.exists() || !sourceFile.isFile()) return;
|
||||
|
||||
// Check if the destination folder exists and is a directory
|
||||
if (!toFolder.exists() || !toFolder.isDirectory()) return;
|
||||
|
||||
// Get the name of the source file
|
||||
String fileName = sourceFile.getName();
|
||||
File destinationFile = new File(toFolder, fileName);
|
||||
|
||||
|
||||
if (replaceExisting) Files.copy(sourceFile.toPath(), destinationFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
|
||||
else Files.copy(sourceFile.toPath(), destinationFile.toPath());
|
||||
}
|
||||
|
||||
public static void copyFiles(File fromFolder, File toFolder, boolean replaceExisting) throws IOException {
|
||||
// Check if the source directory exists and is a directory
|
||||
if (!fromFolder.exists() || !fromFolder.isDirectory()) return;
|
||||
|
||||
// Check if the destination directory exists and is a directory
|
||||
if (!toFolder.exists() || !toFolder.isDirectory()) return;
|
||||
|
||||
// List all files in the source directory
|
||||
File[] filesToCopy = fromFolder.listFiles();
|
||||
if (filesToCopy == null) return;
|
||||
|
||||
// Iterate through the files and copy them to the destination directory
|
||||
for (File file : filesToCopy) {
|
||||
Path source = file.toPath();
|
||||
Path destination = new File(toFolder, file.getName()).toPath();
|
||||
|
||||
if (replaceExisting) Files.copy(source, destination, StandardCopyOption.REPLACE_EXISTING);
|
||||
else Files.copy(source, destination);
|
||||
}
|
||||
}
|
||||
|
||||
public static void writeFile(File file, String text) throws IOException {
|
||||
Writer writer = new BufferedWriter(new OutputStreamWriter(Files.newOutputStream(file.toPath()), StandardCharsets.UTF_8));
|
||||
writer.write(text);
|
||||
writer.close();
|
||||
}
|
||||
|
||||
public static void hideFile(File file) throws IOException {
|
||||
if (file.isHidden()) return;
|
||||
Files.setAttribute(Paths.get(file.getPath()), "dos:hidden", true, LinkOption.NOFOLLOW_LINKS);
|
||||
}
|
||||
|
||||
public static void unHideFile(File file) throws IOException {
|
||||
if (!file.isHidden()) return;
|
||||
Files.setAttribute(Paths.get(file.getPath()), "dos:hidden", false, LinkOption.NOFOLLOW_LINKS);
|
||||
}
|
||||
|
||||
}
|
36
src/main/java/me/finn/unlegitlibrary/file/ReflectUtils.java
Normal file
36
src/main/java/me/finn/unlegitlibrary/file/ReflectUtils.java
Normal file
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright (C) 2024 UnlegitDqrk - All Rights Reserved
|
||||
*
|
||||
* You are unauthorized to remove this copyright.
|
||||
* You have to give Credits to the Author in your project and link this GitHub site: https://github.com/UnlegitDqrk
|
||||
* See LICENSE-File if exists
|
||||
*/
|
||||
|
||||
package me.finn.unlegitlibrary.file;
|
||||
|
||||
import me.finn.unlegitlibrary.utils.DefaultMethodsOverrider;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class ReflectUtils extends DefaultMethodsOverrider {
|
||||
|
||||
public static Method getMethodByArgs(final Class<?> clazz, final Class<?>... args) {
|
||||
for (Method method : clazz.getDeclaredMethods()) if (Arrays.equals(method.getParameterTypes(), args)) return method;
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Field getEnumField(final Enum<?> value) throws IllegalAccessException {
|
||||
for (Field field : value.getClass().getDeclaredFields()) {
|
||||
if (!Modifier.isStatic(field.getModifiers())) continue;
|
||||
if (!field.getType().equals(value.getClass())) continue;
|
||||
|
||||
field.setAccessible(true);
|
||||
if (value.equals(field.get(null))) return field;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user