Merge "Use try-with-resources mechanism for handling clipboard streams" into main
diff --git a/vmlauncher_app/java/com/android/virtualization/vmlauncher/MainActivity.java b/vmlauncher_app/java/com/android/virtualization/vmlauncher/MainActivity.java
index 102b86e..22adac2 100644
--- a/vmlauncher_app/java/com/android/virtualization/vmlauncher/MainActivity.java
+++ b/vmlauncher_app/java/com/android/virtualization/vmlauncher/MainActivity.java
@@ -529,7 +529,6 @@
 
     private boolean writeClipboardToVm() {
         ClipboardManager clipboardManager = getClipboardManager();
-
         if (!clipboardManager.hasPrimaryClip()) {
             Log.d(TAG, "host device has no clipboard data");
             return true;
@@ -539,14 +538,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();
@@ -560,14 +557,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");
@@ -576,8 +571,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)) {