android.os.ParcelFileDescriptor.AutoCloseInputStream Java Examples

The following examples show how to use android.os.ParcelFileDescriptor.AutoCloseInputStream. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WriteFileTask.java    From samba-documents-provider with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Void doInBackground(Void... args) {
  try (final AutoCloseInputStream is = new AutoCloseInputStream(mPfd);
      final SmbFile file = mClient.openFile(mUri, "w")){
    int size;
    byte[] buf = new byte[mBuffer.capacity()];
    while ((size = is.read(buf)) > 0) {
      mBuffer.put(buf, 0, size);
      file.write(mBuffer, size);
      mBuffer.clear();
    }
  } catch (IOException e) {
    Log.e(TAG, "Failed to write file.", e);

    try {
      mPfd.closeWithError(e.getMessage());
    } catch (IOException exc) {
      Log.e(TAG, "Can't even close PFD with error.", exc);
    }
  }

  return null;
}
 
Example #2
Source File: ParcelFileDescriptorUtil.java    From FairEmail with GNU General Public License v3.0 5 votes vote down vote up
public static TransferThread pipeTo(OutputStream outputStream, ParcelFileDescriptor output) {

        AutoCloseInputStream InputStream = new AutoCloseInputStream(output);
        TransferThread t = new TransferThread(InputStream, outputStream);

        t.start();
        return t;
    }
 
Example #3
Source File: ParcelFileDescriptorUtil.java    From FairEmail with GNU General Public License v3.0 5 votes vote down vote up
public static <T> DataSinkTransferThread<T> asyncPipeToDataSink(
        OpenPgpDataSink<T> dataSink, ParcelFileDescriptor output) {
    InputStream inputStream = new BufferedInputStream(new AutoCloseInputStream(output));
    DataSinkTransferThread<T> dataSinkTransferThread = new DataSinkTransferThread<>(dataSink, inputStream);
    dataSinkTransferThread.start();
    return dataSinkTransferThread;
}
 
Example #4
Source File: RunnerArgs.java    From android-test with Apache License 2.0 5 votes vote down vote up
private BufferedReader openFile(Instrumentation instr, String filePath) throws IOException {
  // If we are running as an instant app, then read the file through the shell
  // since the APK is in the targetSandboxVersion="2" with restrictive SELinux
  // policy which prevents reading from /data/local.
  final boolean isInstantApp =
      Build.VERSION.SDK_INT >= 26 && instr.getContext().getPackageManager().isInstantApp();
  return new BufferedReader(
      isInstantApp
          ? new InputStreamReader(
              new AutoCloseInputStream(
                  instr.getUiAutomation().executeShellCommand("cat " + filePath)))
          : new FileReader(new File(filePath)));
}
 
Example #5
Source File: PackageManagerShellCommand.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
private int runSnapshotProfile() throws RemoteException {
    PrintWriter pw = getOutPrintWriter();

    // Parse the arguments
    final String packageName = getNextArg();
    final boolean isBootImage = "android".equals(packageName);

    String codePath = null;
    String opt;
    while ((opt = getNextArg()) != null) {
        switch (opt) {
            case "--code-path":
                if (isBootImage) {
                    pw.write("--code-path cannot be used for the boot image.");
                    return -1;
                }
                codePath = getNextArg();
                break;
            default:
                pw.write("Unknown arg: " + opt);
                return -1;
        }
    }

    // If no code path was explicitly requested, select the base code path.
    String baseCodePath = null;
    if (!isBootImage) {
        PackageInfo packageInfo = mInterface.getPackageInfo(packageName, /* flags */ 0,
                /* userId */0);
        if (packageInfo == null) {
            pw.write("Package not found " + packageName);
            return -1;
        }
        baseCodePath = packageInfo.applicationInfo.getBaseCodePath();
        if (codePath == null) {
            codePath = baseCodePath;
        }
    }

    // Create the profile snapshot.
    final SnapshotRuntimeProfileCallback callback = new SnapshotRuntimeProfileCallback();
    // The calling package is needed to debug permission access.
    final String callingPackage = (Binder.getCallingUid() == Process.ROOT_UID)
            ? "root" : "com.android.shell";
    final int profileType = isBootImage
            ? ArtManager.PROFILE_BOOT_IMAGE : ArtManager.PROFILE_APPS;
    if (!mInterface.getArtManager().isRuntimeProfilingEnabled(profileType, callingPackage)) {
        pw.println("Error: Runtime profiling is not enabled");
        return -1;
    }
    mInterface.getArtManager().snapshotRuntimeProfile(profileType, packageName,
            codePath, callback, callingPackage);
    if (!callback.waitTillDone()) {
        pw.println("Error: callback not called");
        return callback.mErrCode;
    }

    // Copy the snapshot profile to the output profile file.
    try (InputStream inStream = new AutoCloseInputStream(callback.mProfileReadFd)) {
        final String outputFileSuffix = isBootImage || Objects.equals(baseCodePath, codePath)
                ? "" : ("-" + new File(codePath).getName());
        final String outputProfilePath =
                ART_PROFILE_SNAPSHOT_DEBUG_LOCATION + packageName + outputFileSuffix + ".prof";
        try (OutputStream outStream = new FileOutputStream(outputProfilePath)) {
            Streams.copy(inStream, outStream);
        }
        // Give read permissions to the other group.
        Os.chmod(outputProfilePath, /*mode*/ 0644 );
    } catch (IOException | ErrnoException e) {
        pw.println("Error when reading the profile fd: " + e.getMessage());
        e.printStackTrace(pw);
        return -1;
    }
    return 0;
}