7020888: (file) Miscellaneous and trivial clean-ups (typos and opportunities to use suppressed exceptions)
Reviewed-by: mduigou, chegar
--- a/jdk/src/share/classes/java/io/BufferedReader.java Tue Feb 22 10:19:35 2011 +0000
+++ b/jdk/src/share/classes/java/io/BufferedReader.java Tue Feb 22 12:04:48 2011 +0000
@@ -512,11 +512,14 @@
public void close() throws IOException {
synchronized (lock) {
- if (in == null)
- return;
- in.close();
- in = null;
- cb = null;
+ if (in != null) {
+ try {
+ in.close();
+ } finally {
+ in = null;
+ cb = null;
+ }
+ }
}
}
}
--- a/jdk/src/share/classes/java/io/BufferedWriter.java Tue Feb 22 10:19:35 2011 +0000
+++ b/jdk/src/share/classes/java/io/BufferedWriter.java Tue Feb 22 12:04:48 2011 +0000
@@ -255,17 +255,16 @@
}
}
+ @SuppressWarnings("try")
public void close() throws IOException {
synchronized (lock) {
- if (out == null) {
- return;
- }
- try {
- flushBuffer();
- } finally {
- out.close();
- out = null;
- cb = null;
+ if (out != null) {
+ try (Writer w = out) {
+ flushBuffer();
+ } finally {
+ out = null;
+ cb = null;
+ }
}
}
}
--- a/jdk/src/share/classes/java/io/File.java Tue Feb 22 10:19:35 2011 +0000
+++ b/jdk/src/share/classes/java/io/File.java Tue Feb 22 12:04:48 2011 +0000
@@ -2055,7 +2055,7 @@
*
* @return a {@code Path} constructed from this abstract path
*
- * @throws InvalidPathException
+ * @throws java.nio.file.InvalidPathException
* if a {@code Path} object cannot be constructed from the abstract
* path (see {@link java.nio.file.FileSystem#getPath FileSystem.getPath})
*
--- a/jdk/src/share/classes/java/io/FilterOutputStream.java Tue Feb 22 10:19:35 2011 +0000
+++ b/jdk/src/share/classes/java/io/FilterOutputStream.java Tue Feb 22 12:04:48 2011 +0000
@@ -152,11 +152,10 @@
* @see java.io.FilterOutputStream#flush()
* @see java.io.FilterOutputStream#out
*/
+ @SuppressWarnings("try")
public void close() throws IOException {
- try {
- flush();
- } catch (IOException ignored) {
+ try (OutputStream ostream = out) {
+ flush();
}
- out.close();
}
}
--- a/jdk/src/share/classes/java/io/PushbackInputStream.java Tue Feb 22 10:19:35 2011 +0000
+++ b/jdk/src/share/classes/java/io/PushbackInputStream.java Tue Feb 22 12:04:48 2011 +0000
@@ -374,10 +374,13 @@
* @exception IOException if an I/O error occurs.
*/
public synchronized void close() throws IOException {
- if (in == null)
- return;
- in.close();
- in = null;
- buf = null;
+ if (in != null) {
+ try {
+ in.close();
+ } finally {
+ in = null;
+ buf = null;
+ }
+ }
}
}
--- a/jdk/src/share/classes/java/io/PushbackReader.java Tue Feb 22 10:19:35 2011 +0000
+++ b/jdk/src/share/classes/java/io/PushbackReader.java Tue Feb 22 12:04:48 2011 +0000
@@ -245,8 +245,11 @@
* @exception IOException If an I/O error occurs
*/
public void close() throws IOException {
- super.close();
- buf = null;
+ try {
+ super.close();
+ } finally {
+ buf = null;
+ }
}
/**
--- a/jdk/src/share/classes/java/nio/channels/AsynchronousFileChannel.java Tue Feb 22 10:19:35 2011 +0000
+++ b/jdk/src/share/classes/java/nio/channels/AsynchronousFileChannel.java Tue Feb 22 12:04:48 2011 +0000
@@ -53,7 +53,7 @@
* operation. This class also defines read and write methods that initiate
* asynchronous operations, returning a {@link Future} to represent the pending
* result of the operation. The {@code Future} may be used to check if the
- * operation has completed, to wait for its completion.
+ * operation has completed, wait for its completion, and retrieve the result.
*
* <p> In addition to read and write operations, this class defines the
* following operations: </p>
@@ -79,7 +79,7 @@
* itself a thread in the thread pool, then the completion handler may be invoked
* directly by the initiating thread. When an {@code AsynchronousFileChannel} is
* created without specifying a thread pool then the channel is associated with
- * a system-dependent and default thread pool that may be shared with other
+ * a system-dependent default thread pool that may be shared with other
* channels. The default thread pool is configured by the system properties
* defined by the {@link AsynchronousChannelGroup} class.
*
--- a/jdk/src/share/classes/java/nio/channels/SocketChannel.java Tue Feb 22 10:19:35 2011 +0000
+++ b/jdk/src/share/classes/java/nio/channels/SocketChannel.java Tue Feb 22 12:04:48 2011 +0000
@@ -182,10 +182,13 @@
SocketChannel sc = open();
try {
sc.connect(remote);
- } finally {
- if (!sc.isConnected()) {
- try { sc.close(); } catch (IOException x) { }
+ } catch (Throwable x) {
+ try {
+ sc.close();
+ } catch (Throwable suppressed) {
+ x.addSuppressed(suppressed);
}
+ throw x;
}
assert sc.isConnected();
return sc;
--- a/jdk/src/share/classes/java/nio/file/CopyMoveHelper.java Tue Feb 22 10:19:35 2011 +0000
+++ b/jdk/src/share/classes/java/nio/file/CopyMoveHelper.java Tue Feb 22 12:04:48 2011 +0000
@@ -135,11 +135,13 @@
view.setTimes(attrs.lastModifiedTime(),
attrs.lastAccessTime(),
attrs.creationTime());
- } catch (IOException x) {
+ } catch (Throwable x) {
// rollback
try {
Files.delete(target);
- } catch (IOException ignore) { }
+ } catch (Throwable suppressed) {
+ x.addSuppressed(suppressed);
+ }
throw x;
}
}
--- a/jdk/src/share/classes/java/nio/file/Files.java Tue Feb 22 10:19:35 2011 +0000
+++ b/jdk/src/share/classes/java/nio/file/Files.java Tue Feb 22 12:04:48 2011 +0000
@@ -129,17 +129,18 @@
* <pre>
* Path path = ...
*
- * // replace an existing file or create the file if it doesn't initially exist
+ * // truncate and overwrite an existing file, or create the file if
+ * // it doesn't initially exist
* OutputStream out = Files.newOutputStream(path);
*
* // append to an existing file, fail if the file does not exist
* out = Files.newOutputStream(path, APPEND);
*
* // append to an existing file, create file if it doesn't initially exist
- * out = Files.newOutputStream(CREATE, APPEND);
+ * out = Files.newOutputStream(path, CREATE, APPEND);
*
* // always create new file, failing if it already exists
- * out = Files.newOutputStream(CREATE_NEW);
+ * out = Files.newOutputStream(path, CREATE_NEW);
* </pre>
*
* @param path
@@ -895,8 +896,8 @@
/**
* Creates a new directory in the default temporary-file directory, using
- * the given prefix and suffix to generate its name. The resulting {@code
- * Path} is associated with the default {@code FileSystem}.
+ * the given prefix to generate its name. The resulting {@code Path} is
+ * associated with the default {@code FileSystem}.
*
* <p> This method works in exactly the manner specified by {@link
* #createTempDirectory(Path,String,FileAttribute[])} method for the case
@@ -2583,7 +2584,7 @@
* walkFileTree(start, EnumSet.noneOf(FileVisitOption.class), Integer.MAX_VALUE, visitor)
* </pre></blockquote>
* In other words, it does not follow symbolic links, and visits all levels
- * of the file level.
+ * of the file tree.
*
* @param start
* the starting file
@@ -3005,7 +3006,7 @@
* or after some bytes have been written to the file.
*
* <p> <b>Usage example</b>: By default the method creates a new file or
- * overrides an existing file. Suppose you instead want to append bytes
+ * overwrites an existing file. Suppose you instead want to append bytes
* to an existing file:
* <pre>
* Path path = ...
--- a/jdk/src/share/classes/sun/nio/ch/FileChannelImpl.java Tue Feb 22 10:19:35 2011 +0000
+++ b/jdk/src/share/classes/sun/nio/ch/FileChannelImpl.java Tue Feb 22 12:04:48 2011 +0000
@@ -475,8 +475,8 @@
assert !target.isOpen();
try {
close();
- } catch (IOException ignore) {
- // nothing we can do
+ } catch (Throwable suppressed) {
+ e.addSuppressed(suppressed);
}
throw e;
} catch (IOException ioe) {
--- a/jdk/src/solaris/classes/sun/nio/ch/UnixAsynchronousServerSocketChannelImpl.java Tue Feb 22 10:19:35 2011 +0000
+++ b/jdk/src/solaris/classes/sun/nio/ch/UnixAsynchronousServerSocketChannelImpl.java Tue Feb 22 12:04:48 2011 +0000
@@ -236,7 +236,9 @@
} catch (SecurityException x) {
try {
ch.close();
- } catch (IOException ignore) { }
+ } catch (Throwable suppressed) {
+ x.addSuppressed(suppressed);
+ }
throw x;
}
return ch;
--- a/jdk/src/solaris/classes/sun/nio/ch/UnixAsynchronousSocketChannelImpl.java Tue Feb 22 10:19:35 2011 +0000
+++ b/jdk/src/solaris/classes/sun/nio/ch/UnixAsynchronousSocketChannelImpl.java Tue Feb 22 12:04:48 2011 +0000
@@ -255,10 +255,11 @@
// close channel if connection cannot be established
try {
close();
- } catch (IOException ignore) { }
+ } catch (Throwable suppressed) {
+ e.addSuppressed(suppressed);
+ }
}
-
// invoke handler and set result
CompletionHandler<Void,Object> handler = connectHandler;
Object att = connectAttachment;
@@ -345,7 +346,9 @@
if (e != null) {
try {
close();
- } catch (IOException ignore) { }
+ } catch (Throwable suppressed) {
+ e.addSuppressed(suppressed);
+ }
}
if (handler == null) {
return CompletedFuture.withResult(null, e);
--- a/jdk/test/java/lang/ProcessBuilder/Basic.java Tue Feb 22 10:19:35 2011 +0000
+++ b/jdk/test/java/lang/ProcessBuilder/Basic.java Tue Feb 22 12:04:48 2011 +0000
@@ -1762,9 +1762,9 @@
equal(p.exitValue(), 5);
- p.getInputStream().close();
- p.getErrorStream().close();
- p.getOutputStream().close();
+ try { p.getInputStream().close(); } catch (IOException ignore) { }
+ try {p.getErrorStream().close(); } catch (IOException ignore) { }
+ try { p.getOutputStream().close(); } catch (IOException ignore) { }
InputStream[] streams = { p.getInputStream(), p.getErrorStream() };
for (final InputStream in : streams) {