提交 57de7398 编写于 作者: A alanb

7020888: (file) Miscellaneous and trivial clean-ups (typos and opportunities...

7020888: (file) Miscellaneous and trivial clean-ups (typos and opportunities to use suppressed exceptions)
Reviewed-by: mduigou, chegar
上级 1bccae0d
...@@ -512,11 +512,14 @@ public class BufferedReader extends Reader { ...@@ -512,11 +512,14 @@ public class BufferedReader extends Reader {
public void close() throws IOException { public void close() throws IOException {
synchronized (lock) { synchronized (lock) {
if (in == null) if (in != null) {
return; try {
in.close(); in.close();
in = null; } finally {
cb = null; in = null;
cb = null;
}
}
} }
} }
} }
...@@ -255,17 +255,16 @@ public class BufferedWriter extends Writer { ...@@ -255,17 +255,16 @@ public class BufferedWriter extends Writer {
} }
} }
@SuppressWarnings("try")
public void close() throws IOException { public void close() throws IOException {
synchronized (lock) { synchronized (lock) {
if (out == null) { if (out != null) {
return; try (Writer w = out) {
} flushBuffer();
try { } finally {
flushBuffer(); out = null;
} finally { cb = null;
out.close(); }
out = null;
cb = null;
} }
} }
} }
......
...@@ -2055,7 +2055,7 @@ public class File ...@@ -2055,7 +2055,7 @@ public class File
* *
* @return a {@code Path} constructed from this abstract path * @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 * if a {@code Path} object cannot be constructed from the abstract
* path (see {@link java.nio.file.FileSystem#getPath FileSystem.getPath}) * path (see {@link java.nio.file.FileSystem#getPath FileSystem.getPath})
* *
......
...@@ -152,11 +152,10 @@ class FilterOutputStream extends OutputStream { ...@@ -152,11 +152,10 @@ class FilterOutputStream extends OutputStream {
* @see java.io.FilterOutputStream#flush() * @see java.io.FilterOutputStream#flush()
* @see java.io.FilterOutputStream#out * @see java.io.FilterOutputStream#out
*/ */
@SuppressWarnings("try")
public void close() throws IOException { public void close() throws IOException {
try { try (OutputStream ostream = out) {
flush(); flush();
} catch (IOException ignored) {
} }
out.close();
} }
} }
...@@ -374,10 +374,13 @@ class PushbackInputStream extends FilterInputStream { ...@@ -374,10 +374,13 @@ class PushbackInputStream extends FilterInputStream {
* @exception IOException if an I/O error occurs. * @exception IOException if an I/O error occurs.
*/ */
public synchronized void close() throws IOException { public synchronized void close() throws IOException {
if (in == null) if (in != null) {
return; try {
in.close(); in.close();
in = null; } finally {
buf = null; in = null;
buf = null;
}
}
} }
} }
...@@ -245,8 +245,11 @@ public class PushbackReader extends FilterReader { ...@@ -245,8 +245,11 @@ public class PushbackReader extends FilterReader {
* @exception IOException If an I/O error occurs * @exception IOException If an I/O error occurs
*/ */
public void close() throws IOException { public void close() throws IOException {
super.close(); try {
buf = null; super.close();
} finally {
buf = null;
}
} }
/** /**
......
...@@ -53,7 +53,7 @@ import java.util.Collections; ...@@ -53,7 +53,7 @@ import java.util.Collections;
* operation. This class also defines read and write methods that initiate * operation. This class also defines read and write methods that initiate
* asynchronous operations, returning a {@link Future} to represent the pending * asynchronous operations, returning a {@link Future} to represent the pending
* result of the operation. The {@code Future} may be used to check if the * 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 * <p> In addition to read and write operations, this class defines the
* following operations: </p> * following operations: </p>
...@@ -79,7 +79,7 @@ import java.util.Collections; ...@@ -79,7 +79,7 @@ import java.util.Collections;
* itself a thread in the thread pool, then the completion handler may be invoked * itself a thread in the thread pool, then the completion handler may be invoked
* directly by the initiating thread. When an {@code AsynchronousFileChannel} is * directly by the initiating thread. When an {@code AsynchronousFileChannel} is
* created without specifying a thread pool then the channel is associated with * 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 * channels. The default thread pool is configured by the system properties
* defined by the {@link AsynchronousChannelGroup} class. * defined by the {@link AsynchronousChannelGroup} class.
* *
......
...@@ -182,10 +182,13 @@ public abstract class SocketChannel ...@@ -182,10 +182,13 @@ public abstract class SocketChannel
SocketChannel sc = open(); SocketChannel sc = open();
try { try {
sc.connect(remote); sc.connect(remote);
} finally { } catch (Throwable x) {
if (!sc.isConnected()) { try {
try { sc.close(); } catch (IOException x) { } sc.close();
} catch (Throwable suppressed) {
x.addSuppressed(suppressed);
} }
throw x;
} }
assert sc.isConnected(); assert sc.isConnected();
return sc; return sc;
......
...@@ -135,11 +135,13 @@ class CopyMoveHelper { ...@@ -135,11 +135,13 @@ class CopyMoveHelper {
view.setTimes(attrs.lastModifiedTime(), view.setTimes(attrs.lastModifiedTime(),
attrs.lastAccessTime(), attrs.lastAccessTime(),
attrs.creationTime()); attrs.creationTime());
} catch (IOException x) { } catch (Throwable x) {
// rollback // rollback
try { try {
Files.delete(target); Files.delete(target);
} catch (IOException ignore) { } } catch (Throwable suppressed) {
x.addSuppressed(suppressed);
}
throw x; throw x;
} }
} }
......
...@@ -129,17 +129,18 @@ public final class Files { ...@@ -129,17 +129,18 @@ public final class Files {
* <pre> * <pre>
* Path path = ... * 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); * OutputStream out = Files.newOutputStream(path);
* *
* // append to an existing file, fail if the file does not exist * // append to an existing file, fail if the file does not exist
* out = Files.newOutputStream(path, APPEND); * out = Files.newOutputStream(path, APPEND);
* *
* // append to an existing file, create file if it doesn't initially exist * // 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 * // always create new file, failing if it already exists
* out = Files.newOutputStream(CREATE_NEW); * out = Files.newOutputStream(path, CREATE_NEW);
* </pre> * </pre>
* *
* @param path * @param path
...@@ -895,8 +896,8 @@ public final class Files { ...@@ -895,8 +896,8 @@ public final class Files {
/** /**
* Creates a new directory in the default temporary-file directory, using * Creates a new directory in the default temporary-file directory, using
* the given prefix and suffix to generate its name. The resulting {@code * the given prefix to generate its name. The resulting {@code Path} is
* Path} is associated with the default {@code FileSystem}. * associated with the default {@code FileSystem}.
* *
* <p> This method works in exactly the manner specified by {@link * <p> This method works in exactly the manner specified by {@link
* #createTempDirectory(Path,String,FileAttribute[])} method for the case * #createTempDirectory(Path,String,FileAttribute[])} method for the case
...@@ -2583,7 +2584,7 @@ public final class Files { ...@@ -2583,7 +2584,7 @@ public final class Files {
* walkFileTree(start, EnumSet.noneOf(FileVisitOption.class), Integer.MAX_VALUE, visitor) * walkFileTree(start, EnumSet.noneOf(FileVisitOption.class), Integer.MAX_VALUE, visitor)
* </pre></blockquote> * </pre></blockquote>
* In other words, it does not follow symbolic links, and visits all levels * In other words, it does not follow symbolic links, and visits all levels
* of the file level. * of the file tree.
* *
* @param start * @param start
* the starting file * the starting file
...@@ -3005,7 +3006,7 @@ public final class Files { ...@@ -3005,7 +3006,7 @@ public final class Files {
* or after some bytes have been written to the file. * or after some bytes have been written to the file.
* *
* <p> <b>Usage example</b>: By default the method creates a new file or * <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: * to an existing file:
* <pre> * <pre>
* Path path = ... * Path path = ...
......
...@@ -475,8 +475,8 @@ public class FileChannelImpl ...@@ -475,8 +475,8 @@ public class FileChannelImpl
assert !target.isOpen(); assert !target.isOpen();
try { try {
close(); close();
} catch (IOException ignore) { } catch (Throwable suppressed) {
// nothing we can do e.addSuppressed(suppressed);
} }
throw e; throw e;
} catch (IOException ioe) { } catch (IOException ioe) {
......
...@@ -236,7 +236,9 @@ class UnixAsynchronousServerSocketChannelImpl ...@@ -236,7 +236,9 @@ class UnixAsynchronousServerSocketChannelImpl
} catch (SecurityException x) { } catch (SecurityException x) {
try { try {
ch.close(); ch.close();
} catch (IOException ignore) { } } catch (Throwable suppressed) {
x.addSuppressed(suppressed);
}
throw x; throw x;
} }
return ch; return ch;
......
...@@ -255,10 +255,11 @@ class UnixAsynchronousSocketChannelImpl ...@@ -255,10 +255,11 @@ class UnixAsynchronousSocketChannelImpl
// close channel if connection cannot be established // close channel if connection cannot be established
try { try {
close(); close();
} catch (IOException ignore) { } } catch (Throwable suppressed) {
e.addSuppressed(suppressed);
}
} }
// invoke handler and set result // invoke handler and set result
CompletionHandler<Void,Object> handler = connectHandler; CompletionHandler<Void,Object> handler = connectHandler;
Object att = connectAttachment; Object att = connectAttachment;
...@@ -345,7 +346,9 @@ class UnixAsynchronousSocketChannelImpl ...@@ -345,7 +346,9 @@ class UnixAsynchronousSocketChannelImpl
if (e != null) { if (e != null) {
try { try {
close(); close();
} catch (IOException ignore) { } } catch (Throwable suppressed) {
e.addSuppressed(suppressed);
}
} }
if (handler == null) { if (handler == null) {
return CompletedFuture.withResult(null, e); return CompletedFuture.withResult(null, e);
......
...@@ -1762,9 +1762,9 @@ public class Basic { ...@@ -1762,9 +1762,9 @@ public class Basic {
equal(p.exitValue(), 5); equal(p.exitValue(), 5);
p.getInputStream().close(); try { p.getInputStream().close(); } catch (IOException ignore) { }
p.getErrorStream().close(); try {p.getErrorStream().close(); } catch (IOException ignore) { }
p.getOutputStream().close(); try { p.getOutputStream().close(); } catch (IOException ignore) { }
InputStream[] streams = { p.getInputStream(), p.getErrorStream() }; InputStream[] streams = { p.getInputStream(), p.getErrorStream() };
for (final InputStream in : streams) { for (final InputStream in : streams) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册