Fix android.os.strictmode.LeakedClosableViolation

Ensure the file opened in
  com.android.server.PersistentDataBlockService.getBlockOutputChannel
is consistently closed as required.

Test: atest PersistentDataBlockServiceTest
Bug: 296945440
Change-Id: I6b6cee1d6477c4c615733154efa9ad6af5fc96cc
diff --git a/services/core/java/com/android/server/pdb/PersistentDataBlockService.java b/services/core/java/com/android/server/pdb/PersistentDataBlockService.java
index a8cba53..f985b5b 100644
--- a/services/core/java/com/android/server/pdb/PersistentDataBlockService.java
+++ b/services/core/java/com/android/server/pdb/PersistentDataBlockService.java
@@ -355,15 +355,7 @@
     private boolean computeAndWriteDigestLocked() {
         byte[] digest = computeDigestLocked(null);
         if (digest != null) {
-            FileChannel channel;
-            try {
-                channel = getBlockOutputChannel();
-            } catch (IOException e) {
-                Slog.e(TAG, "partition not available?", e);
-                return false;
-            }
-
-            try {
+            try (FileChannel channel = getBlockOutputChannel()) {
                 ByteBuffer buf = ByteBuffer.allocate(DIGEST_SIZE_BYTES);
                 buf.put(digest);
                 buf.flip();
@@ -424,8 +416,7 @@
     @VisibleForTesting
     void formatPartitionLocked(boolean setOemUnlockEnabled) {
 
-        try {
-            FileChannel channel = getBlockOutputChannel();
+        try (FileChannel channel = getBlockOutputChannel()) {
             // Format the data selectively.
             //
             // 1. write header, set length = 0
@@ -471,8 +462,7 @@
 
     private void doSetOemUnlockEnabledLocked(boolean enabled) {
 
-        try {
-            FileChannel channel = getBlockOutputChannel();
+        try (FileChannel channel = getBlockOutputChannel()) {
 
             channel.position(getBlockDeviceSize() - 1);
 
@@ -554,14 +544,6 @@
                 return (int) -maxBlockSize;
             }
 
-            FileChannel channel;
-            try {
-                channel = getBlockOutputChannel();
-            } catch (IOException e) {
-                Slog.e(TAG, "partition not available?", e);
-               return -1;
-            }
-
             ByteBuffer headerAndData = ByteBuffer.allocate(
                                            data.length + HEADER_SIZE + DIGEST_SIZE_BYTES);
             headerAndData.put(new byte[DIGEST_SIZE_BYTES]);
@@ -574,7 +556,7 @@
                     return -1;
                 }
 
-                try {
+                try (FileChannel channel = getBlockOutputChannel()) {
                     channel.write(headerAndData);
                     channel.force(true);
                 } catch (IOException e) {
@@ -831,8 +813,7 @@
                 if (!mIsWritable) {
                     return;
                 }
-                try {
-                    FileChannel channel = getBlockOutputChannel();
+                try (FileChannel channel = getBlockOutputChannel()) {
                     channel.position(offset);
                     channel.write(dataBuffer);
                     channel.force(true);