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 2021/08/08 14:03:40 UTC
[commons-io] branch master updated (8a49feb -> b43c74a)
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/commons-io.git.
from 8a49feb Comment typo.
new 3ca0590 Add PathUtils.getTempDirectory().
new a8b3ae6 Remove useless parens.
new b43c74a Reimplement internals with NIO.
The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
src/changes/changes.xml | 3 ++
src/main/java/org/apache/commons/io/FileUtils.java | 2 +-
.../java/org/apache/commons/io/file/PathUtils.java | 17 ++++++++---
.../io/output/DeferredFileOutputStream.java | 33 ++++++++++++----------
.../org/apache/commons/io/file/PathUtilsTest.java | 6 ++++
5 files changed, 41 insertions(+), 20 deletions(-)
[commons-io] 02/03: Remove useless parens.
Posted by gg...@apache.org.
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-io.git
commit a8b3ae661ea2f0876d1ac1b6d2fa3500c531d1a0
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Aug 8 09:35:18 2021 -0400
Remove useless parens.
---
src/main/java/org/apache/commons/io/FileUtils.java | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/main/java/org/apache/commons/io/FileUtils.java b/src/main/java/org/apache/commons/io/FileUtils.java
index 82e2f60..409cd20 100644
--- a/src/main/java/org/apache/commons/io/FileUtils.java
+++ b/src/main/java/org/apache/commons/io/FileUtils.java
@@ -2195,7 +2195,7 @@ public class FileUtils {
* @see File#mkdirs()
*/
private static File mkdirs(final File directory) throws IOException {
- if ((directory != null) && (!directory.mkdirs() && !directory.isDirectory())) {
+ if (directory != null && !directory.mkdirs() && !directory.isDirectory()) {
throw new IOException("Cannot create directory '" + directory + "'.");
}
return directory;
[commons-io] 01/03: Add PathUtils.getTempDirectory().
Posted by gg...@apache.org.
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-io.git
commit 3ca05905e4c18d3f26ec4b3ae51167da84c4619a
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Aug 8 09:35:04 2021 -0400
Add PathUtils.getTempDirectory().
---
src/changes/changes.xml | 3 +++
src/main/java/org/apache/commons/io/file/PathUtils.java | 17 +++++++++++++----
.../java/org/apache/commons/io/file/PathUtilsTest.java | 6 ++++++
3 files changed, 22 insertions(+), 4 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index cb4af92..d15cd25 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -126,6 +126,9 @@ The <action> type attribute can be add,update,fix,remove.
<action dev="ggregory" type="add" due-to="Gary Gregory">
Add and reuse UncheckedIOExceptions.
</action>
+ <action dev="ggregory" type="add" due-to="Gary Gregory">
+ Add PathUtils.getTempDirectory().
+ </action>
<!-- UPDATE -->
<action dev="ggregory" type="update" due-to="Dependabot">
Bump Maven Javadoc plugin from 3.2.0 to 3.3.0.
diff --git a/src/main/java/org/apache/commons/io/file/PathUtils.java b/src/main/java/org/apache/commons/io/file/PathUtils.java
index f7b640c..f1075b9 100644
--- a/src/main/java/org/apache/commons/io/file/PathUtils.java
+++ b/src/main/java/org/apache/commons/io/file/PathUtils.java
@@ -60,6 +60,7 @@ import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.commons.io.Charsets;
+import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOExceptionList;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.UncheckedIOExceptions;
@@ -300,10 +301,7 @@ public final class PathUtils {
*/
public static Path createParentDirectories(final Path path, final FileAttribute<?>... attrs) throws IOException {
final Path parent = path.getParent();
- if (parent == null) {
- return null;
- }
- return Files.createDirectories(parent, attrs);
+ return parent == null ? null : Files.createDirectories(parent, attrs);
}
/**
@@ -697,6 +695,17 @@ public final class PathUtils {
}
/**
+ * Returns a {@link Path} representing the system temporary directory.
+ *
+ * @return the system temporary directory.
+ *
+ * @since 2.12.0
+ */
+ public static Path getTempDirectory() {
+ return Paths.get(FileUtils.getTempDirectoryPath());
+ }
+
+ /**
* Tests whether the specified {@code Path} is a directory or not. Implemented as a null-safe delegate to
* {@code Files.isDirectory(Path path, LinkOption... options)}.
*
diff --git a/src/test/java/org/apache/commons/io/file/PathUtilsTest.java b/src/test/java/org/apache/commons/io/file/PathUtilsTest.java
index f90b267..63f29bd 100644
--- a/src/test/java/org/apache/commons/io/file/PathUtilsTest.java
+++ b/src/test/java/org/apache/commons/io/file/PathUtilsTest.java
@@ -161,6 +161,12 @@ public class PathUtilsTest extends TestArguments {
}
@Test
+ public void testGetTempDirectory() {
+ final Path tempDirectory = Paths.get(System.getProperty("java.io.tmpdir"));
+ assertEquals(tempDirectory, PathUtils.getTempDirectory());
+ }
+
+ @Test
public void testIsDirectory() throws IOException {
assertFalse(PathUtils.isDirectory(null));
[commons-io] 03/03: Reimplement internals with NIO.
Posted by gg...@apache.org.
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-io.git
commit b43c74a3c36c4b1472936f4c6632d88c00153b91
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Aug 8 09:36:31 2021 -0400
Reimplement internals with NIO.
---
.../io/output/DeferredFileOutputStream.java | 33 ++++++++++++----------
1 file changed, 18 insertions(+), 15 deletions(-)
diff --git a/src/main/java/org/apache/commons/io/output/DeferredFileOutputStream.java b/src/main/java/org/apache/commons/io/output/DeferredFileOutputStream.java
index 990657f..143587a 100644
--- a/src/main/java/org/apache/commons/io/output/DeferredFileOutputStream.java
+++ b/src/main/java/org/apache/commons/io/output/DeferredFileOutputStream.java
@@ -21,9 +21,10 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.function.Supplier;
-import org.apache.commons.io.FileUtils;
-import org.apache.commons.io.IOUtils;
+import org.apache.commons.io.file.PathUtils;
/**
* An output stream which will retain data in memory until a specified threshold is reached, and only then commit it to
@@ -50,7 +51,7 @@ public class DeferredFileOutputStream extends ThresholdingOutputStream {
/**
* The file to which output will be directed if the threshold is exceeded.
*/
- private File outputFile;
+ private Path outputPath;
/**
* The temporary file prefix.
@@ -65,7 +66,7 @@ public class DeferredFileOutputStream extends ThresholdingOutputStream {
/**
* The directory to use for temporary files.
*/
- private final File directory;
+ private final Path directory;
/**
* True when close() has been called successfully.
@@ -96,12 +97,12 @@ public class DeferredFileOutputStream extends ThresholdingOutputStream {
* @param initialBufferSize The initial size of the in memory buffer.
*/
private DeferredFileOutputStream(final int threshold, final File outputFile, final String prefix,
- final String suffix, final File directory, final int initialBufferSize) {
+ final String suffix, final File directory, final int initialBufferSize) {
super(threshold);
- this.outputFile = outputFile;
+ this.outputPath = toPath(outputFile, null);
this.prefix = prefix;
this.suffix = suffix;
- this.directory = directory;
+ this.directory = toPath(directory, PathUtils::getTempDirectory);
memoryOutputStream = new ByteArrayOutputStream(initialBufferSize);
currentOutputStream = memoryOutputStream;
@@ -200,7 +201,7 @@ public class DeferredFileOutputStream extends ThresholdingOutputStream {
* @return The file for this output stream, or {@code null} if no such file exists.
*/
public File getFile() {
- return outputFile;
+ return outputPath != null ? outputPath.toFile() : null;
}
/**
@@ -235,10 +236,10 @@ public class DeferredFileOutputStream extends ThresholdingOutputStream {
@Override
protected void thresholdReached() throws IOException {
if (prefix != null) {
- outputFile = File.createTempFile(prefix, suffix, directory);
+ outputPath = Files.createTempFile(directory, prefix, suffix);
}
- FileUtils.forceMkdirParent(outputFile);
- final OutputStream fos = Files.newOutputStream(outputFile.toPath());
+ PathUtils.createParentDirectories(outputPath);
+ final OutputStream fos = Files.newOutputStream(outputPath);
try {
memoryOutputStream.writeTo(fos);
} catch (final IOException e) {
@@ -274,7 +275,11 @@ public class DeferredFileOutputStream extends ThresholdingOutputStream {
if (isInMemory()) {
return memoryOutputStream.toInputStream();
}
- return Files.newInputStream(outputFile.toPath());
+ return Files.newInputStream(outputPath);
+ }
+
+ private Path toPath(final File file, final Supplier<Path> defaultPathSupplier) {
+ return file != null ? file.toPath() : defaultPathSupplier == null ? null : defaultPathSupplier.get();
}
/**
@@ -295,9 +300,7 @@ public class DeferredFileOutputStream extends ThresholdingOutputStream {
if (isInMemory()) {
memoryOutputStream.writeTo(outputStream);
} else {
- try (InputStream fis = Files.newInputStream(outputFile.toPath())) {
- IOUtils.copy(fis, outputStream);
- }
+ Files.copy(outputPath, outputStream);
}
}
}