Skip to content

Commit

Permalink
feat(Utils): Added new methods to serialize/deserialize objects.
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeV220 committed May 2, 2023
1 parent 638f592 commit 38368d1
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions utilities/src/main/java/com/georgev22/library/utilities/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,88 @@ public static <T> T deserialize(@NotNull String string, @NotNull Type type) {
return new Gson().fromJson(byteaOut.toString(), type);
}

/**
* Serializes an object and saves it to the specified file path.
* <p>
* Serializes the specified object using the {@link ObjectOutputStream} and saves the serialized data to the file
* specified by the given file path.
* </p>
*
* @param obj the object to be serialized
* @param filePath the file path where the serialized data will be saved
* @throws IOException if there is an error accessing or writing to the file
*/
public static void serializeObject(@NotNull Object obj, @NotNull String filePath) throws IOException {
FileOutputStream fileOutputStream = new FileOutputStream(filePath);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
objectOutputStream.writeObject(obj);
objectOutputStream.flush();
objectOutputStream.close();
fileOutputStream.close();
}

/**
* Serializes an object and returns the serialized data as a string.
* <p>
* Serializes the specified object using the {@link ObjectOutputStream} and returns the serialized data as a string.
*
* @param obj the object to be serialized
* @return the serialized data as a string
* @throws IOException if there is an error during serialization
*/
@Contract("_ -> new")
public static @NotNull String serializeObjectToString(Object obj) throws IOException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(obj);
objectOutputStream.flush();
objectOutputStream.close();
byteArrayOutputStream.close();
return byteArrayOutputStream.toString(StandardCharsets.ISO_8859_1);
}

/**
* Deserializes an object from the specified file path.
* <p>
* Deserializes the object
* stored in the file specified by the given file path using the {@link ObjectInputStream} and
* returns the deserialized object.
*
* @param filePath the file path where the serialized object is stored
* @return the deserialized object
* @throws IOException if there is an error accessing or reading from the file
* @throws ClassNotFoundException if the class of the serialized object cannot be found
*/
public static Object deserializeObject(@NotNull String filePath) throws IOException, ClassNotFoundException {
FileInputStream fileInputStream = new FileInputStream(filePath);
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
Object obj = objectInputStream.readObject();
objectInputStream.close();
fileInputStream.close();
return obj;
}

/**
* Deserializes an object from the specified serialized data.
* <p>
* Deserializes the object from the specified serialized data using the {@link ObjectInputStream} and returns the
* deserialized object.
*
* @param serializedData the serialized object data as a string
* @return the deserialized object
* @throws IOException if there is an error accessing or reading from the serialized data
* @throws ClassNotFoundException if the class of the serialized object cannot be found
*/
public static Object deserializeObjectFromString(String serializedData) throws IOException, ClassNotFoundException {
byte[] byteArray = serializedData.getBytes(StandardCharsets.ISO_8859_1);
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray);
ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
Object obj = objectInputStream.readObject();
objectInputStream.close();
byteArrayInputStream.close();
return obj;
}

/**
* Converts a string list to string.
*
Expand Down

0 comments on commit 38368d1

Please sign in to comment.