From 101370c9ff37727b64bb2551ad1c427ffb405075 Mon Sep 17 00:00:00 2001 From: UlrichBR <38494475+UlrichBR@users.noreply.github.com> Date: Wed, 27 Apr 2022 10:27:35 -0300 Subject: [PATCH] Add files via upload --- .../structure/net/querz/io/Deserializer.java | 42 +++++++++++++++++++ .../net/querz/io/ExceptionBiFunction.java | 7 ++++ .../net/querz/io/ExceptionTriConsumer.java | 7 ++++ .../structure/net/querz/io/MaxDepthIO.java | 13 ++++++ .../querz/io/MaxDepthReachedException.java | 12 ++++++ .../structure/net/querz/io/Serializer.java | 26 ++++++++++++ .../net/querz/io/StringDeserializer.java | 37 ++++++++++++++++ .../net/querz/io/StringSerializer.java | 35 ++++++++++++++++ 8 files changed, 179 insertions(+) create mode 100644 src/main/java/me/ulrich/structure/net/querz/io/Deserializer.java create mode 100644 src/main/java/me/ulrich/structure/net/querz/io/ExceptionBiFunction.java create mode 100644 src/main/java/me/ulrich/structure/net/querz/io/ExceptionTriConsumer.java create mode 100644 src/main/java/me/ulrich/structure/net/querz/io/MaxDepthIO.java create mode 100644 src/main/java/me/ulrich/structure/net/querz/io/MaxDepthReachedException.java create mode 100644 src/main/java/me/ulrich/structure/net/querz/io/Serializer.java create mode 100644 src/main/java/me/ulrich/structure/net/querz/io/StringDeserializer.java create mode 100644 src/main/java/me/ulrich/structure/net/querz/io/StringSerializer.java diff --git a/src/main/java/me/ulrich/structure/net/querz/io/Deserializer.java b/src/main/java/me/ulrich/structure/net/querz/io/Deserializer.java new file mode 100644 index 0000000..d298bbf --- /dev/null +++ b/src/main/java/me/ulrich/structure/net/querz/io/Deserializer.java @@ -0,0 +1,42 @@ +package me.ulrich.structure.net.querz.io; + +import java.io.BufferedInputStream; +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; + +public interface Deserializer { + + T fromStream(InputStream stream) throws IOException; + + default T fromFile(File file) throws IOException { + try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file))) { + return fromStream(bis); + } + } + + default T fromBytes(byte[] data) throws IOException { + ByteArrayInputStream stream = new ByteArrayInputStream(data); + return fromStream(stream); + } + + default T fromResource(Class clazz, String path) throws IOException { + try (InputStream stream = clazz.getClassLoader().getResourceAsStream(path)) { + if (stream == null) { + throw new IOException("resource \"" + path + "\" not found"); + } + return fromStream(stream); + } + } + + default T fromURL(URL url) throws IOException { + try (InputStream stream = url.openStream()) { + return fromStream(stream); + } + } + + +} diff --git a/src/main/java/me/ulrich/structure/net/querz/io/ExceptionBiFunction.java b/src/main/java/me/ulrich/structure/net/querz/io/ExceptionBiFunction.java new file mode 100644 index 0000000..cb18a0d --- /dev/null +++ b/src/main/java/me/ulrich/structure/net/querz/io/ExceptionBiFunction.java @@ -0,0 +1,7 @@ +package me.ulrich.structure.net.querz.io; + +@FunctionalInterface +public interface ExceptionBiFunction { + + R accept(T t, U u) throws E; +} diff --git a/src/main/java/me/ulrich/structure/net/querz/io/ExceptionTriConsumer.java b/src/main/java/me/ulrich/structure/net/querz/io/ExceptionTriConsumer.java new file mode 100644 index 0000000..5741b8e --- /dev/null +++ b/src/main/java/me/ulrich/structure/net/querz/io/ExceptionTriConsumer.java @@ -0,0 +1,7 @@ +package me.ulrich.structure.net.querz.io; + +@FunctionalInterface +public interface ExceptionTriConsumer { + + void accept(T t, U u, V v) throws E; +} diff --git a/src/main/java/me/ulrich/structure/net/querz/io/MaxDepthIO.java b/src/main/java/me/ulrich/structure/net/querz/io/MaxDepthIO.java new file mode 100644 index 0000000..a2fd5a9 --- /dev/null +++ b/src/main/java/me/ulrich/structure/net/querz/io/MaxDepthIO.java @@ -0,0 +1,13 @@ +package me.ulrich.structure.net.querz.io; + +public interface MaxDepthIO { + + default int decrementMaxDepth(int maxDepth) { + if (maxDepth < 0) { + throw new IllegalArgumentException("negative maximum depth is not allowed"); + } else if (maxDepth == 0) { + throw new MaxDepthReachedException("reached maximum depth of NBT structure"); + } + return --maxDepth; + } +} diff --git a/src/main/java/me/ulrich/structure/net/querz/io/MaxDepthReachedException.java b/src/main/java/me/ulrich/structure/net/querz/io/MaxDepthReachedException.java new file mode 100644 index 0000000..7fe1b5f --- /dev/null +++ b/src/main/java/me/ulrich/structure/net/querz/io/MaxDepthReachedException.java @@ -0,0 +1,12 @@ +package me.ulrich.structure.net.querz.io; + +/** + * Exception indicating that the maximum (de-)serialization depth has been reached. + */ +@SuppressWarnings("serial") +public class MaxDepthReachedException extends RuntimeException { + + public MaxDepthReachedException(String msg) { + super(msg); + } +} diff --git a/src/main/java/me/ulrich/structure/net/querz/io/Serializer.java b/src/main/java/me/ulrich/structure/net/querz/io/Serializer.java new file mode 100644 index 0000000..5c1b407 --- /dev/null +++ b/src/main/java/me/ulrich/structure/net/querz/io/Serializer.java @@ -0,0 +1,26 @@ +package me.ulrich.structure.net.querz.io; + +import java.io.BufferedOutputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; + +public interface Serializer { + + void toStream(T object, OutputStream out) throws IOException; + + default void toFile(T object, File file) throws IOException { + try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file))) { + toStream(object, bos); + } + } + + default byte[] toBytes(T object) throws IOException { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + toStream(object, bos); + bos.close(); + return bos.toByteArray(); + } +} diff --git a/src/main/java/me/ulrich/structure/net/querz/io/StringDeserializer.java b/src/main/java/me/ulrich/structure/net/querz/io/StringDeserializer.java new file mode 100644 index 0000000..b432419 --- /dev/null +++ b/src/main/java/me/ulrich/structure/net/querz/io/StringDeserializer.java @@ -0,0 +1,37 @@ +package me.ulrich.structure.net.querz.io; + +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; +import java.io.StringReader; + +public interface StringDeserializer extends Deserializer { + + T fromReader(Reader reader) throws IOException; + + default T fromString(String s) throws IOException { + return fromReader(new StringReader(s)); + } + + @Override + default T fromStream(InputStream stream) throws IOException { + try (Reader reader = new InputStreamReader(stream)) { + return fromReader(reader); + } + } + + @Override + default T fromFile(File file) throws IOException { + try (Reader reader = new FileReader(file)) { + return fromReader(reader); + } + } + + @Override + default T fromBytes(byte[] data) throws IOException { + return fromReader(new StringReader(new String(data))); + } +} diff --git a/src/main/java/me/ulrich/structure/net/querz/io/StringSerializer.java b/src/main/java/me/ulrich/structure/net/querz/io/StringSerializer.java new file mode 100644 index 0000000..e6e9792 --- /dev/null +++ b/src/main/java/me/ulrich/structure/net/querz/io/StringSerializer.java @@ -0,0 +1,35 @@ +package me.ulrich.structure.net.querz.io; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.StringWriter; +import java.io.Writer; + +public interface StringSerializer extends Serializer { + + void toWriter(T object, Writer writer) throws IOException; + + default String toString(T object) throws IOException { + Writer writer = new StringWriter(); + toWriter(object, writer); + writer.flush(); + return writer.toString(); + } + + @Override + default void toStream(T object, OutputStream stream) throws IOException { + Writer writer = new OutputStreamWriter(stream); + toWriter(object, writer); + writer.flush(); + } + + @Override + default void toFile(T object, File file) throws IOException { + try (Writer writer = new FileWriter(file)) { + toWriter(object, writer); + } + } +}