diff --git a/Plan/src/main/java/com/djrapitops/plan/utilities/Base64Util.java b/Plan/src/main/java/com/djrapitops/plan/utilities/Base64Util.java index 0e3a2e5bb..009d34e1e 100644 --- a/Plan/src/main/java/com/djrapitops/plan/utilities/Base64Util.java +++ b/Plan/src/main/java/com/djrapitops/plan/utilities/Base64Util.java @@ -21,11 +21,19 @@ public class Base64Util { private Base64Util() { } + public static String encodeBytes(byte[] bytes) { + return new String(Base64.getEncoder().encode(bytes)); + } + public static String encode(String decoded) { byte[] encoded = Base64.getEncoder().encode(decoded.getBytes()); return new String(encoded); } + public static byte[] decodeBytes(String encoded) { + return Base64.getDecoder().decode(encoded.getBytes()); + } + public static String decode(String encoded) { byte[] decoded = Base64.getDecoder().decode(encoded.getBytes()); return new String(decoded); diff --git a/Plan/src/main/java/com/djrapitops/plan/utilities/java/Serializer.java b/Plan/src/main/java/com/djrapitops/plan/utilities/java/Serializer.java new file mode 100644 index 000000000..2d38b3446 --- /dev/null +++ b/Plan/src/main/java/com/djrapitops/plan/utilities/java/Serializer.java @@ -0,0 +1,50 @@ +package com.djrapitops.plan.utilities.java; + +import com.djrapitops.plan.data.store.Type; + +import java.io.*; + +/** + * Utility class for storing {@link Serializable} things. + * + * @author Rsl1122 + */ +public class Serializer { + + private final Type type; + + public Serializer(Type type) { + this.type = type; + } + + /** + * Serializes an object. + * + * @param object Object to Serialize. + * @return byte array that contains the serialized object. + * @throws IOException If output fails. + * @throws NotSerializableException If object does not implement Serializable. + */ + public byte[] serialize(T object) throws IOException { + try (ByteArrayOutputStream out = new ByteArrayOutputStream()) { + try (ObjectOutput oo = new ObjectOutputStream(out)) { + oo.writeObject(object); + } + return out.toByteArray(); + } + } + + /** + * De-serializes an object. + * + * @param bytes byte array that contains the serialized object. + * @return De-serialized object. + * @throws IOException If input fails. + * @throws ClassNotFoundException If a Serialized class is not found. + */ + public T deserialize(byte[] bytes) throws IOException, ClassNotFoundException { + try (ObjectInput oi = new ObjectInputStream(new ByteArrayInputStream(bytes))) { + return (T) oi.readObject(); + } + } +} \ No newline at end of file diff --git a/Plan/src/test/java/com/djrapitops/plan/utilities/java/SerializerTest.java b/Plan/src/test/java/com/djrapitops/plan/utilities/java/SerializerTest.java new file mode 100644 index 000000000..f55b98d4b --- /dev/null +++ b/Plan/src/test/java/com/djrapitops/plan/utilities/java/SerializerTest.java @@ -0,0 +1,41 @@ +package com.djrapitops.plan.utilities.java; + +import com.djrapitops.plan.data.store.Type; +import com.djrapitops.plan.utilities.Base64Util; +import com.djrapitops.plugin.utilities.Format; +import org.junit.Before; +import org.junit.Test; + +import java.io.IOException; +import java.io.Serializable; +import java.util.function.Function; + +import static org.junit.Assert.assertEquals; + +/** + * Test for {@link Serializer}. + * + * @author Rsl1122 + */ +public class SerializerTest { + + private String store; + + @Before + public void setUp() throws Exception { + Function function = (Function & Serializable) + string -> new Format(string).removeSymbols().toString(); + + Serializer> serializer = new Serializer<>(Type.of(function)); + byte[] output = serializer.serialize(function); + store = Base64Util.encodeBytes(output); + } + + @Test + public void test() throws IOException, ClassNotFoundException { + Function function = new Serializer<>(new Type>() {}) + .deserialize(Base64Util.decodeBytes(store)); + String result = function.apply("no-,.-.,-.,-.,-"); + assertEquals("no", result); + } +} \ No newline at end of file