This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-compress.git


The following commit(s) were added to refs/heads/master by this push:
     new 22c373d5 Port some code from IO to NIO APIs s
22c373d5 is described below

commit 22c373d555a6994910236133a3912469ab3c2008
Author: Gary Gregory <garydgreg...@gmail.com>
AuthorDate: Fri Jan 27 14:39:27 2023 -0500

    Port some code from IO to NIO APIs
    s
---
 src/changes/changes.xml                               |  1 +
 .../compress/compressors/pack200/Pack200Utils.java    | 11 +++++------
 .../pack200/TempFileCachingStreamBridge.java          | 19 +++++++++++--------
 3 files changed, 17 insertions(+), 14 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 2f0341a0..5735ce42 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -57,6 +57,7 @@ The <action> type attribute can be add,update,fix,remove.
         If the strings contains non-ISO_8859_1 characters, unknown characters 
are displayed after decompression. 
         Use percent encoding for non ISO_8859_1 characters.
       </action>
+      <action type="fix" dev="ggregory" due-to="Jonathan Leitschuh, Gary 
Gregory">Port some code from IO to NIO APIs.</action>
       <!-- ADD -->
       <action type="add" issue="COMPRESS-614" dev="ggregory" due-to="Andre 
Brait, Gary Gregory">Use FileTime for time fields in SevenZipArchiveEntry 
#256.</action>
       <action type="add" issue="COMPRESS-621" dev="ggregory" 
due-to="Glavo">Fix calculation the offset of the first zip central directory 
entry #334.</action>
diff --git 
a/src/main/java/org/apache/commons/compress/compressors/pack200/Pack200Utils.java
 
b/src/main/java/org/apache/commons/compress/compressors/pack200/Pack200Utils.java
index 23e34fd0..5c36f6ac 100644
--- 
a/src/main/java/org/apache/commons/compress/compressors/pack200/Pack200Utils.java
+++ 
b/src/main/java/org/apache/commons/compress/compressors/pack200/Pack200Utils.java
@@ -23,6 +23,7 @@ import java.io.File;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.nio.file.Files;
+import java.nio.file.Path;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.jar.JarFile;
@@ -109,9 +110,9 @@ public class Pack200Utils {
             props = new HashMap<>();
         }
         props.put(Pack200.Packer.SEGMENT_LIMIT, "-1");
-        final File tempFile = File.createTempFile("commons-compress", 
"pack200normalize");
+        final Path tempFile = Files.createTempFile("commons-compress", 
"pack200normalize");
         try {
-            try (OutputStream fos = Files.newOutputStream(tempFile.toPath());
+            try (OutputStream fos = Files.newOutputStream(tempFile);
                  JarFile jarFile = new JarFile(from)) {
                 final Pack200.Packer packer = Pack200.newPacker();
                 packer.properties().putAll(props);
@@ -119,12 +120,10 @@ public class Pack200Utils {
             }
             final Pack200.Unpacker unpacker = Pack200.newUnpacker();
             try (JarOutputStream jos = new 
JarOutputStream(Files.newOutputStream(to.toPath()))) {
-                unpacker.unpack(tempFile, jos);
+                unpacker.unpack(tempFile.toFile(), jos);
             }
         } finally {
-            if (!tempFile.delete()) {
-                tempFile.deleteOnExit();
-            }
+            Files.delete(tempFile);
         }
     }
 
diff --git 
a/src/main/java/org/apache/commons/compress/compressors/pack200/TempFileCachingStreamBridge.java
 
b/src/main/java/org/apache/commons/compress/compressors/pack200/TempFileCachingStreamBridge.java
index 57390d8b..4822921f 100644
--- 
a/src/main/java/org/apache/commons/compress/compressors/pack200/TempFileCachingStreamBridge.java
+++ 
b/src/main/java/org/apache/commons/compress/compressors/pack200/TempFileCachingStreamBridge.java
@@ -19,11 +19,11 @@
 
 package org.apache.commons.compress.compressors.pack200;
 
-import java.io.File;
 import java.io.FilterInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.nio.file.Files;
+import java.nio.file.Path;
 
 /**
  * StreamBridge that caches all data written to the output side in
@@ -31,25 +31,28 @@ import java.nio.file.Files;
  * @since 1.3
  */
 class TempFileCachingStreamBridge extends StreamBridge {
-    private final File f;
+    private final Path f;
 
     TempFileCachingStreamBridge() throws IOException {
-        f = File.createTempFile("commons-compress", "packtemp");
-        f.deleteOnExit();
-        out = Files.newOutputStream(f.toPath());
+        f = Files.createTempFile("commons-compress", "packtemp");
+        f.toFile().deleteOnExit();
+        out = Files.newOutputStream(f);
     }
 
     @Override
     InputStream getInputView() throws IOException {
         out.close();
-        return new FilterInputStream(Files.newInputStream(f.toPath())) {
+        return new FilterInputStream(Files.newInputStream(f)) {
             @Override
             public void close() throws IOException {
                 try {
                     super.close();
                 } finally {
-                    // if this fails the only thing we can do is to rely on 
deleteOnExit
-                    f.delete(); // NOSONAR
+                    try {
+                        Files.deleteIfExists(f);
+                    } catch (IOException ignore) {
+                        // if this fails the only thing we can do is to rely 
on deleteOnExit
+                    }
                 }
             }
         };

Reply via email to