66
77import java .io .BufferedReader ;
88import java .io .BufferedWriter ;
9- import java .io .Closeable ;
109import java .io .DataOutputStream ;
1110import java .io .File ;
1211import java .io .FileOutputStream ;
2120
2221public class EnvUtils {
2322
24- /**
25- * Closeable helper
26- *
27- * @param c closable object
28- */
29- private static void close (Closeable c ) {
30- if (c != null ) {
31- try {
32- c .close ();
33- } catch (IOException e ) {
34- e .printStackTrace ();
35- }
36- }
37- }
38-
3923 /**
4024 * Extract file to env directory
4125 *
@@ -47,28 +31,22 @@ private static void close(Closeable c) {
4731 */
4832 private static boolean extractFile (Context c , String target , String rootAsset , String path ) {
4933 AssetManager assetManager = c .getAssets ();
50- InputStream in = null ;
51- OutputStream out = null ;
52- boolean result = true ;
53- try {
54- in = assetManager .open (rootAsset + path );
34+
35+ try (InputStream in = assetManager .open (rootAsset + path )) {
5536 File fname = new File (target + path );
5637 fname .delete ();
57- out = new FileOutputStream (fname );
58- byte [] buffer = new byte [1024 ];
59- int read ;
60- while ((read = in .read (buffer )) != -1 ) {
61- out .write (buffer , 0 , read );
38+ try (OutputStream out = new FileOutputStream (fname )) {
39+ byte [] buffer = new byte [1024 ];
40+ int read ;
41+ while ((read = in .read (buffer )) != -1 ) {
42+ out .write (buffer , 0 , read );
43+ }
44+ out .flush ();
6245 }
63- out . flush () ;
46+ return true ;
6447 } catch (IOException e ) {
65- e .printStackTrace ();
66- result = false ;
67- } finally {
68- close (in );
69- close (out );
48+ return false ;
7049 }
71- return result ;
7250 }
7351
7452 /**
@@ -148,45 +126,20 @@ private static void setPermissions(File path, Boolean executable) {
148126 * @return true if success
149127 */
150128 private static boolean isRooted () {
151- boolean result = false ;
152- OutputStream stdin = null ;
153- InputStream stdout = null ;
154- int n = 0 ;
155129 try {
156130 Process process = Runtime .getRuntime ().exec ("su" );
157- stdin = process .getOutputStream ();
158- stdout = process .getInputStream ();
159-
160- DataOutputStream os = null ;
161- try {
162- os = new DataOutputStream (stdin );
163- os .writeBytes ("ls /data\n " );
164- os .writeBytes ("exit\n " );
165- os .flush ();
166- } catch (IOException e ) {
167- e .printStackTrace ();
168- } finally {
169- close (os );
170- }
131+ try (DataOutputStream stdin = new DataOutputStream (process .getOutputStream ());
132+ BufferedReader stdout = new BufferedReader (new InputStreamReader (process .getInputStream ()))) {
171133
172- BufferedReader reader = null ;
173- try {
174- reader = new BufferedReader (new InputStreamReader (stdout ));
175- while (reader .readLine () != null ) {
176- n ++;
177- }
178- } catch (IOException e ) {
179- e .printStackTrace ();
180- } finally {
181- close (reader );
134+ stdin .writeBytes ("ls /data\n " );
135+ stdin .writeBytes ("exit\n " );
136+ stdin .flush ();
137+
138+ return stdout .readLine () != null ;
182139 }
183140 } catch (IOException e ) {
184- e .printStackTrace ();
185- } finally {
186- close (stdout );
187- close (stdin );
141+ return false ;
188142 }
189- return n > 0 ;
190143 }
191144
192145 /**
@@ -196,19 +149,13 @@ private static boolean isRooted() {
196149 * @return true if success
197150 */
198151 private static boolean setVersion (Context c ) {
199- boolean result = false ;
200152 String f = PrefStore .getEnvDir (c ) + "/version" ;
201- BufferedWriter bw = null ;
202- try {
203- bw = new BufferedWriter (new FileWriter (f ));
153+ try (BufferedWriter bw = new BufferedWriter (new FileWriter (f ))) {
204154 bw .write (PrefStore .getVersion ());
205- result = true ;
155+ return true ;
206156 } catch (IOException e ) {
207- e .printStackTrace ();
208- } finally {
209- close (bw );
157+ return false ;
210158 }
211- return result ;
212159 }
213160
214161 /**
@@ -220,18 +167,13 @@ private static boolean setVersion(Context c) {
220167 public static boolean isLatestVersion (Context c ) {
221168 File f = new File (PrefStore .getEnvDir (c ) + "/version" );
222169 if (!f .exists ()) return false ;
223- boolean result = false ;
224- BufferedReader br = null ;
225- try {
226- br = new BufferedReader (new FileReader (f ));
170+
171+ try (BufferedReader br = new BufferedReader (new FileReader (f ))) {
227172 String line = br .readLine ();
228- if ( PrefStore .getVersion ().equals (line )) result = true ;
173+ return PrefStore .getVersion ().equals (line );
229174 } catch (IOException e ) {
230- e .printStackTrace ();
231- } finally {
232- close (br );
175+ return false ;
233176 }
234- return result ;
235177 }
236178
237179 /**
@@ -246,14 +188,12 @@ public static boolean exec(final Context c, final String shell, final List<Strin
246188 Logger .log (c , "No scripts for processing.\n " );
247189 return false ;
248190 }
249- if ("su" .equals (shell )) {
250- if (!isRooted ()) {
251- Logger .log (c , "Requires superuser privileges (root).\n " );
252- return false ;
253- }
191+
192+ if ("su" .equals (shell ) && !isRooted ()) {
193+ Logger .log (c , "Requires superuser privileges (root).\n " );
194+ return false ;
254195 }
255- boolean result = false ;
256- OutputStream stdin = null ;
196+
257197 try {
258198 ProcessBuilder pb = new ProcessBuilder (shell );
259199 pb .directory (new File (PrefStore .getEnvDir (c )));
@@ -262,43 +202,37 @@ public static boolean exec(final Context c, final String shell, final List<Strin
262202 if (PrefStore .isDebugMode (c )) pb .redirectErrorStream (true );
263203 Process process = pb .start ();
264204
265- stdin = process .getOutputStream ();
266- final InputStream stdout = process .getInputStream ();
267- // final InputStream stderr = process.getErrorStream();
268-
269- // params.add(0, "LD_LIBRARY_PATH=" + PrefStore.getLibsDir(c) + ":$LD_LIBRARY_PATH");
270- params .add (0 , "PATH=" + PrefStore .getBinDir (c ) + ":$PATH" );
271- if (PrefStore .isTraceMode (c )) params .add (0 , "set -x" );
272- params .add ("exit $?" );
273-
274- DataOutputStream os = null ;
275- try {
276- os = new DataOutputStream (stdin );
277- for (String cmd : params ) {
278- os .writeBytes (cmd + "\n " );
205+ try (DataOutputStream os = new DataOutputStream (process .getOutputStream ());
206+ InputStream stdout = process .getInputStream ()) {
207+ // final InputStream stderr = process.getErrorStream();
208+
209+ // params.add(0, "LD_LIBRARY_PATH=" + PrefStore.getLibsDir(c) + ":$LD_LIBRARY_PATH");
210+ params .add (0 , "PATH=" + PrefStore .getBinDir (c ) + ":$PATH" );
211+ if (PrefStore .isTraceMode (c ))
212+ params .add (0 , "set -x" );
213+ params .add ("exit $?" );
214+
215+ try {
216+ for (String cmd : params ) {
217+ os .writeBytes (cmd + "\n " );
218+ }
219+ os .flush ();
220+ } catch (IOException e ) {
221+ e .printStackTrace ();
279222 }
280- os .flush ();
281- } catch (IOException e ) {
282- e .printStackTrace ();
283- } finally {
284- close (os );
285- }
286223
287- (new Thread () {
288- @ Override
289- public void run () {
290- Logger .log (c , stdout );
291- }
292- }).start ();
224+ (new Thread () {
225+ @ Override
226+ public void run () {
227+ Logger .log (c , stdout );
228+ }
229+ }).start ();
230+ }
293231
294- if (process .waitFor () == 0 ) result = true ;
295- } catch (Exception e ) {
296- result = false ;
297- e .printStackTrace ();
298- } finally {
299- close (stdin );
232+ return process .waitFor () == 0 ;
233+ } catch (IOException | InterruptedException e ) {
234+ return false ;
300235 }
301- return result ;
302236 }
303237
304238 /**
@@ -399,22 +333,17 @@ static boolean updateEnv(final Context c) {
399333 * @return true if success
400334 */
401335 private static boolean makeMainScript (Context c ) {
402- boolean result = false ;
403336 String scriptFile = PrefStore .getBinDir (c ) + "/linuxdeploy" ;
404- BufferedWriter bw = null ;
405- try {
406- bw = new BufferedWriter (new FileWriter (scriptFile ));
337+
338+ try (BufferedWriter bw = new BufferedWriter (new FileWriter (scriptFile ))) {
407339 bw .write ("#!" + PrefStore .getShell (c ) + "\n " );
408340 bw .write ("PATH=" + PrefStore .getPath (c ) + ":$PATH\n " );
409341 bw .write ("ENV_DIR=\" " + PrefStore .getEnvDir (c ) + "\" \n " );
410342 bw .write (". \" ${ENV_DIR}/cli.sh\" \n " );
411- result = true ;
343+ return true ;
412344 } catch (IOException e ) {
413- e .printStackTrace ();
414- } finally {
415- close (bw );
345+ return false ;
416346 }
417- return result ;
418347 }
419348
420349 /**
@@ -499,7 +428,7 @@ static boolean telnetd(Context c, String cmd) {
499428 if (cmd .equals ("stop" )) break ;
500429 case "start" :
501430 if (!PrefStore .isTelnet (c )) break ;
502- makeIssueFile (c , PrefStore .getEnvDir (c ) + "/issue" );
431+ makeIssueFile (PrefStore .getEnvDir (c ) + "/issue" );
503432 String args = "" ;
504433 args += " -l " + PrefStore .getShell (c );
505434 args += " -p " + PrefStore .getTelnetPort (c );
@@ -551,40 +480,27 @@ static boolean httpd(Context c, String cmd) {
551480 * @return true if success
552481 */
553482 private static boolean makeHttpdConf (Context c , String f ) {
554- boolean result = false ;
555- BufferedWriter bw = null ;
556- try {
557- bw = new BufferedWriter (new FileWriter (f ));
483+ try (BufferedWriter bw = new BufferedWriter (new FileWriter (f ))) {
558484 for (String part : PrefStore .getHttpConf (c ).split (" " )) {
559485 bw .write (part + "\n " );
560486 }
561- result = true ;
487+ return true ;
562488 } catch (IOException e ) {
563- e .printStackTrace ();
564- } finally {
565- close (bw );
489+ return false ;
566490 }
567- return result ;
568491 }
569492
570493 /**
571494 * Make issue file
572495 *
573- * @param c context
574496 * @return true if success
575497 */
576- private static boolean makeIssueFile (Context c , String f ) {
577- boolean result = false ;
578- BufferedWriter bw = null ;
579- try {
580- bw = new BufferedWriter (new FileWriter (f ));
498+ private static boolean makeIssueFile (String f ) {
499+ try (BufferedWriter bw = new BufferedWriter (new FileWriter (f ))) {
581500 bw .write ("Linux Deploy \\ m \\ l\n " );
582- result = true ;
501+ return true ;
583502 } catch (IOException e ) {
584- e .printStackTrace ();
585- } finally {
586- close (bw );
503+ return false ;
587504 }
588- return result ;
589505 }
590506}
0 commit comments