Use try-with-resources mechanism for handling clipboard streams
Bug: 349702327
Test: m
Change-Id: Ia9ebee4b0323774612bfa2666f80033d7581662b
diff --git a/vmlauncher_app/java/com/android/virtualization/vmlauncher/MainActivity.java b/vmlauncher_app/java/com/android/virtualization/vmlauncher/MainActivity.java
index 7747c3e..dd0766b 100644
--- a/vmlauncher_app/java/com/android/virtualization/vmlauncher/MainActivity.java
+++ b/vmlauncher_app/java/com/android/virtualization/vmlauncher/MainActivity.java
@@ -509,7 +509,6 @@
private boolean writeClipboardToVm() {
ClipboardManager clipboardManager = getClipboardManager();
-
if (!clipboardManager.hasPrimaryClip()) {
Log.d(TAG, "host device has no clipboard data");
return true;
@@ -519,14 +518,12 @@
ByteBuffer header =
constructClipboardHeader(
WRITE_CLIPBOARD_TYPE_TEXT_PLAIN, text.getBytes().length + 1);
-
ParcelFileDescriptor pfd = connectClipboardSharingServer();
if (pfd == null) {
Log.d(TAG, "file descriptor of ClipboardSharingServer is null");
return false;
}
- OutputStream stream = new AutoCloseOutputStream(pfd);
- try {
+ try (OutputStream stream = new AutoCloseOutputStream(pfd)) {
stream.write(header.array());
stream.write(text.getBytes());
stream.flush();
@@ -540,14 +537,12 @@
private boolean readClipboardFromVm() {
ByteBuffer request = constructClipboardHeader(READ_CLIPBOARD_FROM_VM, 0);
-
ParcelFileDescriptor pfd = connectClipboardSharingServer();
if (pfd == null) {
Log.d(TAG, "file descriptor of ClipboardSharingServer is null");
return false;
}
- OutputStream output = new AutoCloseOutputStream(pfd);
- try {
+ try (OutputStream output = new AutoCloseOutputStream(pfd)) {
output.write(request.array());
output.flush();
Log.d(TAG, "successfully send request to the VM for reading clipboard");
@@ -556,8 +551,7 @@
return false;
}
- InputStream input = new AutoCloseInputStream(pfd);
- try {
+ try (InputStream input = new AutoCloseInputStream(pfd)) {
ByteBuffer header = ByteBuffer.wrap(input.readNBytes(8));
header.order(ByteOrder.LITTLE_ENDIAN);
switch (header.get(0)) {