-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
179 additions
and
0 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
src/main/java/me/ulrich/structure/net/querz/io/Deserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
|
||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/me/ulrich/structure/net/querz/io/ExceptionBiFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/me/ulrich/structure/net/querz/io/ExceptionTriConsumer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
13
src/main/java/me/ulrich/structure/net/querz/io/MaxDepthIO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/me/ulrich/structure/net/querz/io/MaxDepthReachedException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
26
src/main/java/me/ulrich/structure/net/querz/io/Serializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/me/ulrich/structure/net/querz/io/StringDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/me/ulrich/structure/net/querz/io/StringSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |