Skip to content

Commit 166dea3

Browse files
author
Gary Gregory
committed
Add and reuse IOConsumer.forEach(*) and forEachIndexed(*).
Use forEach and streams.
1 parent cb6f3aa commit 166dea3

16 files changed

Lines changed: 166 additions & 139 deletions

src/changes/changes.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ The <action> type attribute can be add,update,fix,remove.
357357
Add IOBiConsumer.
358358
</action>
359359
<action dev="ggregory" type="add" due-to="Gary Gregory">
360-
Add and reuse IOConsumer.forEach(T[], IOConsumer) and forEachIndexed(Stream, IOConsumer).
360+
Add and reuse IOConsumer.forEach(*) and forEachIndexed(*).
361361
</action>
362362
<action dev="ggregory" type="add" due-to="Gary Gregory">
363363
Add CharsetEncoders.

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

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@
2323
import java.io.OutputStream;
2424
import java.nio.charset.Charset;
2525
import java.time.Duration;
26-
import java.util.ArrayList;
2726
import java.util.Arrays;
2827
import java.util.List;
2928
import java.util.Locale;
3029
import java.util.Objects;
3130
import java.util.StringTokenizer;
31+
import java.util.stream.Collectors;
3232

3333
/**
3434
* General File System utilities.
@@ -487,7 +487,7 @@ List<String> performCommand(final String[] cmdAttribs, final int max, final Dura
487487
// however, it's still not perfect as the JDK support is so poor
488488
// (see commons-exec or Ant for a better multithreaded multi-os solution)
489489

490-
final List<String> lines = new ArrayList<>(20);
490+
final List<String> lines;
491491
Process proc = null;
492492
InputStream in = null;
493493
OutputStream out = null;
@@ -503,28 +503,20 @@ List<String> performCommand(final String[] cmdAttribs, final int max, final Dura
503503
err = proc.getErrorStream();
504504
// default charset is most likely appropriate here
505505
inr = new BufferedReader(new InputStreamReader(in, Charset.defaultCharset()));
506-
String line = inr.readLine();
507-
while (line != null && lines.size() < max) {
508-
line = line.toLowerCase(Locale.ENGLISH).trim();
509-
lines.add(line);
510-
line = inr.readLine();
511-
}
506+
507+
lines = inr.lines().limit(max).map(line -> line.toLowerCase(Locale.ENGLISH).trim()).collect(Collectors.toList());
512508

513509
proc.waitFor();
514510

515511
ThreadMonitor.stop(monitor);
516512

517513
if (proc.exitValue() != 0) {
518514
// OS command problem, throw exception
519-
throw new IOException(
520-
"Command line returned OS error code '" + proc.exitValue() +
521-
"' for command " + Arrays.asList(cmdAttribs));
515+
throw new IOException("Command line returned OS error code '" + proc.exitValue() + "' for command " + Arrays.asList(cmdAttribs));
522516
}
523517
if (lines.isEmpty()) {
524518
// unknown problem, throw exception
525-
throw new IOException(
526-
"Command line did not return any info " +
527-
"for command " + Arrays.asList(cmdAttribs));
519+
throw new IOException("Command line did not return any info " + "for command " + Arrays.asList(cmdAttribs));
528520
}
529521

530522
inr.close();

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.List;
2626
import java.util.regex.Matcher;
2727
import java.util.regex.Pattern;
28+
import java.util.stream.Stream;
2829

2930
/**
3031
* General file name and file path manipulation utilities.
@@ -1054,12 +1055,7 @@ public static boolean isExtension(final String fileName, final String... extensi
10541055
return indexOfExtension(fileName) == NOT_FOUND;
10551056
}
10561057
final String fileExt = getExtension(fileName);
1057-
for (final String extension : extensions) {
1058-
if (fileExt.equals(extension)) {
1059-
return true;
1060-
}
1061-
}
1062-
return false;
1058+
return Stream.of(extensions).anyMatch(fileExt::equals);
10631059
}
10641060

10651061
/**

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

Lines changed: 77 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.io.OutputStream;
3232
import java.io.OutputStreamWriter;
3333
import java.io.Reader;
34+
import java.io.UncheckedIOException;
3435
import java.io.Writer;
3536
import java.net.HttpURLConnection;
3637
import java.net.ServerSocket;
@@ -45,12 +46,13 @@
4546
import java.nio.channels.Selector;
4647
import java.nio.charset.Charset;
4748
import java.nio.file.Files;
48-
import java.util.ArrayList;
4949
import java.util.Arrays;
5050
import java.util.Collection;
51+
import java.util.Iterator;
5152
import java.util.List;
5253
import java.util.Objects;
5354
import java.util.function.Consumer;
55+
import java.util.stream.Collectors;
5456
import java.util.stream.Stream;
5557

5658
import org.apache.commons.io.function.IOConsumer;
@@ -428,6 +430,15 @@ public static void close(final URLConnection conn) {
428430
}
429431
}
430432

433+
/**
434+
* Avoids the need to type cast.
435+
*
436+
* @param closeable the object to close, may be null
437+
*/
438+
private static void closeQ(final Closeable closeable) {
439+
closeQuietly(closeable, null);
440+
}
441+
431442
/**
432443
* Closes a {@link Closeable} unconditionally.
433444
*
@@ -473,15 +484,6 @@ public static void closeQuietly(final Closeable closeable) {
473484
closeQuietly(closeable, null);
474485
}
475486

476-
/**
477-
* Avoids the need to type cast.
478-
*
479-
* @param closeable the object to close, may be null
480-
*/
481-
private static void closeQ(final Closeable closeable) {
482-
closeQuietly(closeable, null);
483-
}
484-
485487
/**
486488
* Closes a {@link Closeable} unconditionally.
487489
* <p>
@@ -602,22 +604,6 @@ public static void closeQuietly(final Iterable<Closeable> closeables) {
602604
}
603605
}
604606

605-
/**
606-
* Closes a stream of {@link Closeable} unconditionally.
607-
* <p>
608-
* Equivalent calling {@link Closeable#close()} on each element, except any exceptions will be ignored.
609-
* </p>
610-
*
611-
* @param closeables the objects to close, may be null or already closed
612-
* @see #closeQuietly(Closeable)
613-
* @since 2.12.0
614-
*/
615-
public static void closeQuietly(final Stream<Closeable> closeables) {
616-
if (closeables != null) {
617-
closeables.forEach(IOUtils::closeQuietly);
618-
}
619-
}
620-
621607
/**
622608
* Closes an {@link OutputStream} unconditionally.
623609
* <p>
@@ -784,6 +770,22 @@ public static void closeQuietly(final Socket socket) {
784770
closeQ(socket);
785771
}
786772

773+
/**
774+
* Closes a stream of {@link Closeable} unconditionally.
775+
* <p>
776+
* Equivalent calling {@link Closeable#close()} on each element, except any exceptions will be ignored.
777+
* </p>
778+
*
779+
* @param closeables the objects to close, may be null or already closed
780+
* @see #closeQuietly(Closeable)
781+
* @since 2.12.0
782+
*/
783+
public static void closeQuietly(final Stream<Closeable> closeables) {
784+
if (closeables != null) {
785+
closeables.forEach(IOUtils::closeQuietly);
786+
}
787+
}
788+
787789
/**
788790
* Closes an {@link Writer} unconditionally.
789791
* <p>
@@ -895,6 +897,19 @@ public static boolean contentEquals(final InputStream input1, final InputStream
895897
}
896898
}
897899

900+
// TODO Consider making public
901+
private static boolean contentEquals(final Iterator<?> iterator1, final Iterator<?> iterator2) {
902+
while (iterator1.hasNext()) {
903+
if (!iterator2.hasNext()) {
904+
return false;
905+
}
906+
if (!Objects.equals(iterator1.next(), iterator2.next())) {
907+
return false;
908+
}
909+
}
910+
return !iterator2.hasNext();
911+
}
912+
898913
/**
899914
* Compares the contents of two Readers to determine if they are equal or not.
900915
* <p>
@@ -953,6 +968,28 @@ public static boolean contentEquals(final Reader input1, final Reader input2) th
953968
}
954969
}
955970

971+
// TODO Consider making public
972+
private static boolean contentEquals(final Stream<?> stream1, final Stream<?> stream2) {
973+
if (stream1 == stream2) {
974+
return true;
975+
}
976+
if (stream1 == null || stream2 == null) {
977+
return false;
978+
}
979+
return contentEquals(stream1.iterator(), stream2.iterator());
980+
}
981+
982+
// TODO Consider making public
983+
private static boolean contentEqualsIgnoreEOL(final BufferedReader reader1, final BufferedReader reader2) {
984+
if (reader1 == reader2) {
985+
return true;
986+
}
987+
if (reader1 == null || reader2 == null) {
988+
return false;
989+
}
990+
return contentEquals(reader1.lines(), reader2.lines());
991+
}
992+
956993
/**
957994
* Compares the contents of two Readers to determine if they are equal or
958995
* not, ignoring EOL characters.
@@ -965,28 +1002,18 @@ public static boolean contentEquals(final Reader input1, final Reader input2) th
9651002
* @param reader2 the second reader
9661003
* @return true if the content of the readers are equal (ignoring EOL differences), false otherwise
9671004
* @throws NullPointerException if either input is null
968-
* @throws IOException if an I/O error occurs
1005+
* @throws UncheckedIOException if an I/O error occurs
9691006
* @since 2.2
9701007
*/
9711008
@SuppressWarnings("resource")
972-
public static boolean contentEqualsIgnoreEOL(final Reader reader1, final Reader reader2)
973-
throws IOException {
1009+
public static boolean contentEqualsIgnoreEOL(final Reader reader1, final Reader reader2) throws UncheckedIOException {
9741010
if (reader1 == reader2) {
9751011
return true;
9761012
}
977-
if (reader1 == null ^ reader2 == null) {
1013+
if (reader1 == null || reader2 == null) {
9781014
return false;
9791015
}
980-
final BufferedReader br1 = toBufferedReader(reader1);
981-
final BufferedReader br2 = toBufferedReader(reader2);
982-
983-
String line1 = br1.readLine();
984-
String line2 = br2.readLine();
985-
while (line1 != null && line1.equals(line2)) {
986-
line1 = br1.readLine();
987-
line2 = br2.readLine();
988-
}
989-
return Objects.equals(line1, line2);
1016+
return contentEqualsIgnoreEOL(toBufferedReader(reader1), toBufferedReader(reader2));
9901017
}
9911018

9921019
/**
@@ -2042,12 +2069,12 @@ public static void readFully(final Reader reader, final char[] buffer, final int
20422069
* @param input the {@link InputStream} to read from, not null
20432070
* @return the list of Strings, never null
20442071
* @throws NullPointerException if the input is null
2045-
* @throws IOException if an I/O error occurs
2072+
* @throws UncheckedIOException if an I/O error occurs
20462073
* @since 1.1
20472074
* @deprecated 2.5 use {@link #readLines(InputStream, Charset)} instead
20482075
*/
20492076
@Deprecated
2050-
public static List<String> readLines(final InputStream input) throws IOException {
2077+
public static List<String> readLines(final InputStream input) throws UncheckedIOException {
20512078
return readLines(input, Charset.defaultCharset());
20522079
}
20532080

@@ -2063,12 +2090,11 @@ public static List<String> readLines(final InputStream input) throws IOException
20632090
* @param charset the charset to use, null means platform default
20642091
* @return the list of Strings, never null
20652092
* @throws NullPointerException if the input is null
2066-
* @throws IOException if an I/O error occurs
2093+
* @throws UncheckedIOException if an I/O error occurs
20672094
* @since 2.3
20682095
*/
2069-
public static List<String> readLines(final InputStream input, final Charset charset) throws IOException {
2070-
final InputStreamReader reader = new InputStreamReader(input, Charsets.toCharset(charset));
2071-
return readLines(reader);
2096+
public static List<String> readLines(final InputStream input, final Charset charset) throws UncheckedIOException {
2097+
return readLines(new InputStreamReader(input, Charsets.toCharset(charset)));
20722098
}
20732099

20742100
/**
@@ -2087,13 +2113,13 @@ public static List<String> readLines(final InputStream input, final Charset char
20872113
* @param charsetName the name of the requested charset, null means platform default
20882114
* @return the list of Strings, never null
20892115
* @throws NullPointerException if the input is null
2090-
* @throws IOException if an I/O error occurs
2116+
* @throws UncheckedIOException if an I/O error occurs
20912117
* @throws java.nio.charset.UnsupportedCharsetException thrown instead of {@link java.io
20922118
* .UnsupportedEncodingException} in version 2.2 if the
20932119
* encoding is not supported.
20942120
* @since 1.1
20952121
*/
2096-
public static List<String> readLines(final InputStream input, final String charsetName) throws IOException {
2122+
public static List<String> readLines(final InputStream input, final String charsetName) throws UncheckedIOException {
20972123
return readLines(input, Charsets.toCharset(charsetName));
20982124
}
20992125

@@ -2108,18 +2134,12 @@ public static List<String> readLines(final InputStream input, final String chars
21082134
* @param reader the {@link Reader} to read from, not null
21092135
* @return the list of Strings, never null
21102136
* @throws NullPointerException if the input is null
2111-
* @throws IOException if an I/O error occurs
2137+
* @throws UncheckedIOException if an I/O error occurs
21122138
* @since 1.1
21132139
*/
21142140
@SuppressWarnings("resource") // reader wraps input and is the responsibility of the caller.
2115-
public static List<String> readLines(final Reader reader) throws IOException {
2116-
final BufferedReader bufReader = toBufferedReader(reader);
2117-
final List<String> list = new ArrayList<>();
2118-
String line;
2119-
while ((line = bufReader.readLine()) != null) {
2120-
list.add(line);
2121-
}
2122-
return list;
2141+
public static List<String> readLines(final Reader reader) throws UncheckedIOException {
2142+
return toBufferedReader(reader).lines().collect(Collectors.toList());
21232143
}
21242144

21252145
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -820,7 +820,7 @@ public static DosFileAttributeView getDosFileAttributeView(final Path path, fina
820820
* where {@link File#lastModified()} looses milliseconds and always ends in 000. This bug is in OpenJDK 8 and 9, and
821821
* fixed in 11.
822822
* </p>
823-
*
823+
*
824824
* @param file the file to query.
825825
* @return the file's last modified time.
826826
* @throws IOException Thrown if an I/O error occurs.

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
package org.apache.commons.io.file;
1919

20+
import java.util.stream.Stream;
21+
2022
import org.apache.commons.io.IOUtils;
2123

2224
/**
@@ -43,12 +45,7 @@ public static boolean overrideReadOnly(final DeleteOption[] options) {
4345
if (IOUtils.length(options) == 0) {
4446
return false;
4547
}
46-
for (final DeleteOption deleteOption : options) {
47-
if (deleteOption == StandardDeleteOption.OVERRIDE_READ_ONLY) {
48-
return true;
49-
}
50-
}
51-
return false;
48+
return Stream.of(options).anyMatch(e -> StandardDeleteOption.OVERRIDE_READ_ONLY == e);
5249
}
5350

5451
}

src/main/java/org/apache/commons/io/filefilter/AndFileFilter.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.Collections;
2626
import java.util.List;
2727
import java.util.Objects;
28+
import java.util.stream.Stream;
2829

2930
/**
3031
* A {@link java.io.FileFilter} providing conditional AND logic across a list of
@@ -148,9 +149,7 @@ public void addFileFilter(final IOFileFilter fileFilter) {
148149
* @since 2.9.0
149150
*/
150151
public void addFileFilter(final IOFileFilter... fileFilters) {
151-
for (final IOFileFilter fileFilter : Objects.requireNonNull(fileFilters, "fileFilters")) {
152-
addFileFilter(fileFilter);
153-
}
152+
Stream.of(Objects.requireNonNull(fileFilters, "fileFilters")).forEach(this::addFileFilter);
154153
}
155154

156155
/**

0 commit comments

Comments
 (0)