下面列出了怎么用android.os.ParcelFileDescriptor.AutoCloseInputStream的API类实例代码及写法,或者点击链接到github查看源代码。
@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;
}
public static TransferThread pipeTo(OutputStream outputStream, ParcelFileDescriptor output) {
AutoCloseInputStream InputStream = new AutoCloseInputStream(output);
TransferThread t = new TransferThread(InputStream, outputStream);
t.start();
return t;
}
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;
}
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)));
}
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;
}