Class Base64SerializationUtils

java.lang.Object
org.daiitech.naftah.utils.Base64SerializationUtils

public final class Base64SerializationUtils extends Object
Utility class for serializing and deserializing Java objects to and from Base64 strings.

Supports both in-memory and file-based operations. All methods are static and the class cannot be instantiated.

Usage examples:


 String base64 = Base64SerializationUtils.serialize(myObject);
 MyClass obj = (MyClass) Base64SerializationUtils.deserialize(base64);
 
Author:
Chakib Daii
  • Constructor Details

    • Base64SerializationUtils

      private Base64SerializationUtils()
      Private constructor to prevent instantiation.
  • Method Details

    • serialize

      public static String serialize(Serializable o) throws IOException
      Serializes a Serializable object to a Base64-encoded string.
      Parameters:
      o - the serializable object to serialize
      Returns:
      a Base64-encoded string representation of the object
      Throws:
      IOException - if an I/O error occurs during serialization
    • serialize

      public static String serialize(Serializable o, Path path) throws IOException
      Serializes a Serializable object to a Base64-encoded string and writes it to a file.
      Parameters:
      o - the serializable object to serialize
      path - the file path where the Base64 string should be saved
      Returns:
      the string path to the file
      Throws:
      IOException - if an I/O error occurs during serialization or file writing
    • serialize

      public static String serialize(Serializable o, File file) throws IOException
      Serializes a Serializable object to a Base64-encoded string and writes it to a file.
      Parameters:
      o - the serializable object to serialize
      file - the file where the Base64 string should be saved
      Returns:
      the string path to the file
      Throws:
      IOException - if an I/O error occurs during serialization or file writing
    • deserialize

      public static Object deserialize(String s) throws IOException, ClassNotFoundException
      Deserializes an object from a Base64-encoded string.
      Parameters:
      s - the Base64 string to deserialize
      Returns:
      the deserialized object
      Throws:
      IOException - if an I/O error occurs during deserialization
      ClassNotFoundException - if the class of the serialized object cannot be found
    • deserialize

      public static Object deserialize(Path path) throws IOException, ClassNotFoundException
      Deserializes an object from a file containing a Base64-encoded string.
      Parameters:
      path - the path to the file
      Returns:
      the deserialized object
      Throws:
      IOException - if an I/O error occurs during file reading or deserialization
      ClassNotFoundException - if the class of the serialized object cannot be found
    • deserialize

      public static Object deserialize(File file) throws IOException, ClassNotFoundException
      Deserializes an object from a file containing a Base64-encoded string.
      Parameters:
      file - the file containing the Base64 string
      Returns:
      the deserialized object
      Throws:
      IOException - if an I/O error occurs during file reading or deserialization
      ClassNotFoundException - if the class of the serialized object cannot be found