Author: ggregory
Date: Sat May 7 00:06:57 2016
New Revision: 1742675
URL: http://svn.apache.org/viewvc?rev=1742675&view=rev
Log:
[IO-505] Deprecated of all IOUtils.closeQuietly() methods and use
try-with-resources internally.
Modified:
commons/proper/io/trunk/src/changes/changes.xml
commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java
commons/proper/io/trunk/src/main/java/org/apache/commons/io/IOUtils.java
commons/proper/io/trunk/src/main/java/org/apache/commons/io/filefilter/MagicNumberFileFilter.java
commons/proper/io/trunk/src/main/java/org/apache/commons/io/input/Tailer.java
commons/proper/io/trunk/src/main/java/org/apache/commons/io/output/DeferredFileOutputStream.java
commons/proper/io/trunk/src/main/java/org/apache/commons/io/output/FileWriterWithEncoding.java
commons/proper/io/trunk/src/main/java/org/apache/commons/io/output/LockableFileWriter.java
Modified: commons/proper/io/trunk/src/changes/changes.xml
URL:
http://svn.apache.org/viewvc/commons/proper/io/trunk/src/changes/changes.xml?rev=1742675&r1=1742674&r2=1742675&view=diff
==============================================================================
--- commons/proper/io/trunk/src/changes/changes.xml (original)
+++ commons/proper/io/trunk/src/changes/changes.xml Sat May 7 00:06:57 2016
@@ -59,6 +59,9 @@ The <action> type attribute can be add,u
<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>
Modified:
commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java
URL:
http://svn.apache.org/viewvc/commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java?rev=1742675&r1=1742674&r2=1742675&view=diff
==============================================================================
--- commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java
(original)
+++ commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java
Sat May 7 00:06:57 2016
@@ -432,8 +432,7 @@ public class FileUtils {
*/
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) {
@@ -745,16 +744,9 @@ public class FileUtils {
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);
}
}
@@ -798,22 +790,13 @@ public class FileUtils {
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);
}
}
@@ -1133,15 +1116,10 @@ public class FileUtils {
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;
@@ -1154,20 +1132,6 @@ public class FileUtils {
}
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
@@ -1536,10 +1500,8 @@ public class FileUtils {
* @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);
}
}
@@ -1561,12 +1523,9 @@ public class FileUtils {
* @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);
}
}
@@ -1772,12 +1731,8 @@ public class FileUtils {
* @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);
}
}
@@ -1822,12 +1777,8 @@ public class FileUtils {
* @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);
}
}
@@ -1842,12 +1793,8 @@ public class FileUtils {
* @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);
}
}
@@ -1917,11 +1864,15 @@ public class FileUtils {
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;
}
}
@@ -1986,15 +1937,10 @@ public class FileUtils {
* @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);
}
}
@@ -2200,13 +2146,8 @@ public class FileUtils {
*/
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);
}
}
@@ -2317,15 +2258,8 @@ public class FileUtils {
*/
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);
}
}
@@ -2880,12 +2814,8 @@ public class FileUtils {
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;
}
Modified:
commons/proper/io/trunk/src/main/java/org/apache/commons/io/IOUtils.java
URL:
http://svn.apache.org/viewvc/commons/proper/io/trunk/src/main/java/org/apache/commons/io/IOUtils.java?rev=1742675&r1=1742674&r2=1742675&view=diff
==============================================================================
--- commons/proper/io/trunk/src/main/java/org/apache/commons/io/IOUtils.java
(original)
+++ commons/proper/io/trunk/src/main/java/org/apache/commons/io/IOUtils.java
Sat May 7 00:06:57 2016
@@ -212,7 +212,12 @@ public class IOUtils {
* </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);
}
@@ -238,7 +243,12 @@ public class IOUtils {
* </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);
}
@@ -265,7 +275,12 @@ public class IOUtils {
* </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);
}
@@ -293,7 +308,12 @@ public class IOUtils {
* </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);
}
@@ -332,7 +352,12 @@ public class IOUtils {
*
* @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) {
@@ -385,7 +410,12 @@ public class IOUtils {
* @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;
@@ -417,7 +447,12 @@ public class IOUtils {
*
* @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 {
@@ -450,7 +485,12 @@ public class IOUtils {
*
* @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 {
@@ -483,7 +523,12 @@ public class IOUtils {
*
* @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 {
Modified:
commons/proper/io/trunk/src/main/java/org/apache/commons/io/filefilter/MagicNumberFileFilter.java
URL:
http://svn.apache.org/viewvc/commons/proper/io/trunk/src/main/java/org/apache/commons/io/filefilter/MagicNumberFileFilter.java?rev=1742675&r1=1742674&r2=1742675&view=diff
==============================================================================
---
commons/proper/io/trunk/src/main/java/org/apache/commons/io/filefilter/MagicNumberFileFilter.java
(original)
+++
commons/proper/io/trunk/src/main/java/org/apache/commons/io/filefilter/MagicNumberFileFilter.java
Sat May 7 00:06:57 2016
@@ -23,8 +23,6 @@ import java.io.Serializable;
import java.nio.charset.Charset;
import java.util.Arrays;
-import org.apache.commons.io.IOUtils;
-
/**
* <p>
* File filter for matching files containing a "magic number". A magic number
@@ -234,20 +232,19 @@ public class MagicNumberFileFilter exten
@Override
public boolean accept(final File file) {
if (file != null && file.isFile() && file.canRead()) {
- RandomAccessFile randomAccessFile = null;
try {
- final byte[] fileBytes = new byte[this.magicNumbers.length];
- randomAccessFile = new RandomAccessFile(file, "r");
- randomAccessFile.seek(byteOffset);
- final int read = randomAccessFile.read(fileBytes);
- if (read != magicNumbers.length) {
- return false;
+ try (RandomAccessFile randomAccessFile = new
RandomAccessFile(file, "r")) {
+ final byte[] fileBytes = new
byte[this.magicNumbers.length];
+ randomAccessFile.seek(byteOffset);
+ final int read = randomAccessFile.read(fileBytes);
+ if (read != magicNumbers.length) {
+ return false;
+ }
+ return Arrays.equals(this.magicNumbers, fileBytes);
}
- return Arrays.equals(this.magicNumbers, fileBytes);
- } catch (final IOException ioe) {
+ }
+ catch (final IOException ioe) {
// Do nothing, fall through and do not accept file
- } finally {
- IOUtils.closeQuietly(randomAccessFile);
}
}
Modified:
commons/proper/io/trunk/src/main/java/org/apache/commons/io/input/Tailer.java
URL:
http://svn.apache.org/viewvc/commons/proper/io/trunk/src/main/java/org/apache/commons/io/input/Tailer.java?rev=1742675&r1=1742674&r2=1742675&view=diff
==============================================================================
---
commons/proper/io/trunk/src/main/java/org/apache/commons/io/input/Tailer.java
(original)
+++
commons/proper/io/trunk/src/main/java/org/apache/commons/io/input/Tailer.java
Sat May 7 00:06:57 2016
@@ -26,7 +26,6 @@ import java.io.RandomAccessFile;
import java.nio.charset.Charset;
import org.apache.commons.io.FileUtils;
-import org.apache.commons.io.IOUtils;
/**
* Simple implementation of the unix "tail -f" functionality.
@@ -431,10 +430,9 @@ public class Tailer implements Runnable
if (length < position) {
// File was rotated
listener.fileRotated();
- // Reopen the reader after rotation
- try {
- // Ensure that the old file is closed iff we re-open
it successfully
- final RandomAccessFile save = reader;
+ // Reopen the reader after rotation ensuring that the old
file is closed iff we re-open it
+ // successfully
+ try (RandomAccessFile save = reader) {
reader = new RandomAccessFile(file, RAF_MODE);
// At this point, we're sure that the old file is
rotated
// Finish scanning the old file and then we'll start
with the new one
@@ -444,8 +442,6 @@ public class Tailer implements Runnable
listener.handle(ioe);
}
position = 0;
- // close old file explicitly rather than relying on GC
picking up previous RAF
- IOUtils.closeQuietly(save);
} catch (final FileNotFoundException e) {
// in this case we continue to use the previous reader
and position values
listener.fileNotFound();
@@ -471,8 +467,8 @@ public class Tailer implements Runnable
last = file.lastModified();
}
}
- if (reOpen) {
- IOUtils.closeQuietly(reader);
+ if (reOpen && reader != null) {
+ reader.close();
}
Thread.sleep(delayMillis);
if (getRun() && reOpen) {
@@ -482,24 +478,23 @@ public class Tailer implements Runnable
}
} catch (final InterruptedException e) {
Thread.currentThread().interrupt();
- stop(e);
+ listener.handle(e);
} catch (final Exception e) {
- stop(e);
+ listener.handle(e);
} finally {
- IOUtils.closeQuietly(reader);
+ try {
+ if (reader != null) {
+ reader.close();
+ }
+ }
+ catch (final IOException e) {
+ listener.handle(e);
+ }
+ stop();
}
}
/**
- * Stops the tailer with an exception
- * @param e The exception to send to listener
- */
- private void stop(final Exception e) {
- listener.handle(e);
- stop();
- }
-
- /**
* Allows the tailer to complete its current loop and return.
*/
public void stop() {
@@ -514,47 +509,47 @@ public class Tailer implements Runnable
* @throws java.io.IOException if an I/O error occurs.
*/
private long readLines(final RandomAccessFile reader) throws IOException {
- ByteArrayOutputStream lineBuf = new ByteArrayOutputStream(64);
- long pos = reader.getFilePointer();
- long rePos = pos; // position to re-read
- int num;
- boolean seenCR = false;
- while (getRun() && ((num = reader.read(inbuf)) != EOF)) {
- for (int i = 0; i < num; i++) {
- final byte ch = inbuf[i];
- switch (ch) {
- case '\n':
- seenCR = false; // swallow CR before LF
- listener.handle(new String(lineBuf.toByteArray(), cset));
- lineBuf.reset();
- rePos = pos + i + 1;
- break;
- case '\r':
- if (seenCR) {
- lineBuf.write('\r');
+ try (ByteArrayOutputStream lineBuf = new ByteArrayOutputStream(64)) {
+ long pos = reader.getFilePointer();
+ long rePos = pos; // position to re-read
+ int num;
+ boolean seenCR = false;
+ while (getRun() && ((num = reader.read(inbuf)) != EOF)) {
+ for (int i = 0; i < num; i++) {
+ final byte ch = inbuf[i];
+ switch ( ch ) {
+ case '\n':
+ seenCR = false; // swallow CR before LF
+ listener.handle(new String(lineBuf.toByteArray(),
cset));
+ lineBuf.reset();
+ rePos = pos + i + 1;
+ break;
+ case '\r':
+ if (seenCR) {
+ lineBuf.write('\r');
+ }
+ seenCR = true;
+ break;
+ default:
+ if (seenCR) {
+ seenCR = false; // swallow final CR
+ listener.handle(new
String(lineBuf.toByteArray(), cset));
+ lineBuf.reset();
+ rePos = pos + i + 1;
+ }
+ lineBuf.write(ch);
}
- seenCR = true;
- break;
- default:
- if (seenCR) {
- seenCR = false; // swallow final CR
- listener.handle(new String(lineBuf.toByteArray(),
cset));
- lineBuf.reset();
- rePos = pos + i + 1;
- }
- lineBuf.write(ch);
}
+ pos = reader.getFilePointer();
}
- pos = reader.getFilePointer();
- }
- IOUtils.closeQuietly(lineBuf); // not strictly necessary
- reader.seek(rePos); // Ensure we can re-read if necessary
- if (listener instanceof TailerListenerAdapter) {
- ((TailerListenerAdapter) listener).endOfFileReached();
- }
+ reader.seek(rePos); // Ensure we can re-read if necessary
- return rePos;
- }
+ if (listener instanceof TailerListenerAdapter) {
+ ((TailerListenerAdapter) listener).endOfFileReached();
+ }
+ return rePos;
+ }
+ }
}
Modified:
commons/proper/io/trunk/src/main/java/org/apache/commons/io/output/DeferredFileOutputStream.java
URL:
http://svn.apache.org/viewvc/commons/proper/io/trunk/src/main/java/org/apache/commons/io/output/DeferredFileOutputStream.java?rev=1742675&r1=1742674&r2=1742675&view=diff
==============================================================================
---
commons/proper/io/trunk/src/main/java/org/apache/commons/io/output/DeferredFileOutputStream.java
(original)
+++
commons/proper/io/trunk/src/main/java/org/apache/commons/io/output/DeferredFileOutputStream.java
Sat May 7 00:06:57 2016
@@ -267,22 +267,15 @@ public class DeferredFileOutputStream
// we may only need to check if this is closed if we are working with
a file
// but we should force the habit of closing wether we are working with
// a file or memory.
- if (!closed)
- {
+ if (!closed) {
throw new IOException("Stream not closed");
}
- if(isInMemory())
- {
+ if (isInMemory()) {
memoryOutputStream.writeTo(out);
- }
- else
- {
- final FileInputStream fis = new FileInputStream(outputFile);
- try {
+ } else {
+ try (FileInputStream fis = new FileInputStream(outputFile)) {
IOUtils.copy(fis, out);
- } finally {
- IOUtils.closeQuietly(fis);
}
}
}
Modified:
commons/proper/io/trunk/src/main/java/org/apache/commons/io/output/FileWriterWithEncoding.java
URL:
http://svn.apache.org/viewvc/commons/proper/io/trunk/src/main/java/org/apache/commons/io/output/FileWriterWithEncoding.java?rev=1742675&r1=1742674&r2=1742675&view=diff
==============================================================================
---
commons/proper/io/trunk/src/main/java/org/apache/commons/io/output/FileWriterWithEncoding.java
(original)
+++
commons/proper/io/trunk/src/main/java/org/apache/commons/io/output/FileWriterWithEncoding.java
Sat May 7 00:06:57 2016
@@ -26,7 +26,6 @@ import java.nio.charset.Charset;
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.
@@ -230,34 +229,30 @@ public class FileWriterWithEncoding exte
if (encoding == null) {
throw new NullPointerException("Encoding is missing");
}
- final boolean fileExistedAlready = file.exists();
OutputStream stream = null;
- Writer writer = null;
+ final boolean fileExistedAlready = file.exists();
try {
stream = new FileOutputStream(file, append);
if (encoding instanceof Charset) {
- writer = new OutputStreamWriter(stream, (Charset)encoding);
+ return new OutputStreamWriter(stream, (Charset)encoding);
} else if (encoding instanceof CharsetEncoder) {
- writer = new OutputStreamWriter(stream,
(CharsetEncoder)encoding);
+ return new OutputStreamWriter(stream,
(CharsetEncoder)encoding);
} else {
- writer = new OutputStreamWriter(stream, (String)encoding);
+ return new OutputStreamWriter(stream, (String)encoding);
}
- } catch (final IOException ex) {
- IOUtils.closeQuietly(writer);
- IOUtils.closeQuietly(stream);
- if (fileExistedAlready == false) {
- FileUtils.deleteQuietly(file);
+ } catch (final IOException | RuntimeException ex) {
+ try {
+ if (stream != null) {
+ stream.close();
+ }
+ } catch (final IOException e) {
+ ex.addSuppressed(e);
}
- throw ex;
- } catch (final RuntimeException ex) {
- IOUtils.closeQuietly(writer);
- IOUtils.closeQuietly(stream);
if (fileExistedAlready == false) {
FileUtils.deleteQuietly(file);
}
throw ex;
}
- return writer;
}
//-----------------------------------------------------------------------
Modified:
commons/proper/io/trunk/src/main/java/org/apache/commons/io/output/LockableFileWriter.java
URL:
http://svn.apache.org/viewvc/commons/proper/io/trunk/src/main/java/org/apache/commons/io/output/LockableFileWriter.java?rev=1742675&r1=1742674&r2=1742675&view=diff
==============================================================================
---
commons/proper/io/trunk/src/main/java/org/apache/commons/io/output/LockableFileWriter.java
(original)
+++
commons/proper/io/trunk/src/main/java/org/apache/commons/io/output/LockableFileWriter.java
Sat May 7 00:06:57 2016
@@ -19,14 +19,12 @@ package org.apache.commons.io.output;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
-import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.Charset;
import org.apache.commons.io.Charsets;
import org.apache.commons.io.FileUtils;
-import org.apache.commons.io.IOUtils;
/**
* FileWriter that will create and honor lock files to allow simple
@@ -267,29 +265,17 @@ public class LockableFileWriter extends
*/
private Writer initWriter(final File file, final Charset encoding, final
boolean append) throws IOException {
final boolean fileExistedAlready = file.exists();
- OutputStream stream = null;
- Writer writer = null;
try {
- stream = new FileOutputStream(file.getAbsolutePath(), append);
- writer = new OutputStreamWriter(stream,
Charsets.toCharset(encoding));
- } catch (final IOException ex) {
- IOUtils.closeQuietly(writer);
- IOUtils.closeQuietly(stream);
- FileUtils.deleteQuietly(lockFile);
- if (fileExistedAlready == false) {
- FileUtils.deleteQuietly(file);
- }
- throw ex;
- } catch (final RuntimeException ex) {
- IOUtils.closeQuietly(writer);
- IOUtils.closeQuietly(stream);
+ return new OutputStreamWriter(new
FileOutputStream(file.getAbsolutePath(), append),
+ Charsets.toCharset(encoding));
+
+ } catch (final IOException | RuntimeException ex) {
FileUtils.deleteQuietly(lockFile);
if (fileExistedAlready == false) {
FileUtils.deleteQuietly(file);
}
throw ex;
}
- return writer;
}
//-----------------------------------------------------------------------