11package me .thehandsomeyoni .persistentdataapi .data ;
22
33
4- import me .thehandsomeyoni .persistentdataapi .manager .DataSerializer ;
54import org .bukkit .NamespacedKey ;
65import org .bukkit .persistence .PersistentDataContainer ;
76import org .bukkit .persistence .PersistentDataType ;
87
9- import java .io .Serializable ;
8+ import java .io .* ;
109import java .util .HashMap ;
1110import java .util .HashSet ;
1211import java .util .Set ;
1312
1413import static me .thehandsomeyoni .persistentdataapi .PersistentDataAPI .getJavaPlugin ;
14+ import static me .thehandsomeyoni .persistentdataapi .data .DataContainerManager .DataSerializer .deserialize ;
1515import static me .thehandsomeyoni .persistentdataapi .manager .DataSerializer .serialize ;
1616
1717public class DataContainerManager {
@@ -49,7 +49,7 @@ public byte[] getSerialized (String key) {
4949 * @return The data in the given type.
5050 */
5151 public Serializable getUnserialized (String key ){
52- return DataSerializer . deserialize (container .get (new NamespacedKey (getJavaPlugin (), key ), PersistentDataType .BYTE_ARRAY ));
52+ return deserialize (container .get (new NamespacedKey (getJavaPlugin (), key ), PersistentDataType .BYTE_ARRAY ));
5353 }
5454
5555 /**
@@ -58,7 +58,7 @@ public Serializable getUnserialized(String key){
5858 * @return The data in the given type.
5959 */
6060 public Serializable getUnserialized (NamespacedKey key ){
61- return DataSerializer . deserialize (container .get (key , PersistentDataType .BYTE_ARRAY ));
61+ return deserialize (container .get (key , PersistentDataType .BYTE_ARRAY ));
6262 }
6363
6464 public Set <String > getAllKeys (){
@@ -105,4 +105,54 @@ public boolean has(String key){
105105 return container .has (new NamespacedKey (getJavaPlugin (), key ), PersistentDataType .BYTE_ARRAY );
106106 }
107107
108+ /**
109+ * Simple serializer for serializing and deserializing data.
110+ * @author TheHandsomeYoni
111+ * @version 1.5.0
112+ */
113+ static class DataSerializer {
114+ private static ByteArrayOutputStream byteArrayOutput ;
115+ private static ObjectOutputStream objectOutput ;
116+
117+ private static ByteArrayInputStream byteArrayInput ;
118+ private static ObjectInputStream objectInput ;
119+
120+
121+ /**
122+ * Turns the raw data into byte array.
123+ *
124+ * @param object The data.
125+ * @return The data in bytes.
126+ */
127+ public static byte [] serialize (Object object ) {
128+ try {
129+ byteArrayOutput = new ByteArrayOutputStream ();
130+ objectOutput = new ObjectOutputStream (byteArrayOutput );
131+ objectOutput .writeObject (object );
132+ objectOutput .flush ();
133+ return byteArrayOutput .toByteArray ();
134+ } catch (IOException e ) {
135+ e .printStackTrace ();
136+ return null ;
137+ }
138+ }
139+
140+ /**
141+ * Turns the byte array into data.
142+ *
143+ * @param bytes The bytes.
144+ * @return The data.
145+ */
146+ public static Serializable deserialize (byte [] bytes ) {
147+ byteArrayInput = new ByteArrayInputStream (bytes );
148+ try {
149+ objectInput = new ObjectInputStream (byteArrayInput );
150+ return (Serializable ) objectInput .readObject ();
151+ } catch (IOException | ClassNotFoundException e ) {
152+ e .printStackTrace ();
153+ return null ;
154+ }
155+ }
156+ }
157+
108158}
0 commit comments