...
Code Block | ||
---|---|---|
| ||
public class CreateFile {
public static void main(String[] args) throws FileNotFoundException {
final PrintStream out = new PrintStream( new BufferedOutputStream(
new FileOutputStream("foo.txt")));
out.println("hello");
Runtime.getRuntime().exit(1);
}
}
|
...
Code Block | ||
---|---|---|
| ||
public class CreateFile {
public static void main(String[] args) throws FileNotFoundException {
final PrintStream out = new PrintStream( new BufferedOutputStream(
new FileOutputStream("foo.txt")));
out.println("hello");
out.close();
Runtime.getRuntime().exit(1);
}
}
|
...
This compliant solution adds a shutdown hook to close the file. This hook is invoked by Runtime.exit()
and is called before the JVM is halted.
Code Block | ||
---|---|---|
| ||
public class CreateFile {
public static void main(String[] args) throws FileNotFoundException {
final PrintStream out = new PrintStream( new BufferedOutputStream(
new FileOutputStream("foo.txt")));
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
public void run() {
out.close();
}
}));
out.println("hello");
Runtime.getRuntime().exit(1);
}
}
|
...
This noncompliant code example calls Runtime.halt()
instead of Runtime.exit()
. The Runtime.halt()
method stops the JVM without invoking any shutdown hooks; consequently the file is not properly written to or closed.
Code Block | ||
---|---|---|
| ||
public class CreateFile {
public static void main(String[] args) throws FileNotFoundException {
final PrintStream out = new PrintStream( new BufferedOutputStream(
new FileOutputStream("foo.txt")));
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
public void run() {
out.close();
}
}));
out.println("hello");
Runtime.getRuntime().halt(1);
}
}
|
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="1753a6f91e2ab169-cf84fa8b-4eaf4074-af408637-3cfd81aff4c23f9d0acfc5fd"><ac:plain-text-body><![CDATA[ | [[MITRE 07 | AA. Bibliography#MITRE 07]] | [CWE ID 705 | http://cwe.mitre.org/data/definitions/705.html], "Incorrect Control Flow Scoping" | ]]></ac:plain-text-body></ac:structured-macro> |
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="3f6f3cbc6da81645-8bf273ef-4e5a468d-a369bd20-d54591b1e100c7b14693eb1c"><ac:plain-text-body><![CDATA[ | [[ISO/IEC PDTR 24772 | AA. Bibliography#ISO/IEC PDTR 24772]] | "REU Termination strategy" | ]]></ac:plain-text-body></ac:structured-macro> |
...