55import java .io .BufferedReader ;
66import java .io .IOException ;
77import java .io .InputStreamReader ;
8+ import java .util .concurrent .TimeUnit ;
89
910/**
1011 * Shuts down or restarts the computer.
@@ -37,12 +38,13 @@ public void execute(String[] args, ShellContext context) throws IOException {
3738 System .out .println ("WARNING: This will execute a real shutdown command!" );
3839 System .out .print ("Are you sure you want to continue? (yes/no): " );
3940
40- java .util .Scanner scanner = new java .util .Scanner (System .in );
41- String confirmation = scanner .nextLine ().trim ().toLowerCase ();
42-
43- if (!confirmation .equals ("yes" )) {
44- System .out .println ("Shutdown cancelled." );
45- return ;
41+ try (java .util .Scanner scanner = new java .util .Scanner (System .in )) {
42+ String confirmation = scanner .nextLine ().trim ().toLowerCase ();
43+ if (!confirmation .equals ("yes" )) {
44+ System .out .println ("Shutdown cancelled." );
45+ return ;
46+ }
47+ }
4648 }
4749
4850 try {
@@ -61,21 +63,15 @@ public void execute(String[] args, ShellContext context) throws IOException {
6163 }
6264
6365 Process process = pb .start ();
64- BufferedReader reader = new BufferedReader (new InputStreamReader (process .getInputStream ()));
65- BufferedReader errorReader =
66- new BufferedReader (new InputStreamReader (process .getErrorStream ()));
66+ try (java .util .Scanner scanner = new java .util .Scanner (System .in )) {
67+ String confirmation = scanner .nextLine ().trim ().toLowerCase ();
6768
68- String line ;
69- while ((line = reader .readLine ()) != null ) {
70- System .out .println (line );
69+ if (!confirmation .equals ("yes" )) {
70+ System .out .println ("Shutdown cancelled." );
71+ return ;
72+ }
7173 }
7274
73- while ((line = errorReader .readLine ()) != null ) {
74- System .err .println (line );
75- }
76-
77- process .waitFor ();
78-
7975 } catch (Exception e ) {
8076 System .out .println ("Error executing shutdown: " + e .getMessage ());
8177 System .out .println ("Administrator privileges may be required." );
0 commit comments