Skip to content

Commit

Permalink
Restructure utility classes
Browse files Browse the repository at this point in the history
  • Loading branch information
ME1312 committed Oct 23, 2021
1 parent b571cfe commit 384fa8d
Show file tree
Hide file tree
Showing 38 changed files with 547 additions and 397 deletions.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.ME1312.Galaxi.Library.Config;

import net.ME1312.Galaxi.Library.Try;
import net.ME1312.Galaxi.Library.Util;

import org.yaml.snakeyaml.DumperOptions;
Expand Down Expand Up @@ -27,7 +28,7 @@ public class YAMLConfig {
*/
@SuppressWarnings("unchecked")
public YAMLConfig(File file) throws IOException, YAMLException {
if (Util.isNull(file)) throw new NullPointerException();
Util.nullpo(file);
this.file = file;
this.yaml = new Yaml(getDumperOptions());
if (file.exists()) {
Expand All @@ -54,7 +55,7 @@ public YAMLSection get() {
* @param value Value
*/
public void set(YAMLSection value) {
if (Util.isNull(value)) throw new NullPointerException();
Util.nullpo(value);
config = value;
}

Expand Down Expand Up @@ -105,10 +106,10 @@ public String toString() {

static DumperOptions getDumperOptions() {
DumperOptions options = new DumperOptions();
Util.isException(() -> options.setAllowUnicode(false));
Util.isException(() -> options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK));
Util.isException(() -> options.setSplitLines(false));
Util.isException(() -> options.setIndent(2));
Try.all.run(() -> options.setAllowUnicode(false));
Try.all.run(() -> options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK));
Try.all.run(() -> options.setSplitLines(false));
Try.all.run(() -> options.setIndent(2));

return options;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public YAMLSection() {
* @throws YAMLException
*/
public YAMLSection(InputStream stream) throws YAMLException {
if (Util.isNull(stream)) throw new NullPointerException();
Util.nullpo(stream);
setAll((LinkedHashMap<String, Object>) (this.yaml = new Yaml(YAMLConfig.getDumperOptions())).loadAs(stream, LinkedHashMap.class));
}

Expand All @@ -46,7 +46,7 @@ public YAMLSection(InputStream stream) throws YAMLException {
* @throws YAMLException
*/
public YAMLSection(Reader reader) throws YAMLException {
if (Util.isNull(reader)) throw new NullPointerException();
Util.nullpo(reader);
setAll((LinkedHashMap<String, Object>) (this.yaml = new Yaml(YAMLConfig.getDumperOptions())).loadAs(reader, LinkedHashMap.class));
}

Expand All @@ -57,7 +57,7 @@ public YAMLSection(Reader reader) throws YAMLException {
* @throws YAMLException
*/
public YAMLSection(String str) throws YAMLException {
if (Util.isNull(str)) throw new NullPointerException();
Util.nullpo(str);
setAll((LinkedHashMap<String, Object>) (this.yaml = new Yaml(YAMLConfig.getDumperOptions())).loadAs(str, LinkedHashMap.class));
}

Expand Down
162 changes: 162 additions & 0 deletions GalaxiAPI/global/src/net/ME1312/Galaxi/Library/Directories.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package net.ME1312.Galaxi.Library;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.util.LinkedList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public final class Directories {
private Directories() {}

/**
* Copy a Directory
*
* @param from Source
* @param to Destination
*/
public static void copy(File from, File to) {
if (from.isDirectory() && !Files.isSymbolicLink(from.toPath())) {
if (!to.exists()) {
to.mkdirs();
}

String files[] = from.list();

for (String file : files) {
File srcFile = new File(from, file);
File destFile = new File(to, file);

copy(srcFile, destFile);
}
} else {
try {
if (!to.exists()) Files.copy(from.toPath(), to.toPath(), LinkOption.NOFOLLOW_LINKS);
} catch (Exception e) {
e.printStackTrace();
}
}
}

/**
* Search a Directory
*
* @param folder Location
* @return List of all found file paths
*/
public static List<String> search(File folder) {
return search(folder, folder);
}

private static List<String> search(File origin, File file) {
List<String> list = new LinkedList<String>();
if (file.isFile()) {
if (origin == file) {
list.add(file.getName());
} else {
list.add(file.getAbsolutePath().substring(origin.getAbsolutePath().length()+1));
}
}
if (file.isDirectory()) for (File next : file.listFiles()) {
list.addAll(search(origin, next));
}
return list;
}

/**
* Zip a Directory
*
* @param folder Location
* @param zip Zip Data Stream
*/
public static void zip(File folder, OutputStream zip) {
File dir = (folder.isFile())?folder.getParentFile():folder;
byte[] buffer = new byte[4096];

try {
ZipOutputStream zos = new ZipOutputStream(zip);

for (String next : search(folder)) {
zos.putNextEntry(new ZipEntry(next.replace(File.separatorChar, '/')));
FileInputStream in = new FileInputStream(dir.getAbsolutePath() + File.separator + next);

int len;
while ((len = in.read(buffer)) != -1) {
zos.write(buffer, 0, len);
}

in.close();
}

zos.closeEntry();
zos.close();
} catch(IOException ex) {
ex.printStackTrace();
}
}

/**
* Unzip a Directory
*
* @param zip Zip Data Stream
* @param folder Parent Location
*/
public static void unzip(InputStream zip, File folder) {
byte[] buffer = new byte[4096];
try{
ZipInputStream zis = new ZipInputStream(zip);
ZipEntry ze;
while ((ze = zis.getNextEntry()) != null) {
File newFile = new File(folder + File.separator + ze.getName().replace('/', File.separatorChar));
if (newFile.exists()) {
if (newFile.isDirectory()) {
delete(newFile);
} else {
newFile.delete();
}
}
if (ze.isDirectory()) {
newFile.mkdirs();
continue;
} else if (!newFile.getParentFile().exists()) {
newFile.getParentFile().mkdirs();
}
FileOutputStream fos = new FileOutputStream(newFile);
int len;
while ((len = zis.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}

fos.close();
}
zis.closeEntry();
zis.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}

/**
* Delete Directory
*
* @param folder Location
*/
public static void delete(File folder) {
File[] files = folder.listFiles();
if(files!=null) {
for(File f : files) {
if(f.isDirectory() && !Files.isSymbolicLink(f.toPath())) {
delete(f);
} else try {
Files.delete(f.toPath());
} catch (IOException e) {
e.printStackTrace();
}
}
}
folder.delete();
}
}
Loading

0 comments on commit 384fa8d

Please sign in to comment.