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-fileupload.git


The following commit(s) were added to refs/heads/master by this push:
     new a2669c89 Refactor: minor readability and cleanup improvements in 
DiskFileItem (#458)
a2669c89 is described below

commit a2669c89a94d4ecd293845805cbad29aafdbf4f4
Author: tPionTech <[email protected]>
AuthorDate: Sat Nov 8 21:42:48 2025 +0100

    Refactor: minor readability and cleanup improvements in DiskFileItem (#458)
    
    This commit simplifies the null character handling by replacing a 
single-case
    switch statement with an equivalent if-condition.
     It simplifies return statements.
     It removes a leftover
    auto-generated TODO comment.
    It fixes minor typos in javadoc.
    
    No functional or behavioral changes are introduced.
    Existing tests continue to pass successfully.
    
    Co-authored-by: Tim Piontek <[email protected]>
---
 .../commons/fileupload2/core/DiskFileItem.java     | 38 +++++++---------------
 1 file changed, 11 insertions(+), 27 deletions(-)

diff --git 
a/commons-fileupload2-core/src/main/java/org/apache/commons/fileupload2/core/DiskFileItem.java
 
b/commons-fileupload2-core/src/main/java/org/apache/commons/fileupload2/core/DiskFileItem.java
index b91f07e6..4202127f 100644
--- 
a/commons-fileupload2-core/src/main/java/org/apache/commons/fileupload2/core/DiskFileItem.java
+++ 
b/commons-fileupload2-core/src/main/java/org/apache/commons/fileupload2/core/DiskFileItem.java
@@ -230,13 +230,10 @@ public final class DiskFileItem implements 
FileItem<DiskFileItem> {
                 final var sb = new StringBuilder();
                 for (var i = 0; i < fileName.length(); i++) {
                     final var c = fileName.charAt(i);
-                    switch (c) {
-                    case 0:
+                    if (c == 0) {
                         sb.append("\\0");
-                        break;
-                    default:
+                    } else {
                         sb.append(c);
-                        break;
                     }
                 }
                 throw new InvalidPathException(fileName, sb.toString(), 
indexOf0);
@@ -478,13 +475,12 @@ public final class DiskFileItem implements 
FileItem<DiskFileItem> {
                 final Listener persistenceListener = new Listener() {
                     @Override
                     public void persisted(final Path pPath) {
-                        // TODO Auto-generated method stub
                         Listener.super.persisted(pPath);
                         final FileCleaningTracker fct = 
getFileCleaningTracker();
                         if (fct != null) {
                             fct.track(getPath(), this);
                         }
-                        }
+                    }
                 };
                 dos = new DeferrableOutputStream(threshold, pathSupplier, 
persistenceListener);
             } catch (final IOException ioe) {
@@ -496,16 +492,13 @@ public final class DiskFileItem implements 
FileItem<DiskFileItem> {
 
     /**
      * Gets the {@link Path} for the {@code FileItem}'s data's temporary 
location on the disk. Note that for {@code FileItem}s that have their data 
stored in
-     * memory, this method will return {@code null}. When handling large 
files, you can use {@link Files#move(Path,Path,CopyOption...)} to move the file 
to new
-     * location without copying the data, if the source and destination 
locations reside within the same logical volume.
+     * memory, this method will return {@code null}. When handling large 
files, you can use {@link Files#move(Path,Path,CopyOption...)} to move the file 
to a
+     * new location without copying the data, if the source and destination 
locations reside within the same logical volume.
      *
      * @return The data file, or {@code null} if the data is stored in memory.
      */
     public Path getPath() {
-        if (dos != null) {
-            return dos.getPath();
-        }
-        return null;
+        return dos == null ? null : dos.getPath();
     }
 
     /**
@@ -514,7 +507,7 @@ public final class DiskFileItem implements 
FileItem<DiskFileItem> {
      * This is the case, for example, if the underlying output stream has not 
yet
      * been closed.
      * @return The contents of the file as a {@link Reader}
-     * @throws UnsupportedEncodingException The chararacter set, which is
+     * @throws UnsupportedEncodingException The character set, which is
      *   specified in the files "content-type" header, is invalid.
      * @throws IOException An I/O error occurred, while the
      *   underlying {@link #getInputStream() input stream} was created.
@@ -536,10 +529,7 @@ public final class DiskFileItem implements 
FileItem<DiskFileItem> {
      */
     @Override
     public long getSize() {
-        if (dos != null) {
-            return dos.getSize();
-        }
-        return 0L;
+        return dos == null ? 0L : dos.getSize();
     }
 
     /**
@@ -549,17 +539,14 @@ public final class DiskFileItem implements 
FileItem<DiskFileItem> {
      * @throws IOException if an I/O error occurs
      * @throws OutOfMemoryError See {@link Files#readAllBytes(Path)}: If a 
string of the required size cannot be allocated,
      *   for example the file is larger than {@code 2GB}. If so, you should 
use {@link #getReader()}.
-     * @throws UnsupportedEncodingException The chararacter set, which is
+     * @throws UnsupportedEncodingException The character set, which is
      *   specified in the files "content-type" header, is invalid.
      * @deprecated Since 2.0.0, use {@link #getReader()} instead.
      */
     @Override
     public String getString() throws IOException, 
UnsupportedEncodingException, OutOfMemoryError {
         final byte[] bytes = get();
-        if (bytes == null) {
-            return null;
-        }
-        return new String(bytes, getCharset());
+        return bytes == null ? null : new String(bytes, getCharset());
     }
 
     /**
@@ -600,10 +587,7 @@ public final class DiskFileItem implements 
FileItem<DiskFileItem> {
      */
     @Override
     public boolean isInMemory() {
-        if (dos != null) {
-            return dos.isInMemory();
-        }
-        return true;
+        return dos == null || dos.isInMemory();
     }
 
     /**

Reply via email to