MCPcopy Create free account
hub / github.com/Acode-Foundation/Acode / readAsText

Method readAsText

src/plugins/sdcard/src/android/SDcard.java:488–532  ·  view source on GitHub ↗
(final String filename, final String encoding, final CallbackContext callback)

Source from the content-addressed store, hash-verified

486 }
487
488 private void readAsText(final String filename, final String encoding, final CallbackContext callback) {
489 cordova
490 .getThreadPool()
491 .execute(
492 new Runnable() {
493 public void run() {
494 try {
495 String formattedUri = formatUri(filename);
496 Uri uri = Uri.parse(formattedUri);
497
498 String charSetName = encoding;
499 if (charSetName == null || charSetName.isEmpty() || "auto".equalsIgnoreCase(charSetName)) {
500 charSetName = "UTF-8";
501 }
502 if (!Charset.isSupported(charSetName)) {
503 callback.error("Charset not supported: " + charSetName);
504 return;
505 }
506 Charset charset = Charset.forName(charSetName);
507
508 InputStream is = context
509 .getContentResolver()
510 .openInputStream(uri);
511
512 if (is == null) {
513 callback.error("File not found");
514 return;
515 }
516
517 StringBuilder sb = new StringBuilder();
518 try (BufferedReader reader = new BufferedReader(new InputStreamReader(is, charset))) {
519 char[] buffer = new char[8192];
520 int charsRead;
521 while ((charsRead = reader.read(buffer)) != -1) {
522 sb.append(buffer, 0, charsRead);
523 }
524 }
525 callback.success(sb.toString());
526 } catch (Exception e) {
527 callback.error(e.toString());
528 }
529 }
530 }
531 );
532 }
533
534 private void writeText(
535 final String filename,

Callers 4

executeMethod · 0.95
readAsTextFunction · 0.80
readFileFunction · 0.80
readAssetFunction · 0.80

Calls 1

executeMethod · 0.45

Tested by

no test coverage detected