Skip to content

Commit

Permalink
feat: read file util
Browse files Browse the repository at this point in the history
  • Loading branch information
arifBurakDemiray committed Sep 21, 2023
1 parent 5071dc4 commit c09a1fd
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions sdk-java/src/main/java/ly/count/sdk/java/internal/Utils.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit c09a1fd

Please sign in to comment.