Skip to content

Commit

Permalink
[IO-505] Deprecated of all IOUtils.closeQuietly() methods and use try…
Browse files Browse the repository at this point in the history
…-with-resources internally.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@1742675 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
garydgregory committed May 7, 2016
1 parent 842bf6f commit 4dc97b6
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 223 deletions.
3 changes: 3 additions & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ The <action> type attribute can be add,update,fix,remove.
<action issue="IO-505" dev="ggregory" type="update" due-to="Christian Schulte">
Make LineIterator implement Closeable to support try-with-resources statements.
</action>
<action issue="IO-505" dev="ggregory" type="update" due-to="Christian Schulte">
Deprecated of all IOUtils.closeQuietly() methods and use try-with-resources internally.
</action>
<action issue="IO-507" dev="ggregory" type="new">
Add a ByteOrderFactory class.
</action>
Expand Down
144 changes: 37 additions & 107 deletions src/main/java/org/apache/commons/io/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -432,8 +432,7 @@ public static String byteCountToDisplaySize(final long size) {
*/
public static void touch(final File file) throws IOException {
if (!file.exists()) {
final OutputStream out = openOutputStream(file);
IOUtils.closeQuietly(out);
openOutputStream(file).close();
}
final boolean success = file.setLastModified(System.currentTimeMillis());
if (!success) {
Expand Down Expand Up @@ -745,16 +744,9 @@ public static boolean contentEquals(final File file1, final File file2) throws I
return true;
}

InputStream input1 = null;
InputStream input2 = null;
try {
input1 = new FileInputStream(file1);
input2 = new FileInputStream(file2);
try (InputStream input1 = new FileInputStream(file1);
InputStream input2 = new FileInputStream(file2)) {
return IOUtils.contentEquals(input1, input2);

} finally {
IOUtils.closeQuietly(input1);
IOUtils.closeQuietly(input2);
}
}

Expand Down Expand Up @@ -798,22 +790,13 @@ public static boolean contentEqualsIgnoreEOL(final File file1, final File file2,
return true;
}

Reader input1 = null;
Reader input2 = null;
try {
if (charsetName == null) {
// N.B. make explicit the use of the default charset
input1 = new InputStreamReader(new FileInputStream(file1), Charset.defaultCharset());
input2 = new InputStreamReader(new FileInputStream(file2), Charset.defaultCharset());
} else {
input1 = new InputStreamReader(new FileInputStream(file1), charsetName);
input2 = new InputStreamReader(new FileInputStream(file2), charsetName);
}
try (Reader input1 = charsetName == null
? new InputStreamReader(new FileInputStream(file1), Charset.defaultCharset())
: new InputStreamReader(new FileInputStream(file1), charsetName);
Reader input2 = charsetName == null
? new InputStreamReader(new FileInputStream(file2), Charset.defaultCharset())
: new InputStreamReader(new FileInputStream(file2), charsetName)) {
return IOUtils.contentEqualsIgnoreEOL(input1, input2);

} finally {
IOUtils.closeQuietly(input1);
IOUtils.closeQuietly(input2);
}
}

Expand Down Expand Up @@ -1133,15 +1116,10 @@ private static void doCopyFile(final File srcFile, final File destFile, final bo
throw new IOException("Destination '" + destFile + "' exists but is a directory");
}

FileInputStream fis = null;
FileOutputStream fos = null;
FileChannel input = null;
FileChannel output = null;
try {
fis = new FileInputStream(srcFile);
fos = new FileOutputStream(destFile);
input = fis.getChannel();
output = fos.getChannel();
try (FileInputStream fis = new FileInputStream(srcFile);
FileChannel input = fis.getChannel();
FileOutputStream fos = new FileOutputStream(destFile);
FileChannel output = fos.getChannel()) {
final long size = input.size(); // TODO See IO-386
long pos = 0;
long count = 0;
Expand All @@ -1154,20 +1132,6 @@ private static void doCopyFile(final File srcFile, final File destFile, final bo
}
pos += bytesCopied;
}

output.close();
output = null;

fos.close();
fos = null;

input.close();
input = null;

fis.close();
fis = null;
} finally {
IOUtils.closeQuietly(output, fos, input, fis);
}

final long srcLen = srcFile.length(); // TODO See IO-386
Expand Down Expand Up @@ -1536,10 +1500,8 @@ public static void copyURLToFile(final URL source, final File destination,
* @since 2.0
*/
public static void copyInputStreamToFile(final InputStream source, final File destination) throws IOException {
try {
copyToFile(source, destination);
} finally {
IOUtils.closeQuietly(source);
try (InputStream in = source) {
copyToFile(in, destination);
}
}

Expand All @@ -1561,12 +1523,9 @@ public static void copyInputStreamToFile(final InputStream source, final File de
* @since 2.5
*/
public static void copyToFile(final InputStream source, final File destination) throws IOException {
final FileOutputStream output = openOutputStream(destination);
try {
IOUtils.copy(source, output);
output.close(); // don't swallow close Exception if copy completes normally
} finally {
IOUtils.closeQuietly(output);
try (InputStream in = source;
OutputStream out = openOutputStream(destination)) {
IOUtils.copy(in, out);
}
}

Expand Down Expand Up @@ -1772,12 +1731,8 @@ public static boolean waitFor(final File file, final int seconds) {
* @since 2.3
*/
public static String readFileToString(final File file, final Charset encoding) throws IOException {
InputStream in = null;
try {
in = openInputStream(file);
try (InputStream in = openInputStream(file)) {
return IOUtils.toString(in, Charsets.toCharset(encoding));
} finally {
IOUtils.closeQuietly(in);
}
}

Expand Down Expand Up @@ -1822,12 +1777,8 @@ public static String readFileToString(final File file) throws IOException {
* @since 1.1
*/
public static byte[] readFileToByteArray(final File file) throws IOException {
InputStream in = null;
try {
in = openInputStream(file);
try (InputStream in = openInputStream(file)) {
return IOUtils.toByteArray(in); // Do NOT use file.length() - see IO-453
} finally {
IOUtils.closeQuietly(in);
}
}

Expand All @@ -1842,12 +1793,8 @@ public static byte[] readFileToByteArray(final File file) throws IOException {
* @since 2.3
*/
public static List<String> readLines(final File file, final Charset encoding) throws IOException {
InputStream in = null;
try {
in = openInputStream(file);
try (InputStream in = openInputStream(file)) {
return IOUtils.readLines(in, Charsets.toCharset(encoding));
} finally {
IOUtils.closeQuietly(in);
}
}

Expand Down Expand Up @@ -1917,11 +1864,15 @@ public static LineIterator lineIterator(final File file, final String encoding)
try {
in = openInputStream(file);
return IOUtils.lineIterator(in, encoding);
} catch (final IOException ex) {
IOUtils.closeQuietly(in);
throw ex;
} catch (final RuntimeException ex) {
IOUtils.closeQuietly(in);
} catch (final IOException | RuntimeException ex) {
try {
if (in != null) {
in.close();
}
}
catch (final IOException e) {
ex.addSuppressed(e);
}
throw ex;
}
}
Expand Down Expand Up @@ -1986,15 +1937,10 @@ public static void writeStringToFile(final File file, final String data, final S
* @throws IOException in case of an I/O error
* @since 2.3
*/
public static void writeStringToFile(final File file, final String data, final Charset encoding, final boolean
append) throws IOException {
OutputStream out = null;
try {
out = openOutputStream(file, append);
public static void writeStringToFile(final File file, final String data, final Charset encoding,
final boolean append) throws IOException {
try (OutputStream out = openOutputStream(file, append)) {
IOUtils.write(data, out, encoding);
out.close(); // don't swallow close Exception if copy completes normally
} finally {
IOUtils.closeQuietly(out);
}
}

Expand Down Expand Up @@ -2200,13 +2146,8 @@ public static void writeByteArrayToFile(final File file, final byte[] data, fina
*/
public static void writeByteArrayToFile(final File file, final byte[] data, final int off, final int len,
final boolean append) throws IOException {
OutputStream out = null;
try {
out = openOutputStream(file, append);
try (OutputStream out = openOutputStream(file, append)) {
out.write(data, off, len);
out.close(); // don't swallow close Exception if copy completes normally
} finally {
IOUtils.closeQuietly(out);
}
}

Expand Down Expand Up @@ -2317,15 +2258,8 @@ public static void writeLines(final File file, final String encoding, final Coll
*/
public static void writeLines(final File file, final String encoding, final Collection<?> lines,
final String lineEnding, final boolean append) throws IOException {
FileOutputStream out = null;
try {
out = openOutputStream(file, append);
final BufferedOutputStream buffer = new BufferedOutputStream(out);
IOUtils.writeLines(lines, lineEnding, buffer, encoding);
buffer.flush();
out.close(); // don't swallow close Exception if copy completes normally
} finally {
IOUtils.closeQuietly(out);
try (OutputStream out = new BufferedOutputStream(openOutputStream(file, append))) {
IOUtils.writeLines(lines, lineEnding, out, encoding);
}
}

Expand Down Expand Up @@ -2880,12 +2814,8 @@ public static Checksum checksum(final File file, final Checksum checksum) throws
if (file.isDirectory()) {
throw new IllegalArgumentException("Checksums can't be computed on directories");
}
InputStream in = null;
try {
in = new CheckedInputStream(new FileInputStream(file), checksum);
try (InputStream in = new CheckedInputStream(new FileInputStream(file), checksum)) {
IOUtils.copy(in, new NullOutputStream());
} finally {
IOUtils.closeQuietly(in);
}
return checksum;
}
Expand Down
45 changes: 45 additions & 0 deletions src/main/java/org/apache/commons/io/IOUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,12 @@ public static void close(final URLConnection conn) {
* </pre>
*
* @param input the Reader to close, may be null or already closed
*
* @deprecated As of 2.6 removed without replacement. Please use the try-with-resources statement or handle
* suppressed exceptions manually.
* @see Throwable#addSuppressed(java.lang.Throwable)
*/
@Deprecated
public static void closeQuietly(final Reader input) {
closeQuietly((Closeable) input);
}
Expand All @@ -238,7 +243,12 @@ public static void closeQuietly(final Reader input) {
* </pre>
*
* @param output the Writer to close, may be null or already closed
*
* @deprecated As of 2.6 removed without replacement. Please use the try-with-resources statement or handle
* suppressed exceptions manually.
* @see Throwable#addSuppressed(java.lang.Throwable)
*/
@Deprecated
public static void closeQuietly(final Writer output) {
closeQuietly((Closeable) output);
}
Expand All @@ -265,7 +275,12 @@ public static void closeQuietly(final Writer output) {
* </pre>
*
* @param input the InputStream to close, may be null or already closed
*
* @deprecated As of 2.6 removed without replacement. Please use the try-with-resources statement or handle
* suppressed exceptions manually.
* @see Throwable#addSuppressed(java.lang.Throwable)
*/
@Deprecated
public static void closeQuietly(final InputStream input) {
closeQuietly((Closeable) input);
}
Expand Down Expand Up @@ -293,7 +308,12 @@ public static void closeQuietly(final InputStream input) {
* </pre>
*
* @param output the OutputStream to close, may be null or already closed
*
* @deprecated As of 2.6 removed without replacement. Please use the try-with-resources statement or handle
* suppressed exceptions manually.
* @see Throwable#addSuppressed(java.lang.Throwable)
*/
@Deprecated
public static void closeQuietly(final OutputStream output) {
closeQuietly((Closeable) output);
}
Expand Down Expand Up @@ -332,7 +352,12 @@ public static void closeQuietly(final OutputStream output) {
*
* @param closeable the objects to close, may be null or already closed
* @since 2.0
*
* @deprecated As of 2.6 removed without replacement. Please use the try-with-resources statement or handle
* suppressed exceptions manually.
* @see Throwable#addSuppressed(java.lang.Throwable)
*/
@Deprecated
public static void closeQuietly(final Closeable closeable) {
try {
if (closeable != null) {
Expand Down Expand Up @@ -385,7 +410,12 @@ public static void closeQuietly(final Closeable closeable) {
* @param closeables the objects to close, may be null or already closed
* @see #closeQuietly(Closeable)
* @since 2.5
*
* @deprecated As of 2.6 removed without replacement. Please use the try-with-resources statement or handle
* suppressed exceptions manually.
* @see Throwable#addSuppressed(java.lang.Throwable)
*/
@Deprecated
public static void closeQuietly(final Closeable... closeables) {
if (closeables == null) {
return;
Expand Down Expand Up @@ -417,7 +447,12 @@ public static void closeQuietly(final Closeable... closeables) {
*
* @param sock the Socket to close, may be null or already closed
* @since 2.0
*
* @deprecated As of 2.6 removed without replacement. Please use the try-with-resources statement or handle
* suppressed exceptions manually.
* @see Throwable#addSuppressed(java.lang.Throwable)
*/
@Deprecated
public static void closeQuietly(final Socket sock) {
if (sock != null) {
try {
Expand Down Expand Up @@ -450,7 +485,12 @@ public static void closeQuietly(final Socket sock) {
*
* @param selector the Selector to close, may be null or already closed
* @since 2.2
*
* @deprecated As of 2.6 removed without replacement. Please use the try-with-resources statement or handle
* suppressed exceptions manually.
* @see Throwable#addSuppressed(java.lang.Throwable)
*/
@Deprecated
public static void closeQuietly(final Selector selector) {
if (selector != null) {
try {
Expand Down Expand Up @@ -483,7 +523,12 @@ public static void closeQuietly(final Selector selector) {
*
* @param sock the ServerSocket to close, may be null or already closed
* @since 2.2
*
* @deprecated As of 2.6 removed without replacement. Please use the try-with-resources statement or handle
* suppressed exceptions manually.
* @see Throwable#addSuppressed(java.lang.Throwable)
*/
@Deprecated
public static void closeQuietly(final ServerSocket sock) {
if (sock != null) {
try {
Expand Down

0 comments on commit 4dc97b6

Please sign in to comment.