diff --git a/sdk-java/src/main/java/ly/count/sdk/java/internal/Utils.java b/sdk-java/src/main/java/ly/count/sdk/java/internal/Utils.java index e899412b7..fc966e1f4 100644 --- a/sdk-java/src/main/java/ly/count/sdk/java/internal/Utils.java +++ b/sdk-java/src/main/java/ly/count/sdk/java/internal/Utils.java @@ -1,6 +1,9 @@ package ly.count.sdk.java.internal; +import java.io.BufferedReader; import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; @@ -291,6 +294,32 @@ public static boolean isValidDataType(Object value) { value instanceof Float; } + /** + * Read file content using UTF-8 encoding into a string and + * append lines to a "StringBuilder" and return it + * If file doesn't exist, return empty string + * + * @param file to read + * @return file contents or empty string + * @throws IOException if file exists but couldn't be read + */ + public static String readFileContent(File file) throws IOException { + StringBuilder fileContent = new StringBuilder(); + + if (!file.exists()) { + return fileContent.toString(); + } + + try (BufferedReader reader = new BufferedReader(new FileReader(file))) { + String line; + while ((line = reader.readLine()) != null) { + fileContent.append(line); + } + } + + return fileContent.toString(); + } + public static class Base64 { public static String encode(byte[] bytes) { return ly.count.sdk.java.internal.Base64.encodeBytes(bytes);