2022-12-20 15:11:44 +01:00
|
|
|
package wtf.beatrice.hidekobot.util;
|
|
|
|
|
|
|
|
import org.apache.commons.lang3.SerializationException;
|
|
|
|
|
|
|
|
import java.io.*;
|
|
|
|
import java.util.Base64;
|
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
public class SerializationUtil
|
|
|
|
{
|
|
|
|
|
2023-01-16 07:07:42 +01:00
|
|
|
private SerializationUtil() {
|
|
|
|
throw new IllegalStateException("Utility class");
|
|
|
|
}
|
|
|
|
|
2023-01-16 03:53:51 +01:00
|
|
|
public static <T> String serializeBase64(List<T> dataList) {
|
2022-12-20 15:11:44 +01:00
|
|
|
|
|
|
|
try (ByteArrayOutputStream bo = new ByteArrayOutputStream();
|
|
|
|
ObjectOutputStream so = new ObjectOutputStream(bo)) {
|
|
|
|
so.writeObject(dataList);
|
|
|
|
so.flush();
|
|
|
|
return Base64.getEncoder().encodeToString(bo.toByteArray());
|
|
|
|
}
|
2022-12-20 15:13:21 +01:00
|
|
|
catch (IOException e) {
|
|
|
|
throw new SerializationException("Error during serialization", e);
|
|
|
|
}
|
2022-12-20 15:11:44 +01:00
|
|
|
}
|
|
|
|
|
2023-01-16 03:53:51 +01:00
|
|
|
public static <T> LinkedList<T> deserializeBase64(String dataStr) {
|
2022-12-20 15:11:44 +01:00
|
|
|
|
|
|
|
byte[] b = Base64.getDecoder().decode(dataStr);
|
|
|
|
ByteArrayInputStream bi = new ByteArrayInputStream(b);
|
|
|
|
ObjectInputStream si;
|
|
|
|
try {
|
|
|
|
si = new ObjectInputStream(bi);
|
|
|
|
return LinkedList.class.cast(si.readObject());
|
|
|
|
}
|
|
|
|
catch (IOException | ClassNotFoundException e) {
|
|
|
|
throw new SerializationException("Error during deserialization", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|