diff --git a/jdk/src/share/classes/java/io/BufferedReader.java b/jdk/src/share/classes/java/io/BufferedReader.java index e8583e046f0..45a71f5cf1c 100644 --- a/jdk/src/share/classes/java/io/BufferedReader.java +++ b/jdk/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/jdk/src/share/classes/java/io/BufferedWriter.java b/jdk/src/share/classes/java/io/BufferedWriter.java index 4322683d367..fad7b44add4 100644 --- a/jdk/src/share/classes/java/io/BufferedWriter.java +++ b/jdk/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/jdk/src/share/classes/java/io/File.java b/jdk/src/share/classes/java/io/File.java index cde70d0b977..af198a5ed48 100644 --- a/jdk/src/share/classes/java/io/File.java +++ b/jdk/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/jdk/src/share/classes/java/io/FilterOutputStream.java b/jdk/src/share/classes/java/io/FilterOutputStream.java index 97154443a81..b7e920b1753 100644 --- a/jdk/src/share/classes/java/io/FilterOutputStream.java +++ b/jdk/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/jdk/src/share/classes/java/io/PushbackInputStream.java b/jdk/src/share/classes/java/io/PushbackInputStream.java index a0e240f1d84..b6583f1dfb4 100644 --- a/jdk/src/share/classes/java/io/PushbackInputStream.java +++ b/jdk/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/jdk/src/share/classes/java/io/PushbackReader.java b/jdk/src/share/classes/java/io/PushbackReader.java index 3add8a5d3f7..af44ee72fd3 100644 --- a/jdk/src/share/classes/java/io/PushbackReader.java +++ b/jdk/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/jdk/src/share/classes/java/nio/channels/AsynchronousFileChannel.java b/jdk/src/share/classes/java/nio/channels/AsynchronousFileChannel.java index 1ca3417cce2..4f4ac7f4a36 100644 --- a/jdk/src/share/classes/java/nio/channels/AsynchronousFileChannel.java +++ b/jdk/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/jdk/src/share/classes/java/nio/channels/SocketChannel.java b/jdk/src/share/classes/java/nio/channels/SocketChannel.java index fe044c4741d..b582c6d656a 100644 --- a/jdk/src/share/classes/java/nio/channels/SocketChannel.java +++ b/jdk/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/jdk/src/share/classes/java/nio/file/CopyMoveHelper.java b/jdk/src/share/classes/java/nio/file/CopyMoveHelper.java index 70ca6ee50ad..54bfe085962 100644 --- a/jdk/src/share/classes/java/nio/file/CopyMoveHelper.java +++ b/jdk/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/jdk/src/share/classes/java/nio/file/Files.java b/jdk/src/share/classes/java/nio/file/Files.java index 357529ac9cf..e509440696b 100644 --- a/jdk/src/share/classes/java/nio/file/Files.java +++ b/jdk/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/jdk/src/share/classes/sun/nio/ch/FileChannelImpl.java b/jdk/src/share/classes/sun/nio/ch/FileChannelImpl.java
index b15086eab12..c52fff788e9 100644
--- a/jdk/src/share/classes/sun/nio/ch/FileChannelImpl.java
+++ b/jdk/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/jdk/src/solaris/classes/sun/nio/ch/UnixAsynchronousServerSocketChannelImpl.java b/jdk/src/solaris/classes/sun/nio/ch/UnixAsynchronousServerSocketChannelImpl.java
index 119146b68f4..ee3731a0c2e 100644
--- a/jdk/src/solaris/classes/sun/nio/ch/UnixAsynchronousServerSocketChannelImpl.java
+++ b/jdk/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/jdk/src/solaris/classes/sun/nio/ch/UnixAsynchronousSocketChannelImpl.java b/jdk/src/solaris/classes/sun/nio/ch/UnixAsynchronousSocketChannelImpl.java
index 4d887890ccd..13e4ad36434 100644
--- a/jdk/src/solaris/classes/sun/nio/ch/UnixAsynchronousSocketChannelImpl.java
+++ b/jdk/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
         CompletionHandler handler = 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/jdk/test/java/lang/ProcessBuilder/Basic.java b/jdk/test/java/lang/ProcessBuilder/Basic.java
index ebe65fb5bad..e109a4e78bd 100644
--- a/jdk/test/java/lang/ProcessBuilder/Basic.java
+++ b/jdk/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) {