提交 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 {
public void close() throws IOException {
synchronized (lock) {
if (in == null)
return;
if (in != null) {
try {
in.close();
} finally {
in = null;
cb = null;
}
}
}
}
}
......@@ -255,18 +255,17 @@ public class BufferedWriter extends Writer {
}
}
@SuppressWarnings("try")
public void close() throws IOException {
synchronized (lock) {
if (out == null) {
return;
}
try {
if (out != null) {
try (Writer w = out) {
flushBuffer();
} finally {
out.close();
out = null;
cb = null;
}
}
}
}
}
......@@ -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})
*
......
......@@ -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 {
try (OutputStream ostream = out) {
flush();
} catch (IOException ignored) {
}
out.close();
}
}
......@@ -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;
if (in != null) {
try {
in.close();
} finally {
in = null;
buf = null;
}
}
}
}
......@@ -245,9 +245,12 @@ public class PushbackReader extends FilterReader {
* @exception IOException If an I/O error occurs
*/
public void close() throws IOException {
try {
super.close();
} finally {
buf = null;
}
}
/**
* Skips characters. This method will block until some characters are
......
......@@ -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.
*
* <p> In addition to read and write operations, this class defines the
* following operations: </p>
......@@ -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.
*
......
......@@ -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;
......
......@@ -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;
}
}
......
......@@ -129,17 +129,18 @@ public final class Files {
* <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 @@ 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}.
*
* <p> 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)
* </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 @@ public final class Files {
* 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 = ...
......
......@@ -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) {
......
......@@ -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;
......
......@@ -255,9 +255,10 @@ 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<Void,Object> handler = connectHandler;
......@@ -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);
......
......@@ -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) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册