@WorkerThread
  @Override
  public Unit call()
      throws IOException,
          StreamNotFoundException,
          ShellNotRunningException,
          IllegalArgumentException {
    OutputStream outputStream;
    File destFile = null;

    switch (fileAbstraction.scheme) {
      case CONTENT:
        Objects.requireNonNull(fileAbstraction.uri);
        if (fileAbstraction.uri.getAuthority().equals(context.get().getPackageName())) {
          DocumentFile documentFile =
              DocumentFile.fromSingleUri(AppConfig.getInstance(), fileAbstraction.uri);
          if (documentFile != null && documentFile.exists() && documentFile.canWrite()) {
            outputStream = contentResolver.openOutputStream(fileAbstraction.uri, "wt");
          } else {
            destFile = FileUtils.fromContentUri(fileAbstraction.uri);
            outputStream = openFile(destFile, context.get());
          }
        } else {
          outputStream = contentResolver.openOutputStream(fileAbstraction.uri, "wt");
        }
        break;
      case FILE:
        final HybridFileParcelable hybridFileParcelable = fileAbstraction.hybridFileParcelable;
        Objects.requireNonNull(hybridFileParcelable);

        Context context = this.context.get();
        if (context == null) {
          return null;
        }
        outputStream = openFile(hybridFileParcelable.getFile(), context);
        destFile = fileAbstraction.hybridFileParcelable.getFile();
        break;
      default:
        throw new IllegalArgumentException(
            "The scheme for '" + fileAbstraction.scheme + "' cannot be processed!");
    }

    Objects.requireNonNull(outputStream);

    outputStream.write(dataToSave.getBytes());
    outputStream.close();

    if (cachedFile != null && cachedFile.exists() && destFile != null) {
      // cat cache content to original file and delete cache file
      ConcatenateFileCommand.INSTANCE.concatenateFile(cachedFile.getPath(), destFile.getPath());
      cachedFile.delete();
    }
    return Unit.INSTANCE;
  }
@Test
  @Config(shadows = {BlockAllOutputStreamsFileUtil.class, BypassMountPartitionRootUtils.class})
  public void testWriteFileRoot()
      throws IOException, StreamNotFoundException, ShellNotRunningException {
    File file = new File(Environment.getExternalStorageDirectory(), "test.txt");
    File cacheFile = File.createTempFile("test.txt", "cache");
    cacheFile.deleteOnExit();
    Uri uri = Uri.fromFile(file);
    Context ctx = ApplicationProvider.getApplicationContext();
    ContentResolver cr = ctx.getContentResolver();
    WriteTextFileCallable task =
        new WriteTextFileCallable(
            ctx, cr, new EditableFileAbstraction(ctx, uri), contents, cacheFile, true);
    task.call();

    String verify = IoUtils.readFully(new FileInputStream(file));
    assertEquals(contents, verify);
  }