Add org.apache.commons.io.IOUtils.close(Closeable).
Reuse own API.
MODIFY
src/main/java/org/apache/commons/io/FileUtils.java
src/main/java/org/apache/commons/io/FileUtils.java
@@ -1842,15 +1842,13 @@ public static LineIterator lineIterator(final File file) throws IOException {
* @since 1.2
*/
public static LineIterator lineIterator(final File file, final String encoding) throws IOException {
- InputStream in = null;
+ InputStream inputStream = null;
try {
- in = openInputStream(file);
- return IOUtils.lineIterator(in, encoding);
+ inputStream = openInputStream(file);
+ return IOUtils.lineIterator(inputStream, encoding);
} catch (final IOException | RuntimeException ex) {
try {
- if (in != null) {
- in.close();
- }
+ IOUtils.close(inputStream);
}
catch (final IOException e) {
ex.addSuppressed(e);

MODIFY
src/main/java/org/apache/commons/io/LineIterator.java
src/main/java/org/apache/commons/io/LineIterator.java
@@ -162,9 +162,7 @@ public String nextLine() {
public void close() throws IOException {
finished = true;
cachedLine = null;
- if (this.bufferedReader != null) {
- this.bufferedReader.close();
- }
+ IOUtils.close(bufferedReader);
}
/**
@@ -188,13 +186,7 @@ public void remove() {
*/
@Deprecated
public static void closeQuietly(final LineIterator iterator) {
- try {
- if (iterator != null) {
- iterator.close();
- }
- } catch(final IOException e) {
- // Suppressed.
- }
+ IOUtils.closeQuietly(iterator);
}
}

MODIFY
src/main/java/org/apache/commons/io/input/DemuxInputStream.java
src/main/java/org/apache/commons/io/input/DemuxInputStream.java
import java.io.IOException;
import java.io.InputStream;
+import org.apache.commons.io.IOUtils;
+
/**
* Data written to this stream is forwarded to a stream that has been associated with this thread.
*
@@ -47,10 +49,7 @@ public InputStream bindStream(final InputStream input) {
*/
@Override
public void close() throws IOException {
- final InputStream input = m_streams.get();
- if (null != input) {
- input.close();
- }
+ IOUtils.close(m_streams.get());
}
/**

MODIFY
src/main/java/org/apache/commons/io/output/DemuxOutputStream.java
src/main/java/org/apache/commons/io/output/DemuxOutputStream.java
import java.io.IOException;
import java.io.OutputStream;
+import org.apache.commons.io.IOUtils;
+
/**
* Forwards data to a stream that has been associated with this thread.
*
@@ -47,10 +49,7 @@ public OutputStream bindStream(final OutputStream output) {
*/
@Override
public void close() throws IOException {
- final OutputStream output = outputStreamThreadLocal.get();
- if (null != output) {
- output.close();
- }
+ IOUtils.close(outputStreamThreadLocal.get());
}
/**

MODIFY
src/main/java/org/apache/commons/io/output/FileWriterWithEncoding.java
src/main/java/org/apache/commons/io/output/FileWriterWithEncoding.java
import java.nio.charset.CharsetEncoder;
import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.IOUtils;
/**
* Writer of files that allows the encoding to be set.
@@ -242,9 +243,7 @@ private static Writer initWriter(final File file, final Object encoding, final b
}
} catch (final IOException | RuntimeException ex) {
try {
- if (stream != null) {
- stream.close();
- }
+ IOUtils.close(stream);
} catch (final IOException e) {
ex.addSuppressed(e);
}

[IO-635] Add org.apache.commons.io.IOUtils.close(Closeable).

Reuse own API.