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

rmaucher pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tomcat-jakartaee-migration.git


The following commit(s) were added to refs/heads/main by this push:
     new ffc263f  Refactor stream writeTo inside close
ffc263f is described below

commit ffc263f8902e046c43878f97caa6af4295c888dd
Author: remm <[email protected]>
AuthorDate: Mon Jun 8 17:01:23 2026 +0200

    Refactor stream writeTo inside close
---
 .../org/apache/tomcat/jakartaee/Migration.java     | 51 +++++++++++-----------
 .../apache/tomcat/jakartaee/MigrationCache.java    |  2 +-
 2 files changed, 27 insertions(+), 26 deletions(-)

diff --git a/src/main/java/org/apache/tomcat/jakartaee/Migration.java 
b/src/main/java/org/apache/tomcat/jakartaee/Migration.java
index ac5ab45..efb022a 100644
--- a/src/main/java/org/apache/tomcat/jakartaee/Migration.java
+++ b/src/main/java/org/apache/tomcat/jakartaee/Migration.java
@@ -363,7 +363,7 @@ public class Migration {
                 }
                 String destName = profile.convert(srcName);
                 if (srcZipEntry.getMethod() == ZipEntry.STORED) {
-                    try (CrcSizeTrackingOutputStream trackingStream = new 
CrcSizeTrackingOutputStream()) {
+                    try (CrcSizeTrackingOutputStream trackingStream = new 
CrcSizeTrackingOutputStream(destZipStream)) {
                         convertedStream = migrateStream(srcName, srcZipStream, 
trackingStream);
                         MigrationZipArchiveEntry destZipEntry = new 
MigrationZipArchiveEntry(srcZipEntry);
                         destZipEntry.setName(destName);
@@ -373,9 +373,8 @@ public class Migration {
                             
destZipEntry.setLastModifiedTime(FileTime.fromMillis(System.currentTimeMillis()));
                         }
                         destZipStream.putArchiveEntry(destZipEntry);
-                        trackingStream.writeTo(destZipStream);
-                        destZipStream.closeArchiveEntry();
                     }
+                    destZipStream.closeArchiveEntry();
                 } else {
                     MigrationZipArchiveEntry destZipEntry = new 
MigrationZipArchiveEntry(srcZipEntry);
                     destZipEntry.setName(destName);
@@ -567,10 +566,20 @@ public class Migration {
 
         private final CRC32 crc = new CRC32();
         private long size;
+        private final OutputStream destStream;
         private ByteArrayOutputStream buffer = new ByteArrayOutputStream();
         private FileOutputStream fileOutput;
         private File tempFile;
-        private volatile boolean tempFileDeleted = false;
+
+        /**
+         * Create stream that computes its bytes as CRC while streaming them
+         * to the specified stream on close.
+         * @param destStream the destination stream to write the bytes to
+         */
+        CrcSizeTrackingOutputStream(OutputStream destStream) {
+            super();
+            this.destStream = destStream;
+        }
 
         @Override
         public void write(int b) throws IOException {
@@ -615,32 +624,24 @@ public class Migration {
             return crc.getValue();
         }
 
-        public void writeTo(OutputStream out) throws IOException {
+        public void close() throws IOException {
             if (fileOutput != null) {
-                fileOutput.close();
-                fileOutput = null;
-                try (FileInputStream fis = new FileInputStream(tempFile)) {
-                    IOUtils.copy(fis, out);
-                }
-                if (tempFile != null && tempFile.exists()) {
-                    tempFile.delete();
-                    tempFileDeleted = true;
+                try {
+                    fileOutput.close();
+                } finally {
+                    try (FileInputStream fis = new FileInputStream(tempFile)) {
+                        IOUtils.copy(fis, destStream);
+                    } finally {
+                        tempFile.delete();
+                    }
                 }
             } else if (buffer != null) {
-                buffer.writeTo(out);
-            }
-        }
-
-        public void close() throws IOException {
-            if (fileOutput != null) {
-                fileOutput.close();
-                if (!tempFileDeleted && tempFile != null && tempFile.exists()) 
{
-                    tempFile.delete();
+                try {
+                    buffer.writeTo(destStream);
+                } finally {
+                    buffer.close();
                 }
             }
-            if (buffer != null) {
-                buffer.close();
-            }
         }
     }
 }
diff --git a/src/main/java/org/apache/tomcat/jakartaee/MigrationCache.java 
b/src/main/java/org/apache/tomcat/jakartaee/MigrationCache.java
index 46d8a58..b1107ef 100644
--- a/src/main/java/org/apache/tomcat/jakartaee/MigrationCache.java
+++ b/src/main/java/org/apache/tomcat/jakartaee/MigrationCache.java
@@ -296,7 +296,7 @@ public class MigrationCache {
             // Convert to hex string
             StringBuilder sb = new StringBuilder();
             for (byte b : hashBytes) {
-                sb.append(String.format("%02x", Byte.valueOf(b)));
+                sb.append(String.format("%02x", Integer.valueOf(b & 0xFF)));
             }
             return sb.toString();
         } catch (NoSuchAlgorithmException e) {


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to