3131import java .io .OutputStream ;
3232import java .io .OutputStreamWriter ;
3333import java .io .Reader ;
34+ import java .io .UncheckedIOException ;
3435import java .io .Writer ;
3536import java .net .HttpURLConnection ;
3637import java .net .ServerSocket ;
4546import java .nio .channels .Selector ;
4647import java .nio .charset .Charset ;
4748import java .nio .file .Files ;
48- import java .util .ArrayList ;
4949import java .util .Arrays ;
5050import java .util .Collection ;
51+ import java .util .Iterator ;
5152import java .util .List ;
5253import java .util .Objects ;
5354import java .util .function .Consumer ;
55+ import java .util .stream .Collectors ;
5456import java .util .stream .Stream ;
5557
5658import 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 /**
0 commit comments