Skip to content

Commit b43c74a

Browse files
committed
Reimplement internals with NIO.
1 parent a8b3ae6 commit b43c74a

1 file changed

Lines changed: 18 additions & 15 deletions

File tree

src/main/java/org/apache/commons/io/output/DeferredFileOutputStream.java

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@
2121
import java.io.InputStream;
2222
import java.io.OutputStream;
2323
import java.nio.file.Files;
24+
import java.nio.file.Path;
25+
import java.util.function.Supplier;
2426

25-
import org.apache.commons.io.FileUtils;
26-
import org.apache.commons.io.IOUtils;
27+
import org.apache.commons.io.file.PathUtils;
2728

2829
/**
2930
* 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 {
5051
/**
5152
* The file to which output will be directed if the threshold is exceeded.
5253
*/
53-
private File outputFile;
54+
private Path outputPath;
5455

5556
/**
5657
* The temporary file prefix.
@@ -65,7 +66,7 @@ public class DeferredFileOutputStream extends ThresholdingOutputStream {
6566
/**
6667
* The directory to use for temporary files.
6768
*/
68-
private final File directory;
69+
private final Path directory;
6970

7071
/**
7172
* True when close() has been called successfully.
@@ -96,12 +97,12 @@ public DeferredFileOutputStream(final int threshold, final File outputFile) {
9697
* @param initialBufferSize The initial size of the in memory buffer.
9798
*/
9899
private DeferredFileOutputStream(final int threshold, final File outputFile, final String prefix,
99-
final String suffix, final File directory, final int initialBufferSize) {
100+
final String suffix, final File directory, final int initialBufferSize) {
100101
super(threshold);
101-
this.outputFile = outputFile;
102+
this.outputPath = toPath(outputFile, null);
102103
this.prefix = prefix;
103104
this.suffix = suffix;
104-
this.directory = directory;
105+
this.directory = toPath(directory, PathUtils::getTempDirectory);
105106

106107
memoryOutputStream = new ByteArrayOutputStream(initialBufferSize);
107108
currentOutputStream = memoryOutputStream;
@@ -200,7 +201,7 @@ public byte[] getData() {
200201
* @return The file for this output stream, or {@code null} if no such file exists.
201202
*/
202203
public File getFile() {
203-
return outputFile;
204+
return outputPath != null ? outputPath.toFile() : null;
204205
}
205206

206207
/**
@@ -235,10 +236,10 @@ public boolean isInMemory() {
235236
@Override
236237
protected void thresholdReached() throws IOException {
237238
if (prefix != null) {
238-
outputFile = File.createTempFile(prefix, suffix, directory);
239+
outputPath = Files.createTempFile(directory, prefix, suffix);
239240
}
240-
FileUtils.forceMkdirParent(outputFile);
241-
final OutputStream fos = Files.newOutputStream(outputFile.toPath());
241+
PathUtils.createParentDirectories(outputPath);
242+
final OutputStream fos = Files.newOutputStream(outputPath);
242243
try {
243244
memoryOutputStream.writeTo(fos);
244245
} catch (final IOException e) {
@@ -274,7 +275,11 @@ public InputStream toInputStream() throws IOException {
274275
if (isInMemory()) {
275276
return memoryOutputStream.toInputStream();
276277
}
277-
return Files.newInputStream(outputFile.toPath());
278+
return Files.newInputStream(outputPath);
279+
}
280+
281+
private Path toPath(final File file, final Supplier<Path> defaultPathSupplier) {
282+
return file != null ? file.toPath() : defaultPathSupplier == null ? null : defaultPathSupplier.get();
278283
}
279284

280285
/**
@@ -295,9 +300,7 @@ public void writeTo(final OutputStream outputStream) throws IOException {
295300
if (isInMemory()) {
296301
memoryOutputStream.writeTo(outputStream);
297302
} else {
298-
try (InputStream fis = Files.newInputStream(outputFile.toPath())) {
299-
IOUtils.copy(fis, outputStream);
300-
}
303+
Files.copy(outputPath, outputStream);
301304
}
302305
}
303306
}

0 commit comments

Comments
 (0)