|
7 | 7 | import android.os.Build;
|
8 | 8 | import android.os.Environment;
|
9 | 9 | import android.os.StatFs;
|
10 |
| -import android.os.SystemClock; |
11 | 10 | import android.util.Base64;
|
12 | 11 |
|
13 |
| -import com.ReactNativeBlobUtil.Utils.PathResolver; |
14 | 12 | import com.facebook.react.bridge.Arguments;
|
15 | 13 | import com.facebook.react.bridge.Callback;
|
16 | 14 | import com.facebook.react.bridge.Promise;
|
|
20 | 18 | import com.facebook.react.bridge.WritableMap;
|
21 | 19 | import com.facebook.react.modules.core.DeviceEventManagerModule;
|
22 | 20 |
|
23 |
| -import java.io.*; |
24 |
| -import java.nio.charset.Charset; |
| 21 | +import java.io.File; |
| 22 | +import java.io.FileInputStream; |
| 23 | +import java.io.FileNotFoundException; |
| 24 | +import java.io.FileOutputStream; |
| 25 | +import java.io.IOException; |
| 26 | +import java.io.InputStream; |
| 27 | +import java.io.OutputStream; |
25 | 28 | import java.security.MessageDigest;
|
26 | 29 | import java.util.ArrayList;
|
27 | 30 | import java.util.HashMap;
|
28 | 31 | import java.util.Map;
|
29 |
| -import java.util.UUID; |
30 | 32 |
|
31 | 33 | class ReactNativeBlobUtilFS {
|
32 | 34 |
|
@@ -329,106 +331,6 @@ static String getTmpPath(String taskId) {
|
329 | 331 | return ReactNativeBlobUtil.RCTContext.getFilesDir() + "/ReactNativeBlobUtilTmp_" + taskId;
|
330 | 332 | }
|
331 | 333 |
|
332 |
| - /** |
333 |
| - * Create a file stream for read |
334 |
| - * |
335 |
| - * @param path File stream target path |
336 |
| - * @param encoding File stream decoder, should be one of `base64`, `utf8`, `ascii` |
337 |
| - * @param bufferSize Buffer size of read stream, default to 4096 (4095 when encode is `base64`) |
338 |
| - */ |
339 |
| - void readStream(String path, String encoding, int bufferSize, int tick, final String streamId) { |
340 |
| - String resolved = ReactNativeBlobUtilUtils.normalizePath(path); |
341 |
| - if (resolved != null) |
342 |
| - path = resolved; |
343 |
| - |
344 |
| - try { |
345 |
| - int chunkSize = encoding.equalsIgnoreCase("base64") ? 4095 : 4096; |
346 |
| - if (bufferSize > 0) |
347 |
| - chunkSize = bufferSize; |
348 |
| - |
349 |
| - InputStream fs; |
350 |
| - |
351 |
| - if (resolved != null && path.startsWith(ReactNativeBlobUtilConst.FILE_PREFIX_BUNDLE_ASSET)) { |
352 |
| - fs = ReactNativeBlobUtil.RCTContext.getAssets().open(path.replace(ReactNativeBlobUtilConst.FILE_PREFIX_BUNDLE_ASSET, "")); |
353 |
| - } |
354 |
| - // fix issue 287 |
355 |
| - else if (resolved == null) { |
356 |
| - fs = ReactNativeBlobUtil.RCTContext.getContentResolver().openInputStream(Uri.parse(path)); |
357 |
| - } else { |
358 |
| - fs = new FileInputStream(new File(path)); |
359 |
| - } |
360 |
| - |
361 |
| - int cursor = 0; |
362 |
| - boolean error = false; |
363 |
| - |
364 |
| - if (encoding.equalsIgnoreCase("utf8")) { |
365 |
| - InputStreamReader isr = new InputStreamReader(fs, Charset.forName("UTF-8")); |
366 |
| - BufferedReader reader = new BufferedReader(isr, chunkSize); |
367 |
| - char[] buffer = new char[chunkSize]; |
368 |
| - // read chunks of the string |
369 |
| - while (reader.read(buffer, 0, chunkSize) != -1) { |
370 |
| - String chunk = new String(buffer); |
371 |
| - emitStreamEvent(streamId, "data", chunk); |
372 |
| - if (tick > 0) |
373 |
| - SystemClock.sleep(tick); |
374 |
| - } |
375 |
| - |
376 |
| - reader.close(); |
377 |
| - isr.close(); |
378 |
| - } else if (encoding.equalsIgnoreCase("ascii")) { |
379 |
| - byte[] buffer = new byte[chunkSize]; |
380 |
| - while ((cursor = fs.read(buffer)) != -1) { |
381 |
| - WritableArray chunk = Arguments.createArray(); |
382 |
| - for (int i = 0; i < cursor; i++) { |
383 |
| - chunk.pushInt((int) buffer[i]); |
384 |
| - } |
385 |
| - emitStreamEvent(streamId, "data", chunk); |
386 |
| - if (tick > 0) |
387 |
| - SystemClock.sleep(tick); |
388 |
| - } |
389 |
| - } else if (encoding.equalsIgnoreCase("base64")) { |
390 |
| - byte[] buffer = new byte[chunkSize]; |
391 |
| - while ((cursor = fs.read(buffer)) != -1) { |
392 |
| - if (cursor < chunkSize) { |
393 |
| - byte[] copy = new byte[cursor]; |
394 |
| - System.arraycopy(buffer, 0, copy, 0, cursor); |
395 |
| - emitStreamEvent(streamId, "data", Base64.encodeToString(copy, Base64.NO_WRAP)); |
396 |
| - } else |
397 |
| - emitStreamEvent(streamId, "data", Base64.encodeToString(buffer, Base64.NO_WRAP)); |
398 |
| - if (tick > 0) |
399 |
| - SystemClock.sleep(tick); |
400 |
| - } |
401 |
| - } else { |
402 |
| - emitStreamEvent( |
403 |
| - streamId, |
404 |
| - "error", |
405 |
| - "EINVAL", |
406 |
| - "Unrecognized encoding `" + encoding + "`, should be one of `base64`, `utf8`, `ascii`" |
407 |
| - ); |
408 |
| - error = true; |
409 |
| - } |
410 |
| - |
411 |
| - if (!error) |
412 |
| - emitStreamEvent(streamId, "end", ""); |
413 |
| - fs.close(); |
414 |
| - |
415 |
| - } catch (FileNotFoundException err) { |
416 |
| - emitStreamEvent( |
417 |
| - streamId, |
418 |
| - "error", |
419 |
| - "ENOENT", |
420 |
| - "No such file '" + path + "'" |
421 |
| - ); |
422 |
| - } catch (Exception err) { |
423 |
| - emitStreamEvent( |
424 |
| - streamId, |
425 |
| - "error", |
426 |
| - "EUNSPECIFIED", |
427 |
| - "Failed to convert data to " + encoding + " encoded string. This might be because this encoding cannot be used for this data." |
428 |
| - ); |
429 |
| - err.printStackTrace(); |
430 |
| - } |
431 |
| - } |
432 | 334 |
|
433 | 335 | /**
|
434 | 336 | * Unlink file at path
|
|
0 commit comments