< prev index next >

test/hotspot/jtreg/runtime/appcds/SharedArchiveConsistency.java

Print this page

@@ -243,15 +243,14 @@
     }
 
     // Copy file with bytes deleted or inserted
     // del -- true, deleted, false, inserted
     public static void copyFile(File from, File to, boolean del) throws Exception {
-        FileChannel inputChannel = null;
-        FileChannel outputChannel = null;
-        try {
-            inputChannel = new FileInputStream(from).getChannel();
-            outputChannel = new FileOutputStream(to).getChannel();
+        try (
+            FileChannel inputChannel = new FileInputStream(from).getChannel();
+            FileChannel outputChannel = new FileOutputStream(to).getChannel()
+        ) {
             long size = inputChannel.size();
             int init_size = getFileHeaderSize(inputChannel);
             outputChannel.transferFrom(inputChannel, 0, init_size);
             int n = (int)getRandomBetween(0, 1024);
             if (del) {

@@ -262,13 +261,10 @@
                 System.out.println("Insert " + n + " bytes at data start section");
                 outputChannel.position(init_size);
                 outputChannel.write(ByteBuffer.wrap(new byte[n]));
                 outputChannel.transferFrom(inputChannel, init_size + n , size - init_size);
             }
-        } finally {
-            inputChannel.close();
-            outputChannel.close();
         }
     }
 
     public static void restoreJsaFile() throws Exception {
         Files.copy(orgJsaFile.toPath(), jsa.toPath(), REPLACE_EXISTING);
< prev index next >