2
2
3
3
import android .os .Handler ;
4
4
import android .os .HandlerThread ;
5
+
5
6
import androidx .annotation .Nullable ;
7
+
6
8
import okhttp3 .*;
7
9
import okhttp3 .internal .http2 .ErrorCode ;
8
10
import okhttp3 .internal .http2 .StreamResetException ;
9
11
import okio .*;
12
+
10
13
import org .json .JSONArray ;
11
14
import org .json .JSONException ;
12
15
import org .json .JSONObject ;
13
16
import org .json .JSONTokener ;
14
17
15
18
import java .io .ByteArrayOutputStream ;
16
19
import java .io .File ;
20
+ import java .io .FileInputStream ;
17
21
import java .io .FileNotFoundException ;
18
22
import java .io .IOException ;
19
23
import java .net .SocketTimeoutException ;
@@ -109,6 +113,7 @@ public void run() {
109
113
}
110
114
});
111
115
}
116
+
112
117
public static void runInBackground (final Runnable runnable ) {
113
118
executor .submit (new Runnable () {
114
119
@ Override
@@ -434,7 +439,7 @@ public Request authenticate(Route route, Response response) throws IOException {
434
439
FormBody .Builder formBody = null ;
435
440
if (android .os .Build .VERSION .SDK_INT >= android .os .Build .VERSION_CODES .KITKAT ) {
436
441
formBody = new FormBody .Builder (StandardCharsets .UTF_8 );
437
- }else {
442
+ } else {
438
443
formBody = new FormBody .Builder (java .nio .charset .Charset .forName ("UTF-8" ));
439
444
}
440
445
@@ -472,7 +477,7 @@ public void onProgress(long loaded, long total) {
472
477
FormBody .Builder formBody = null ;
473
478
if (android .os .Build .VERSION .SDK_INT >= android .os .Build .VERSION_CODES .KITKAT ) {
474
479
formBody = new FormBody .Builder (StandardCharsets .UTF_8 );
475
- }else {
480
+ } else {
476
481
formBody = new FormBody .Builder (java .nio .charset .Charset .forName ("UTF-8" ));
477
482
}
478
483
@@ -883,15 +888,24 @@ public static void writeFile(final byte[] bytes, final String path, final Callba
883
888
Async .executor .submit (new Runnable () {
884
889
@ Override
885
890
public void run () {
886
- BufferedSink sink ;
891
+ File file = new File (path );
892
+ FileOutputStream stream = null ;
887
893
try {
888
- sink = Okio . buffer ( Okio . sink ( new File ( path )) );
889
- sink .write (bytes );
894
+ stream = new FileOutputStream ( file );
895
+ stream .write (bytes );
890
896
callback .onComplete (null );
891
897
} catch (FileNotFoundException e ) {
892
898
callback .onError (e .getMessage (), e );
893
899
} catch (IOException e ) {
894
900
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
+ }
895
909
}
896
910
}
897
911
});
@@ -902,28 +916,29 @@ public static void readFile(final String path, final @Nullable Options options,
902
916
@ Override
903
917
public void run () {
904
918
File file = new File (path );
905
- BufferedSource source = null ;
906
- Sink sink = null ;
919
+ FileInputStream stream = null ;
907
920
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
+
912
933
callback .onComplete (outputStream .buf ());
913
934
} catch (FileNotFoundException e ) {
914
935
callback .onError (e .getMessage (), e );
915
936
} catch (IOException e ) {
916
937
callback .onError (e .getMessage (), e );
917
938
} finally {
918
939
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 ();
927
942
}
928
943
} catch (IOException ignored ) {
929
944
}
0 commit comments