Skip to content

Commit f7cd3a7

Browse files
authored
Merge pull request #108 from Umutayb/ServiceGeneratorRefactor#2
Update FileUtilities.java
2 parents 9fad15d + 63ebe25 commit f7cd3a7

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

src/main/java/utils/FileUtilities.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.fasterxml.jackson.annotation.JsonAutoDetect;
55
import com.fasterxml.jackson.annotation.PropertyAccessor;
66
import com.fasterxml.jackson.databind.ObjectMapper;
7+
import com.google.gson.Gson;
78
import com.google.gson.JsonElement;
89
import com.google.gson.JsonObject;
910
import com.google.gson.JsonParser;
@@ -54,6 +55,25 @@ public static String getEncodedString(File image) {
5455
}
5556
}
5657

58+
/**
59+
* Saves a Base64-encoded string as an image file in the specified directory.
60+
*
61+
* @param base64String The Base64-encoded string representing the image.
62+
* @param outputFile The file where the image should be saved.
63+
* @throws RuntimeException if an IOException occurs during file writing.
64+
*/
65+
public static void saveDecodedImage(String base64String, File outputFile) {
66+
try {
67+
// Decode the Base64 string to a byte array
68+
byte[] imageBytes = Base64.getDecoder().decode(base64String);
69+
// Write the byte array to the specified file
70+
Files.write(outputFile.toPath(), imageBytes);
71+
} catch (IOException e) {
72+
// Wrap IOException in a RuntimeException and throw it
73+
throw new RuntimeException(e);
74+
}
75+
}
76+
5777
/**
5878
* Retrieves the Base64-encoded string representation of an image file from the given file path.
5979
*
@@ -438,6 +458,7 @@ public static class Json {
438458
public JSONObject urlsJson = new JSONObject();
439459
public JSONObject notificationJson = new JSONObject();
440460
private final Printer log = new Printer(Json.class);
461+
private static final Gson gson = new Gson();
441462

442463
/**
443464
* Saves a JSON object to a file.
@@ -603,5 +624,27 @@ public static String formatJsonString(String json) {
603624
catch (IOException e) {e.printStackTrace();}
604625
return null;
605626
}
627+
628+
/**
629+
* Converts a given input object to another type using Gson serialization/deserialization.
630+
*
631+
* @param <T> The target class type.
632+
* @param input The input object to be converted.
633+
* @param tClass The target class type to convert into.
634+
* @return An instance of the target class {@code T} populated with data from the input object, or null if conversion fails.
635+
*/
636+
public static <T> T typeConversion(Object input, Class<T> tClass) {
637+
return gson.fromJson(getJsonString(input), tClass);
638+
}
639+
640+
/**
641+
* Converts a given input object to its JSON string representation using Gson.
642+
*
643+
* @param input The input object to be serialized into JSON.
644+
* @return A JSON string representing the input object, or null if the input is null.
645+
*/
646+
public static String getJsonString(Object input) {
647+
return gson.toJson(input);
648+
}
606649
}
607650
}

0 commit comments

Comments
 (0)