Object serializer

This commit is contained in:
Rsl1122 2018-08-07 10:15:26 +03:00
parent a515357736
commit efa4b36b6c
3 changed files with 99 additions and 0 deletions

View File

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

View File

@ -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<T> {
private final Type<T> type;
public Serializer(Type<T> 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();
}
}
}

View File

@ -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<String, String> function = (Function<String, String> & Serializable)
string -> new Format(string).removeSymbols().toString();
Serializer<Function<String, String>> serializer = new Serializer<>(Type.of(function));
byte[] output = serializer.serialize(function);
store = Base64Util.encodeBytes(output);
}
@Test
public void test() throws IOException, ClassNotFoundException {
Function<String, String> function = new Serializer<>(new Type<Function<String, String>>() {})
.deserialize(Base64Util.decodeBytes(store));
String result = function.apply("no-,.-.,-.,-.,-");
assertEquals("no", result);
}
}