Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Java] Move storage init to init and get event queue for tests #72

Merged
merged 9 commits into from
Sep 22, 2023
Prev Previous commit
Next Next commit
feat: read file util
  • Loading branch information
arifBurakDemiray committed Sep 21, 2023
commit c09a1fd11801f601cfff933cde97e82742f0abb9
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;
@@ -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);