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 6d07bb6070acb41504ec934ed690a0aaf86b43bb Author: Gary Gregory <[email protected]> AuthorDate: Tue Jun 6 11:58:37 2023 -0400 Switch some code from IO to NIO --- .../org/apache/commons/fileupload2/FileItem.java | 4 +- .../commons/fileupload2/disk/DiskFileItem.java | 44 +++++++++++----------- .../fileupload2/DiskFileItemSerializeTest.java | 25 ++++++++---- 3 files changed, 42 insertions(+), 31 deletions(-) diff --git a/commons-fileupload2-core/src/main/java/org/apache/commons/fileupload2/FileItem.java b/commons-fileupload2-core/src/main/java/org/apache/commons/fileupload2/FileItem.java index 9548186..d20a2cc 100644 --- a/commons-fileupload2-core/src/main/java/org/apache/commons/fileupload2/FileItem.java +++ b/commons-fileupload2-core/src/main/java/org/apache/commons/fileupload2/FileItem.java @@ -16,12 +16,12 @@ */ package org.apache.commons.fileupload2; -import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.UncheckedIOException; import java.io.UnsupportedEncodingException; +import java.nio.file.Path; /** * <p> @@ -168,6 +168,6 @@ public interface FileItem extends FileItemHeadersSupport { * @param file The {@code File} into which the uploaded item should be stored. * @throws IOException if an error occurs. */ - void write(File file) throws IOException; + void write(Path file) throws IOException; } diff --git a/commons-fileupload2-core/src/main/java/org/apache/commons/fileupload2/disk/DiskFileItem.java b/commons-fileupload2-core/src/main/java/org/apache/commons/fileupload2/disk/DiskFileItem.java index e3a13b5..020ccf9 100644 --- a/commons-fileupload2-core/src/main/java/org/apache/commons/fileupload2/disk/DiskFileItem.java +++ b/commons-fileupload2-core/src/main/java/org/apache/commons/fileupload2/disk/DiskFileItem.java @@ -17,7 +17,6 @@ package org.apache.commons.fileupload2.disk; import java.io.ByteArrayInputStream; -import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -26,8 +25,10 @@ import java.io.UnsupportedEncodingException; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.charset.UnsupportedCharsetException; +import java.nio.file.CopyOption; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.StandardCopyOption; import java.util.Map; import java.util.UUID; import java.util.concurrent.atomic.AtomicInteger; @@ -38,7 +39,6 @@ import org.apache.commons.fileupload2.FileUploadException; import org.apache.commons.fileupload2.InvalidFileNameException; import org.apache.commons.fileupload2.ParameterParser; import org.apache.commons.io.Charsets; -import org.apache.commons.io.FileUtils; import org.apache.commons.io.file.PathUtils; import org.apache.commons.io.function.Uncheck; import org.apache.commons.io.output.DeferredFileOutputStream; @@ -212,10 +212,13 @@ public class DiskFileItem implements FileItem { @Override public void delete() { cachedContent = null; - final File outputFile = getStoreLocation(); - if (outputFile != null && !isInMemory() && outputFile.exists() && !outputFile.delete()) { - final String desc = "Cannot delete " + outputFile.toString(); - throw new UncheckedIOException(desc, new IOException(desc)); + final Path outputFile = getStoreLocation(); + if (outputFile != null && !isInMemory() && Files.exists(outputFile)) { + try { + Files.delete(outputFile); + } catch (final IOException e) { + throw new UncheckedIOException(outputFile.toString(), e); + } } } @@ -356,20 +359,20 @@ public class DiskFileItem implements FileItem { } /** - * Gets the {@link java.io.File} object 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 java.io.File#renameTo(java.io.File)} to move - * the file to new location without copying the data, if the source and destination locations reside within the same logical volume. + * 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. * * @return The data file, or {@code null} if the data is stored in memory. */ - public File getStoreLocation() { + public Path getStoreLocation() { if (dfos == null) { return null; } if (isInMemory()) { return null; } - return dfos.getFile(); + return dfos.getFile().toPath(); } /** @@ -512,15 +515,15 @@ public class DiskFileItem implements FileItem { * @throws IOException if an error occurs. */ @Override - public void write(final File file) throws IOException { + public void write(final Path file) throws IOException { if (isInMemory()) { - try (OutputStream fout = Files.newOutputStream(file.toPath())) { + try (OutputStream fout = Files.newOutputStream(file)) { fout.write(get()); } catch (final IOException e) { throw new IOException("Unexpected output data", e); } } else { - final File outputFile = getStoreLocation(); + final Path outputFile = getStoreLocation(); if (outputFile == null) { /* * For whatever reason we cannot write the file to disk. @@ -528,14 +531,11 @@ public class DiskFileItem implements FileItem { throw new FileUploadException("Cannot write uploaded file to disk."); } // Save the length of the file - size = outputFile.length(); - /* - * The uploaded file is being stored on disk in a temporary location so move it to the desired file. - */ - if (file.exists() && !file.delete()) { - throw new FileUploadException("Cannot write uploaded file to disk."); - } - FileUtils.moveFile(outputFile, file); + size = Files.size(outputFile); + // + // The uploaded file is being stored on disk in a temporary location so move it to the desired file. + // + Files.move(outputFile, file, StandardCopyOption.REPLACE_EXISTING); } } } diff --git a/commons-fileupload2-core/src/test/java/org/apache/commons/fileupload2/DiskFileItemSerializeTest.java b/commons-fileupload2-core/src/test/java/org/apache/commons/fileupload2/DiskFileItemSerializeTest.java index 313a7cf..f736c02 100644 --- a/commons-fileupload2-core/src/test/java/org/apache/commons/fileupload2/DiskFileItemSerializeTest.java +++ b/commons-fileupload2-core/src/test/java/org/apache/commons/fileupload2/DiskFileItemSerializeTest.java @@ -27,12 +27,15 @@ import java.io.File; import java.io.IOException; import java.io.ObjectOutputStream; import java.io.OutputStream; +import java.nio.file.FileVisitResult; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.attribute.BasicFileAttributes; import org.apache.commons.fileupload2.disk.DiskFileItemFactory; import org.apache.commons.io.FileUtils; import org.apache.commons.io.file.PathUtils; +import org.apache.commons.io.file.SimplePathVisitor; import org.apache.commons.lang3.SerializationUtils; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; @@ -43,7 +46,9 @@ import org.junit.jupiter.api.Test; */ public class DiskFileItemSerializeTest { - // Use a private repository to catch any files left over by tests + /** + * Use a private repository to catch any files left over by tests. + */ private static final Path REPOSITORY = PathUtils.getTempDirectory().resolve("DiskFileItemRepo"); /** @@ -135,11 +140,17 @@ public class DiskFileItemSerializeTest { @AfterEach public void tearDown() throws IOException { - - for (final File file : FileUtils.listFiles(REPOSITORY.toFile(), null, true)) { - System.out.println("Found leftover file " + file); + if (Files.exists(REPOSITORY)) { + PathUtils.visitFileTree(new SimplePathVisitor() { + @Override + public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException { + System.out.println("Found leftover file " + file); + return FileVisitResult.CONTINUE; + } + + }, REPOSITORY); + PathUtils.deleteDirectory(REPOSITORY); } - PathUtils.deleteDirectory(REPOSITORY); } /** @@ -227,10 +238,10 @@ public class DiskFileItemSerializeTest { * Helper method to test writing item contents to a file. */ public void testWritingToFile(final FileItem item, final byte[] testFieldValueBytes) throws IOException { - final File temp = File.createTempFile("fileupload", null); + final Path temp = Files.createTempFile("fileupload", null); // Note that the file exists and is initially empty; // write() must be able to handle that. item.write(temp); - compareBytes("Initial", FileUtils.readFileToByteArray(temp), testFieldValueBytes); + compareBytes("Initial", Files.readAllBytes(temp), testFieldValueBytes); } }
