Skip to content

Commit 82cb5d4

Browse files
author
Gary Gregory
committed
Refactor internals into new methods:
- Add FileUtils.newOutputStream(File, boolean). - Add PathUtils.newOutputStream(Path, boolean).
1 parent 2bbb935 commit 82cb5d4

4 files changed

Lines changed: 45 additions & 12 deletions

File tree

src/changes/changes.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@ The <action> type attribute can be add,update,fix,remove.
114114
<action dev="ggregory" type="add" due-to="Gary Gregory">
115115
Add PathUtils.readString(Path, Charset).
116116
</action>
117+
<action dev="ggregory" type="add" due-to="Gary Gregory">
118+
Add FileUtils.newOutputStream(File, boolean).
119+
</action>
120+
<action dev="ggregory" type="add" due-to="Gary Gregory">
121+
Add PathUtils.newOutputStream(Path, boolean).
122+
</action>
117123
<!-- UPDATE -->
118124
<action dev="ggregory" type="update" due-to="Dependabot">
119125
Bump Maven Javadoc plugin from 3.2.0 to 3.3.0.

src/main/java/org/apache/commons/io/FileUtils.java

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2364,6 +2364,20 @@ public static void moveToDirectory(final File src, final File destDir, final boo
23642364
}
23652365
}
23662366

2367+
/**
2368+
* Creates a new OutputStream by opening or creating a file, returning an output stream that may be used to write bytes
2369+
* to the file.
2370+
*
2371+
* @param append Whether or not to append.
2372+
* @param file the File.
2373+
* @return a new OutputStream.
2374+
* @throws IOException if an I/O error occurs.
2375+
* @since 2.12.0
2376+
*/
2377+
public static OutputStream newOutputStream(final File file, final boolean append) throws IOException {
2378+
return PathUtils.newOutputStream(file.toPath(), append);
2379+
}
2380+
23672381
/**
23682382
* Opens a {@link FileInputStream} for the specified file, providing better error messages than simply calling
23692383
* {@code new FileInputStream(file)}.
@@ -2581,14 +2595,14 @@ public static List<String> readLines(final File file, final String charsetName)
25812595
return readLines(file, Charsets.toCharset(charsetName));
25822596
}
25832597

2598+
25842599
private static void requireAbsent(final File file, final String name) throws FileExistsException {
25852600
if (file.exists()) {
25862601
throw new FileExistsException(
25872602
String.format("File element in parameter '%s' already exists: '%s'", name, file));
25882603
}
25892604
}
25902605

2591-
25922606
/**
25932607
* Throws IllegalArgumentException if the given files' canonical representations are equal.
25942608
*
@@ -3206,6 +3220,8 @@ public static void write(final File file, final CharSequence data, final Charset
32063220
write(file, data, charset, false);
32073221
}
32083222

3223+
// Private method, must be invoked will a directory parameter
3224+
32093225
/**
32103226
* Writes a CharSequence to a file creating the file if it does not exist.
32113227
*
@@ -3222,8 +3238,6 @@ public static void write(final File file, final CharSequence data, final Charset
32223238
writeStringToFile(file, Objects.toString(data, null), charset, append);
32233239
}
32243240

3225-
// Private method, must be invoked will a directory parameter
3226-
32273241
/**
32283242
* Writes a CharSequence to a file creating the file if it does not exist.
32293243
*
@@ -3256,6 +3270,8 @@ public static void write(final File file, final CharSequence data, final String
32563270
write(file, data, Charsets.toCharset(charsetName), append);
32573271
}
32583272

3273+
// Must be called with a directory
3274+
32593275
/**
32603276
* Writes a byte array to a file creating the file if it does not exist.
32613277
* <p>
@@ -3272,8 +3288,6 @@ public static void writeByteArrayToFile(final File file, final byte[] data) thro
32723288
writeByteArrayToFile(file, data, false);
32733289
}
32743290

3275-
// Must be called with a directory
3276-
32773291
/**
32783292
* Writes a byte array to a file creating the file if it does not exist.
32793293
*
@@ -3357,6 +3371,7 @@ public static void writeLines(final File file, final Collection<?> lines, final
33573371
writeLines(file, null, lines, null, append);
33583372
}
33593373

3374+
33603375
/**
33613376
* Writes the {@code toString()} value of each item in a collection to
33623377
* the specified {@code File} line by line.
@@ -3373,7 +3388,6 @@ public static void writeLines(final File file, final Collection<?> lines, final
33733388
writeLines(file, null, lines, lineEnding, false);
33743389
}
33753390

3376-
33773391
/**
33783392
* Writes the {@code toString()} value of each item in a collection to
33793393
* the specified {@code File} line by line.

src/main/java/org/apache/commons/io/file/PathUtils.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.io.File;
2121
import java.io.IOException;
2222
import java.io.InputStream;
23+
import java.io.OutputStream;
2324
import java.io.UncheckedIOException;
2425
import java.net.URI;
2526
import java.net.URL;
@@ -36,6 +37,7 @@
3637
import java.nio.file.OpenOption;
3738
import java.nio.file.Path;
3839
import java.nio.file.Paths;
40+
import java.nio.file.StandardOpenOption;
3941
import java.nio.file.attribute.AclEntry;
4042
import java.nio.file.attribute.AclFileAttributeView;
4143
import java.nio.file.attribute.BasicFileAttributes;
@@ -833,6 +835,22 @@ public static DirectoryStream<Path> newDirectoryStream(final Path dir, final Pat
833835
return Files.newDirectoryStream(dir, new DirectoryStreamFilter(pathFilter));
834836
}
835837

838+
/**
839+
* Creates a new OutputStream by opening or creating a file, returning an output stream that may be used to write bytes
840+
* to the file.
841+
* @param path the Path.
842+
* @param append Whether or not to append.
843+
*
844+
* @return a new OutputStream.
845+
* @throws IOException if an I/O error occurs.
846+
* @since 2.12.0
847+
*/
848+
public static OutputStream newOutputStream(final Path path, final boolean append) throws IOException {
849+
return Files.newOutputStream(path, append ?
850+
new OpenOption[] {StandardOpenOption.APPEND} :
851+
new OpenOption[] {StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING});
852+
}
853+
836854
/**
837855
* Returns true if the given options contain {@link StandardDeleteOption#OVERRIDE_READ_ONLY}.
838856
*

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@
2323
import java.io.Writer;
2424
import java.nio.charset.Charset;
2525
import java.nio.charset.CharsetEncoder;
26-
import java.nio.file.Files;
27-
import java.nio.file.OpenOption;
28-
import java.nio.file.StandardOpenOption;
2926
import java.util.Objects;
3027

3128
import org.apache.commons.io.Charsets;
@@ -67,9 +64,7 @@ private static Writer initWriter(final File file, final Object encoding, final b
6764
OutputStream stream = null;
6865
final boolean fileExistedAlready = file.exists();
6966
try {
70-
stream = Files.newOutputStream(file.toPath(), append ?
71-
new OpenOption[] {StandardOpenOption.APPEND} :
72-
new OpenOption[] {StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING});
67+
stream = FileUtils.newOutputStream(file, append);
7368
if (encoding == null || encoding instanceof Charset) {
7469
return new OutputStreamWriter(stream, Charsets.toCharset((Charset) encoding));
7570
}

0 commit comments

Comments
 (0)