mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2024-09-28 22:47:38 +02:00
Removed Serializer
This commit is contained in:
parent
c985a9536f
commit
f39e1cd95c
@ -1,66 +0,0 @@
|
|||||||
/*
|
|
||||||
* This file is part of Player Analytics (Plan).
|
|
||||||
*
|
|
||||||
* Plan is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU Lesser General Public License v3 as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* Plan is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU Lesser General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU Lesser General Public License
|
|
||||||
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,41 +0,0 @@
|
|||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user