(
final String filename,
final String content,
final String encoding,
final CallbackContext callback
)
| 532 | } |
| 533 | |
| 534 | private void writeText( |
| 535 | final String filename, |
| 536 | final String content, |
| 537 | final String encoding, |
| 538 | final CallbackContext callback |
| 539 | ) { |
| 540 | final Context context = this.context; |
| 541 | |
| 542 | cordova |
| 543 | .getThreadPool() |
| 544 | .execute( |
| 545 | new Runnable() { |
| 546 | public void run() { |
| 547 | try { |
| 548 | String formattedUri = formatUri(filename); |
| 549 | DocumentFile file = getFile(formattedUri); |
| 550 | if (file == null) { |
| 551 | callback.error("File not found."); |
| 552 | return; |
| 553 | } |
| 554 | if (canWrite(file.getUri())) { |
| 555 | String charSetName = encoding; |
| 556 | if (charSetName == null || charSetName.isEmpty() || "auto".equalsIgnoreCase(charSetName)) { |
| 557 | charSetName = "UTF-8"; |
| 558 | } |
| 559 | if (!Charset.isSupported(charSetName)) { |
| 560 | callback.error("Charset not supported: " + charSetName); |
| 561 | return; |
| 562 | } |
| 563 | Charset charset = Charset.forName(charSetName); |
| 564 | |
| 565 | try (OutputStream op = context |
| 566 | .getContentResolver() |
| 567 | .openOutputStream(file.getUri(), "rwt")) { |
| 568 | byte[] bytes = content.getBytes(charset); |
| 569 | op.write(bytes); |
| 570 | op.flush(); |
| 571 | } |
| 572 | callback.success("OK"); |
| 573 | } else { |
| 574 | callback.error("No write permission"); |
| 575 | } |
| 576 | } catch (Exception e) { |
| 577 | callback.error(e.toString()); |
| 578 | } |
| 579 | } |
| 580 | } |
| 581 | ); |
| 582 | } |
| 583 | |
| 584 | private void writeFile( |
| 585 | final String filename, |
no test coverage detected