Skip to content

Commit

Permalink
Revert read/write file to previous implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
DariaKunoichi committed Dec 15, 2024
1 parent d0ae565 commit ba4f383
Showing 1 changed file with 15 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,28 +153,24 @@ void mkdir(String path, Promise promise) {

void readFile(String path, String encoding, Promise promise) {
Log.w(TAG, "readfile start " + path);
File file = new File(path);
StringBuilder fileContent = new StringBuilder((int) file.length());
try(
FileInputStream fin = new FileInputStream(new File(path));
ByteArrayOutputStream result = new ByteArrayOutputStream();
FileInputStream fin = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fin, encoding);
) {
byte[] buffer = new byte[4096];
char[] buffer = new char[4096];
int charsRead = 0;
while ((charsRead = fin.read(buffer)) != -1) {
result.write(buffer, 0, charsRead);
while ((charsRead = isr.read(buffer)) != -1) {
fileContent.append(buffer, 0, charsRead);
}
String content = fileContent.toString();
Log.w(TAG, "readfile content " + content);

String fileContent = "";
if (encoding == "utf8") {
fileContent = result.toString();
} else if (encoding == "base64") {
fileContent = Base64.encodeToString(result.toByteArray(), Base64.DEFAULT);
}

Log.w(TAG, "readfile content " + fileContent);
promise.resolve(fileContent);
promise.resolve(content);
} catch (Exception e) {
Log.w(TAG, "readfile exception " + e.toString());
promise.reject(e);
Log.w(TAG, "readfile exception " + e.toString());
}
}

Expand All @@ -199,22 +195,15 @@ void writeFile(String path, String data, String encoding, Promise promise){
Log.w(TAG, "writeFile start " + path);
try(
FileOutputStream fout = new FileOutputStream(path);
Writer w = new OutputStreamWriter(fout, encoding);
) {
byte[] buffer = null;
if (encoding == "utf8") {
buffer = data.getBytes();
} else if (encoding == "base64") {
buffer = Base64.decode(data, Base64.DEFAULT);
}

if (buffer != null) {
fout.write(buffer, 0, data.length());
}
w.write(data);

Log.w(TAG, "writeFile end " + path);
promise.resolve(null);
} catch (Exception e) {
Log.w(TAG, "writeFile exception " + e.toString());
promise.reject(e);
Log.w(TAG, "writeFile exception " + e.toString());
}
}

Expand Down

0 comments on commit ba4f383

Please sign in to comment.