You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2023/04/03 03:19:08 UTC

[commons-fileupload] 05/05: Move utility method

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

commit 280ffaca103e43bf3dbef7fe11650fbd6b6daeb0
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Apr 2 23:18:58 2023 -0400

    Move utility method
---
 .../commons/fileupload2/disk/DiskFileItem.java     | 38 +++++++++++--
 .../fileupload2/impl/FileItemStreamImpl.java       |  4 +-
 .../apache/commons/fileupload2/util/Streams.java   | 65 ----------------------
 3 files changed, 36 insertions(+), 71 deletions(-)

diff --git a/src/main/java/org/apache/commons/fileupload2/disk/DiskFileItem.java b/src/main/java/org/apache/commons/fileupload2/disk/DiskFileItem.java
index 87e18c0..cf266ce 100644
--- a/src/main/java/org/apache/commons/fileupload2/disk/DiskFileItem.java
+++ b/src/main/java/org/apache/commons/fileupload2/disk/DiskFileItem.java
@@ -34,8 +34,8 @@ import java.util.concurrent.atomic.AtomicInteger;
 import org.apache.commons.fileupload2.FileItem;
 import org.apache.commons.fileupload2.FileItemHeaders;
 import org.apache.commons.fileupload2.FileUploadException;
+import org.apache.commons.fileupload2.InvalidFileNameException;
 import org.apache.commons.fileupload2.ParameterParser;
-import org.apache.commons.fileupload2.util.Streams;
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.io.output.DeferredFileOutputStream;
@@ -69,8 +69,7 @@ import org.apache.commons.io.output.DeferredFileOutputStream;
  *
  * @since 1.1
  */
-public class DiskFileItem
-    implements FileItem {
+public class DiskFileItem implements FileItem {
 
     /**
      * Default content charset to be used when no explicit charset
@@ -339,7 +338,7 @@ public class DiskFileItem
      */
     @Override
     public String getName() {
-        return Streams.checkFileName(fileName);
+        return DiskFileItem.checkFileName(fileName);
     }
 
     /**
@@ -607,4 +606,35 @@ public class DiskFileItem
             FileUtils.moveFile(outputFile, file);
         }
     }
+
+    /**
+     * Checks, whether the given file name is valid in the sense,
+     * that it doesn't contain any NUL characters. If the file name
+     * is valid, it will be returned without any modifications. Otherwise,
+     * an {@link InvalidFileNameException} is raised.
+     *
+     * @param fileName The file name to check
+     * @return Unmodified file name, if valid.
+     * @throws InvalidFileNameException The file name was found to be invalid.
+     */
+    public static String checkFileName(final String fileName) {
+        if (fileName != null  &&  fileName.indexOf('\u0000') != -1) {
+            // pFileName.replace("\u0000", "\\0")
+            final StringBuilder sb = new StringBuilder();
+            for (int i = 0;  i < fileName.length();  i++) {
+                final char c = fileName.charAt(i);
+                switch (c) {
+                    case 0:
+                        sb.append("\\0");
+                        break;
+                    default:
+                        sb.append(c);
+                        break;
+                }
+            }
+            throw new InvalidFileNameException(fileName,
+                    "Invalid file name: " + sb);
+        }
+        return fileName;
+    }
 }
diff --git a/src/main/java/org/apache/commons/fileupload2/impl/FileItemStreamImpl.java b/src/main/java/org/apache/commons/fileupload2/impl/FileItemStreamImpl.java
index ce94b3f..6a49334 100644
--- a/src/main/java/org/apache/commons/fileupload2/impl/FileItemStreamImpl.java
+++ b/src/main/java/org/apache/commons/fileupload2/impl/FileItemStreamImpl.java
@@ -26,9 +26,9 @@ import org.apache.commons.fileupload2.FileItemStream;
 import org.apache.commons.fileupload2.FileUploadException;
 import org.apache.commons.fileupload2.InvalidFileNameException;
 import org.apache.commons.fileupload2.MultipartStream.ItemInputStream;
+import org.apache.commons.fileupload2.disk.DiskFileItem;
 import org.apache.commons.fileupload2.pub.FileUploadByteCountLimitException;
 import org.apache.commons.fileupload2.util.LimitedInputStream;
-import org.apache.commons.fileupload2.util.Streams;
 
 /**
  * Default implementation of {@link FileItemStream}.
@@ -166,7 +166,7 @@ public class FileItemStreamImpl implements FileItemStream {
      */
     @Override
     public String getName() {
-        return Streams.checkFileName(fileName);
+        return DiskFileItem.checkFileName(fileName);
     }
 
     /**
diff --git a/src/main/java/org/apache/commons/fileupload2/util/Streams.java b/src/main/java/org/apache/commons/fileupload2/util/Streams.java
deleted file mode 100644
index e29a77c..0000000
--- a/src/main/java/org/apache/commons/fileupload2/util/Streams.java
+++ /dev/null
@@ -1,65 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.commons.fileupload2.util;
-
-import org.apache.commons.fileupload2.InvalidFileNameException;
-
-/**
- * Utility class for working with streams.
- */
-public final class Streams {
-
-    /**
-     * Checks, whether the given file name is valid in the sense,
-     * that it doesn't contain any NUL characters. If the file name
-     * is valid, it will be returned without any modifications. Otherwise,
-     * an {@link InvalidFileNameException} is raised.
-     *
-     * @param fileName The file name to check
-     * @return Unmodified file name, if valid.
-     * @throws InvalidFileNameException The file name was found to be invalid.
-     */
-    public static String checkFileName(final String fileName) {
-        if (fileName != null  &&  fileName.indexOf('\u0000') != -1) {
-            // pFileName.replace("\u0000", "\\0")
-            final StringBuilder sb = new StringBuilder();
-            for (int i = 0;  i < fileName.length();  i++) {
-                final char c = fileName.charAt(i);
-                switch (c) {
-                    case 0:
-                        sb.append("\\0");
-                        break;
-                    default:
-                        sb.append(c);
-                        break;
-                }
-            }
-            throw new InvalidFileNameException(fileName,
-                    "Invalid file name: " + sb);
-        }
-        return fileName;
-    }
-
-    /**
-     * Private constructor, to prevent instantiation.
-     * This class has only static methods.
-     */
-    private Streams() {
-        // Does nothing
-    }
-
-}