Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
UlrichBR authored Apr 27, 2022
1 parent 965b077 commit 101370c
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/main/java/me/ulrich/structure/net/querz/io/Deserializer.java
Original file line number Diff line number Diff line change
@@ -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> {

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);
}
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package me.ulrich.structure.net.querz.io;

@FunctionalInterface
public interface ExceptionBiFunction <T, U, R, E extends Exception> {

R accept(T t, U u) throws E;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package me.ulrich.structure.net.querz.io;

@FunctionalInterface
public interface ExceptionTriConsumer<T, U, V, E extends Exception> {

void accept(T t, U u, V v) throws E;
}
13 changes: 13 additions & 0 deletions src/main/java/me/ulrich/structure/net/querz/io/MaxDepthIO.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
26 changes: 26 additions & 0 deletions src/main/java/me/ulrich/structure/net/querz/io/Serializer.java
Original file line number Diff line number Diff line change
@@ -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<T> {

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();
}
}
Original file line number Diff line number Diff line change
@@ -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<T> extends Deserializer<T> {

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)));
}
}
Original file line number Diff line number Diff line change
@@ -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<T> extends Serializer<T> {

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);
}
}
}

0 comments on commit 101370c

Please sign in to comment.