-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathCloseWriter.qhelp
61 lines (42 loc) · 2.12 KB
/
CloseWriter.qhelp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>A subclass of <code>Writer</code> or <code>OutputStream</code> that is opened for writing
but not properly closed later may cause a resource leak.
</p>
</overview>
<recommendation>
<p>Ensure that the resource is always closed to avoid a resource leak. Note that, because of exceptions,
it is safest to close a resource properly in a <code>finally</code> block. (However, this is unnecessary for
subclasses of <code>CharArrayWriter</code>, <code>StringWriter</code> and <code>ByteArrayOutputStream</code>.)</p>
<p>For Java 7 or later, the recommended way to close resources that implement <code>java.lang.AutoCloseable</code>
is to declare them within a <code>try-with-resources</code> statement, so that they are closed implicitly.</p>
</recommendation>
<example>
<p>In the following example, the resource <code>bw</code> is opened but not closed.</p>
<sample src="CloseWriter.java" />
<p>In the following example, the resource <code>bw</code> is opened in a <code>try</code> block and
later closed in a <code>finally</code> block.</p>
<sample src="CloseWriterGood.java" />
<p>Note that nested class instance creation expressions of <code>Writer</code>s or <code>OutputStream</code>s
are not safe to use if the constructor of the outer expression may throw an exception. In the following
example, the <code>OutputStreamWriter</code> may throw an exception, in which case the inner <code>FileOutputStream</code>
is not closed.
</p>
<sample src="CloseWriterNested.java" />
<p>
In this case, the inner expression needs to be assigned to a local variable and closed separately, as shown below.
</p>
<sample src="CloseWriterNestedGood.java" />
</example>
<references>
<li>
IBM developerWorks: <a href="https://web.archive.org/web/20201109041839/http://www.ibm.com/developerworks/java/library/j-jtp03216/index.html">Java theory and practice: Good housekeeping practices</a>.
</li>
<li>
The Java Tutorials: <a href="https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html">The try-with-resources Statement</a>.
</li>
</references>
</qhelp>