Make serialization util class
This commit is contained in:
parent
68dceaff13
commit
1644a4b07d
@ -9,15 +9,14 @@ import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
|
|||||||
import net.dv8tion.jda.api.interactions.components.ActionRow;
|
import net.dv8tion.jda.api.interactions.components.ActionRow;
|
||||||
import net.dv8tion.jda.api.interactions.components.ItemComponent;
|
import net.dv8tion.jda.api.interactions.components.ItemComponent;
|
||||||
import net.dv8tion.jda.api.interactions.components.buttons.Button;
|
import net.dv8tion.jda.api.interactions.components.buttons.Button;
|
||||||
import org.apache.commons.lang3.SerializationException;
|
|
||||||
import org.apache.commons.text.StringEscapeUtils;
|
import org.apache.commons.text.StringEscapeUtils;
|
||||||
import org.apache.commons.text.WordUtils;
|
import org.apache.commons.text.WordUtils;
|
||||||
import org.jsoup.nodes.Element;
|
import org.jsoup.nodes.Element;
|
||||||
import org.jsoup.select.Elements;
|
import org.jsoup.select.Elements;
|
||||||
import wtf.beatrice.hidekobot.Cache;
|
import wtf.beatrice.hidekobot.Cache;
|
||||||
import wtf.beatrice.hidekobot.datasources.DatabaseSource;
|
import wtf.beatrice.hidekobot.datasources.DatabaseSource;
|
||||||
|
import wtf.beatrice.hidekobot.util.SerializationUtil;
|
||||||
|
|
||||||
import java.io.*;
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
public class UrbanDictionary
|
public class UrbanDictionary
|
||||||
@ -88,40 +87,12 @@ public class UrbanDictionary
|
|||||||
return embedBuilder.build();
|
return embedBuilder.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String serialize(List dataList) {
|
|
||||||
|
|
||||||
try (ByteArrayOutputStream bo = new ByteArrayOutputStream();
|
public static String getTermNotFoundError()
|
||||||
ObjectOutputStream so = new ObjectOutputStream(bo)) {
|
|
||||||
so.writeObject(dataList);
|
|
||||||
so.flush();
|
|
||||||
return Base64.getEncoder().encodeToString(bo.toByteArray());
|
|
||||||
}
|
|
||||||
catch (IOException ignored) {}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static LinkedList deserialize(String dataStr) {
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String getNoTermFoundError()
|
|
||||||
{
|
{
|
||||||
return "\uD83D\uDE22 I couldn't find that term!";
|
return "\uD83D\uDE22 I couldn't find that term!";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public static void track(Message message, User user, UrbanSearch search, String sanitizedTerm)
|
public static void track(Message message, User user, UrbanSearch search, String sanitizedTerm)
|
||||||
{
|
{
|
||||||
Cache.getDatabaseSource().queueDisabling(message);
|
Cache.getDatabaseSource().queueDisabling(message);
|
||||||
@ -134,7 +105,6 @@ public class UrbanDictionary
|
|||||||
sanitizedTerm);
|
sanitizedTerm);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static void changePage(ButtonInteractionEvent event, ChangeType changeType)
|
public static void changePage(ButtonInteractionEvent event, ChangeType changeType)
|
||||||
{
|
{
|
||||||
String messageId = event.getMessageId();
|
String messageId = event.getMessageId();
|
||||||
@ -224,10 +194,10 @@ public class UrbanDictionary
|
|||||||
this.serializedContributors = serializedContributors;
|
this.serializedContributors = serializedContributors;
|
||||||
this.serializedDates = serializedDates;
|
this.serializedDates = serializedDates;
|
||||||
|
|
||||||
this.plaintextMeanings = UrbanDictionary.deserialize(serializedMeanings);
|
this.plaintextMeanings = SerializationUtil.deserializeBase64(serializedMeanings);
|
||||||
this.plaintextExamples = UrbanDictionary.deserialize(serializedExamples);
|
this.plaintextExamples = SerializationUtil.deserializeBase64(serializedExamples);
|
||||||
this.contributorsNames = UrbanDictionary.deserialize(serializedContributors);
|
this.contributorsNames = SerializationUtil.deserializeBase64(serializedContributors);
|
||||||
this.submissionDates = UrbanDictionary.deserialize(serializedDates);
|
this.submissionDates = SerializationUtil.deserializeBase64(serializedDates);
|
||||||
|
|
||||||
this.pages = submissionDates.size();
|
this.pages = submissionDates.size();
|
||||||
}
|
}
|
||||||
@ -297,10 +267,10 @@ public class UrbanDictionary
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
serializedMeanings = serialize(plaintextMeanings);
|
serializedMeanings = SerializationUtil.serializeBase64(plaintextMeanings);
|
||||||
serializedExamples = serialize(plaintextExamples);
|
serializedExamples = SerializationUtil.serializeBase64(plaintextExamples);
|
||||||
serializedContributors = serialize(contributorsNames);
|
serializedContributors = SerializationUtil.serializeBase64(contributorsNames);
|
||||||
serializedDates = serialize(submissionDates);
|
serializedDates = SerializationUtil.serializeBase64(submissionDates);
|
||||||
|
|
||||||
pages = submissionDates.size();
|
pages = submissionDates.size();
|
||||||
}
|
}
|
||||||
|
@ -64,7 +64,7 @@ public class UrbanDictionaryCommand implements MessageCommand
|
|||||||
try {
|
try {
|
||||||
doc = Jsoup.connect(url).get();
|
doc = Jsoup.connect(url).get();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
event.getMessage().reply(UrbanDictionary.getNoTermFoundError()).queue();
|
event.getMessage().reply(UrbanDictionary.getTermNotFoundError()).queue();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,7 +57,7 @@ public class UrbanDictionaryCommand extends SlashCommandImpl
|
|||||||
try {
|
try {
|
||||||
doc = Jsoup.connect(url).get();
|
doc = Jsoup.connect(url).get();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
event.reply(UrbanDictionary.getNoTermFoundError())
|
event.reply(UrbanDictionary.getTermNotFoundError())
|
||||||
.setEphemeral(true)
|
.setEphemeral(true)
|
||||||
.queue();
|
.queue();
|
||||||
return;
|
return;
|
||||||
|
@ -0,0 +1,38 @@
|
|||||||
|
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
|
||||||
|
{
|
||||||
|
|
||||||
|
public static String serializeBase64(List dataList) {
|
||||||
|
|
||||||
|
try (ByteArrayOutputStream bo = new ByteArrayOutputStream();
|
||||||
|
ObjectOutputStream so = new ObjectOutputStream(bo)) {
|
||||||
|
so.writeObject(dataList);
|
||||||
|
so.flush();
|
||||||
|
return Base64.getEncoder().encodeToString(bo.toByteArray());
|
||||||
|
}
|
||||||
|
catch (IOException ignored) {}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static LinkedList deserializeBase64(String dataStr) {
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user