Skip to content

Commit d44acb6

Browse files
committed
fix(polyfill): fix file read
1 parent ff17f77 commit d44acb6

File tree

3 files changed

+34
-19
lines changed

3 files changed

+34
-19
lines changed
Binary file not shown.
Binary file not shown.

packages/canvas-polyfill/src-native/AsyncDemo/async/src/main/java/com/github/triniwiz/async/Async.java

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,22 @@
22

33
import android.os.Handler;
44
import android.os.HandlerThread;
5+
56
import androidx.annotation.Nullable;
7+
68
import okhttp3.*;
79
import okhttp3.internal.http2.ErrorCode;
810
import okhttp3.internal.http2.StreamResetException;
911
import okio.*;
12+
1013
import org.json.JSONArray;
1114
import org.json.JSONException;
1215
import org.json.JSONObject;
1316
import org.json.JSONTokener;
1417

1518
import java.io.ByteArrayOutputStream;
1619
import java.io.File;
20+
import java.io.FileInputStream;
1721
import java.io.FileNotFoundException;
1822
import java.io.IOException;
1923
import java.net.SocketTimeoutException;
@@ -109,6 +113,7 @@ public void run() {
109113
}
110114
});
111115
}
116+
112117
public static void runInBackground(final Runnable runnable) {
113118
executor.submit(new Runnable() {
114119
@Override
@@ -434,7 +439,7 @@ public Request authenticate(Route route, Response response) throws IOException {
434439
FormBody.Builder formBody = null;
435440
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
436441
formBody = new FormBody.Builder(StandardCharsets.UTF_8);
437-
}else {
442+
} else {
438443
formBody = new FormBody.Builder(java.nio.charset.Charset.forName("UTF-8"));
439444
}
440445

@@ -472,7 +477,7 @@ public void onProgress(long loaded, long total) {
472477
FormBody.Builder formBody = null;
473478
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
474479
formBody = new FormBody.Builder(StandardCharsets.UTF_8);
475-
}else {
480+
} else {
476481
formBody = new FormBody.Builder(java.nio.charset.Charset.forName("UTF-8"));
477482
}
478483

@@ -883,15 +888,24 @@ public static void writeFile(final byte[] bytes, final String path, final Callba
883888
Async.executor.submit(new Runnable() {
884889
@Override
885890
public void run() {
886-
BufferedSink sink;
891+
File file = new File(path);
892+
FileOutputStream stream = null;
887893
try {
888-
sink = Okio.buffer(Okio.sink(new File(path)));
889-
sink.write(bytes);
894+
stream = new FileOutputStream(file);
895+
stream.write(bytes);
890896
callback.onComplete(null);
891897
} catch (FileNotFoundException e) {
892898
callback.onError(e.getMessage(), e);
893899
} catch (IOException e) {
894900
callback.onError(e.getMessage(), e);
901+
} finally {
902+
if (stream != null) {
903+
try {
904+
stream.flush();
905+
stream.close();
906+
} catch (IOException e) {
907+
}
908+
}
895909
}
896910
}
897911
});
@@ -902,28 +916,29 @@ public static void readFile(final String path, final @Nullable Options options,
902916
@Override
903917
public void run() {
904918
File file = new File(path);
905-
BufferedSource source = null;
906-
Sink sink = null;
919+
FileInputStream stream = null;
907920
try {
908-
source = Okio.buffer(Okio.source(file));
909-
ByteArrayOutputStream2 outputStream = new ByteArrayOutputStream2();
910-
sink = Okio.sink(outputStream);
911-
source.readAll(sink);
921+
stream = new FileInputStream(file);
922+
int count;
923+
byte[] buffer = new byte[1024];
924+
ByteArrayOutputStream2 outputStream = new ByteArrayOutputStream2(stream.available());
925+
926+
while (true) {
927+
count = stream.read(buffer);
928+
if (count <= 0)
929+
break;
930+
outputStream.write(buffer, 0, count);
931+
}
932+
912933
callback.onComplete(outputStream.buf());
913934
} catch (FileNotFoundException e) {
914935
callback.onError(e.getMessage(), e);
915936
} catch (IOException e) {
916937
callback.onError(e.getMessage(), e);
917938
} finally {
918939
try {
919-
if (source != null) {
920-
source.close();
921-
}
922-
} catch (IOException ignored) {
923-
}
924-
try {
925-
if (sink != null) {
926-
sink.close();
940+
if (stream != null) {
941+
stream.close();
927942
}
928943
} catch (IOException ignored) {
929944
}

0 commit comments

Comments
 (0)