diff --git a/src/share/classes/java/io/BufferedReader.java b/src/share/classes/java/io/BufferedReader.java index e8583e046f07c1d490797c6b385026e900be9129..45a71f5cf1c2abb3ff38bde26a016e323e906689 100644 --- a/src/share/classes/java/io/BufferedReader.java +++ b/src/share/classes/java/io/BufferedReader.java @@ -512,11 +512,14 @@ public class BufferedReader extends Reader { 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; + } + } } } } diff --git a/src/share/classes/java/io/BufferedWriter.java b/src/share/classes/java/io/BufferedWriter.java index 4322683d367d270f975efddc897d952a75595e42..fad7b44add49a5dd6e79d23683cff403e79cceb8 100644 --- a/src/share/classes/java/io/BufferedWriter.java +++ b/src/share/classes/java/io/BufferedWriter.java @@ -255,17 +255,16 @@ public class BufferedWriter extends Writer { } } + @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; + } } } } diff --git a/src/share/classes/java/io/File.java b/src/share/classes/java/io/File.java index cde70d0b977019b77b32350b2129509b73dbe1de..af198a5ed486a58ce3462d7aaf175b23ee41e5ae 100644 --- a/src/share/classes/java/io/File.java +++ b/src/share/classes/java/io/File.java @@ -2055,7 +2055,7 @@ public class File * * @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}) * diff --git a/src/share/classes/java/io/FilterOutputStream.java b/src/share/classes/java/io/FilterOutputStream.java index 97154443a81f7515a040b83671fb512d84d0456e..b7e920b1753722793eac650cacb0f650acb2206a 100644 --- a/src/share/classes/java/io/FilterOutputStream.java +++ b/src/share/classes/java/io/FilterOutputStream.java @@ -152,11 +152,10 @@ class FilterOutputStream extends OutputStream { * @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(); } } diff --git a/src/share/classes/java/io/PushbackInputStream.java b/src/share/classes/java/io/PushbackInputStream.java index a0e240f1d84912c9bf5ef7a64934971f951f539e..b6583f1dfb4903dd11f8cb479ff076a2b3a3acf9 100644 --- a/src/share/classes/java/io/PushbackInputStream.java +++ b/src/share/classes/java/io/PushbackInputStream.java @@ -374,10 +374,13 @@ class PushbackInputStream extends FilterInputStream { * @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; + } + } } } diff --git a/src/share/classes/java/io/PushbackReader.java b/src/share/classes/java/io/PushbackReader.java index 3add8a5d3f77a89e4fe9b6f949f71cee89813c12..af44ee72fd308bcf2f3b8c99ebf0389708171319 100644 --- a/src/share/classes/java/io/PushbackReader.java +++ b/src/share/classes/java/io/PushbackReader.java @@ -245,8 +245,11 @@ public class PushbackReader extends FilterReader { * @exception IOException If an I/O error occurs */ public void close() throws IOException { - super.close(); - buf = null; + try { + super.close(); + } finally { + buf = null; + } } /** diff --git a/src/share/classes/java/nio/channels/AsynchronousFileChannel.java b/src/share/classes/java/nio/channels/AsynchronousFileChannel.java index 1ca3417cce22786677e56bfd4a1c2c0a844b8917..4f4ac7f4a366ce324b9162c44330342ad5e7cb7b 100644 --- a/src/share/classes/java/nio/channels/AsynchronousFileChannel.java +++ b/src/share/classes/java/nio/channels/AsynchronousFileChannel.java @@ -53,7 +53,7 @@ import java.util.Collections; * 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. * *
In addition to read and write operations, this class defines the * following operations:
@@ -79,7 +79,7 @@ import java.util.Collections; * 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. * diff --git a/src/share/classes/java/nio/channels/SocketChannel.java b/src/share/classes/java/nio/channels/SocketChannel.java index fe044c4741da16d97975a20cd75b81920d29b353..b582c6d656a6710b89be0443c213cece0a710f78 100644 --- a/src/share/classes/java/nio/channels/SocketChannel.java +++ b/src/share/classes/java/nio/channels/SocketChannel.java @@ -182,10 +182,13 @@ public abstract class SocketChannel 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; diff --git a/src/share/classes/java/nio/file/CopyMoveHelper.java b/src/share/classes/java/nio/file/CopyMoveHelper.java index 70ca6ee50ad3ec5a5491151924461572048d7e16..54bfe085962c218f99d215199d2d3b569d9f51e7 100644 --- a/src/share/classes/java/nio/file/CopyMoveHelper.java +++ b/src/share/classes/java/nio/file/CopyMoveHelper.java @@ -135,11 +135,13 @@ class CopyMoveHelper { 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; } } diff --git a/src/share/classes/java/nio/file/Files.java b/src/share/classes/java/nio/file/Files.java index 357529ac9cf065deaa7301b2d11a167702874537..e509440696b215b061cbe59da4e94797ce84a1fc 100644 --- a/src/share/classes/java/nio/file/Files.java +++ b/src/share/classes/java/nio/file/Files.java @@ -129,17 +129,18 @@ public final class Files { ** 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); ** * @param path @@ -895,8 +896,8 @@ public final class Files { /** * 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}. * *
This method works in exactly the manner specified by {@link * #createTempDirectory(Path,String,FileAttribute[])} method for the case @@ -2583,7 +2584,7 @@ public final class Files { * walkFileTree(start, EnumSet.noneOf(FileVisitOption.class), Integer.MAX_VALUE, visitor) * * 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 @@ public final class Files { * or after some bytes have been written to the file. * *
Usage example: 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: *
* Path path = ... diff --git a/src/share/classes/sun/nio/ch/FileChannelImpl.java b/src/share/classes/sun/nio/ch/FileChannelImpl.java index b15086eab12282ee663d15d143ba04fb91597c92..c52fff788e9d5d244002e71e1b67ceb0d5ad0254 100644 --- a/src/share/classes/sun/nio/ch/FileChannelImpl.java +++ b/src/share/classes/sun/nio/ch/FileChannelImpl.java @@ -475,8 +475,8 @@ public class FileChannelImpl assert !target.isOpen(); try { close(); - } catch (IOException ignore) { - // nothing we can do + } catch (Throwable suppressed) { + e.addSuppressed(suppressed); } throw e; } catch (IOException ioe) { diff --git a/src/solaris/classes/sun/nio/ch/UnixAsynchronousServerSocketChannelImpl.java b/src/solaris/classes/sun/nio/ch/UnixAsynchronousServerSocketChannelImpl.java index 119146b68f43ea23e91f22518acfee41a0e61bfd..ee3731a0c2e29a022b1ae108bea6d990915ff2ea 100644 --- a/src/solaris/classes/sun/nio/ch/UnixAsynchronousServerSocketChannelImpl.java +++ b/src/solaris/classes/sun/nio/ch/UnixAsynchronousServerSocketChannelImpl.java @@ -236,7 +236,9 @@ class UnixAsynchronousServerSocketChannelImpl } catch (SecurityException x) { try { ch.close(); - } catch (IOException ignore) { } + } catch (Throwable suppressed) { + x.addSuppressed(suppressed); + } throw x; } return ch; diff --git a/src/solaris/classes/sun/nio/ch/UnixAsynchronousSocketChannelImpl.java b/src/solaris/classes/sun/nio/ch/UnixAsynchronousSocketChannelImpl.java index 4d887890ccd9241813f426236a6ec15cc6f4fe30..13e4ad36434a02433bfeabe34a35d4851dc90d4d 100644 --- a/src/solaris/classes/sun/nio/ch/UnixAsynchronousSocketChannelImpl.java +++ b/src/solaris/classes/sun/nio/ch/UnixAsynchronousSocketChannelImpl.java @@ -255,10 +255,11 @@ class UnixAsynchronousSocketChannelImpl // close channel if connection cannot be established try { close(); - } catch (IOException ignore) { } + } catch (Throwable suppressed) { + e.addSuppressed(suppressed); + } } - // invoke handler and set result CompletionHandlerhandler = connectHandler; Object att = connectAttachment; @@ -345,7 +346,9 @@ class UnixAsynchronousSocketChannelImpl if (e != null) { try { close(); - } catch (IOException ignore) { } + } catch (Throwable suppressed) { + e.addSuppressed(suppressed); + } } if (handler == null) { return CompletedFuture.withResult(null, e); diff --git a/test/java/lang/ProcessBuilder/Basic.java b/test/java/lang/ProcessBuilder/Basic.java index ebe65fb5bad6e68ebb408c41fc7ca3409cfc7e0f..e109a4e78bd0be22feaad5a72e196c8f38dd4216 100644 --- a/test/java/lang/ProcessBuilder/Basic.java +++ b/test/java/lang/ProcessBuilder/Basic.java @@ -1762,9 +1762,9 @@ public class Basic { 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) {